1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Socket套接字实现服务器端连接

Socket套接字实现服务器端连接

时间:2023-04-25 09:51:46

相关推荐

Socket套接字实现服务器端连接

1usingSystem;

2usingSystem.Drawing;

3usingSystem.Collections;

ponentModel;

5usingSystem.Windows.Forms;

6usingSystem.Data;

7//新添加的命令空间

;

.Sockets;

10usingSystem.Threading;

11usingSystem.Text;

12usingSystem.IO;

13

14namespaceSocketServer

15{

16/**////<summary>

17///Socket套接字实现服务器端连接。

18///</summary>

19publicclassForm1:System.Windows.Forms.Form

20{

21privateSystem.Windows.Forms.RichTextBoxrichTextBox1;

22privateSystem.Windows.Forms.Buttonbutton1;

23//客户端节点

24privateIPEndPointclient;

25//服务器端Socket

26privateSocketserver;

27//服务器端监听线程

28privateThreadthdSvr;

29//网络数据流

30NetworkStreamstream;

31//写数据流

32TextWriterwriter;

33//读数据流

34TextReaderreader;

35/**////<summary>

36///必需的设计器变量。

37///</summary>

ponentModel.Containercomponents=null;

39//监听线程

40privatevoidThreadServer()

41{

42client=newIPEndPoint(IPAddress.Any,34567);

43server=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

44server.Blocking=true;

45server.Bind(client);

46server.Listen(0);

47while(true)

48{

49Sockett=server.Accept();

50if(t!=null)

51{

52stream=newNetworkStream(t);

53writer=newStreamWriter(stream);

54reader=newStreamReader(stream);

55richTextBox1.Text+="接收到一个连接\n";

56writer.WriteLine("欢迎连接到服务器!");

57writer.Flush();

58writer.WriteLine("您现在可以说话了");

59writer.Flush();

60richTextBox1.Text+=reader.ReadLine();

61richTextBox1.Text+="\n";

62reader.Close();

63writer.Close();

64stream.Close();

65t.Close();

66}

67}

68}

69publicForm1()

70{

71//

72//Windows窗体设计器支持所必需的

73//

74InitializeComponent();

75//

76//TODO:在InitializeComponent调用后添加任何构造函数代码

77//

78}

79

80/**////<summary>

81///清理所有正在使用的资源。

82///</summary>

83protectedoverridevoidDispose(booldisposing)

84{

85if(server!=null)

86server.Close();

87if(!button1.Enabled)

88if(thdSvr!=null)

89thdSvr.Abort();

90if(disposing)

91{

92if(components!=null)

93{

94components.Dispose();

95}

96}

97base.Dispose(disposing);

98}

99

100WindowsFormDesignergeneratedcode#regionWindowsFormDesignergeneratedcode

101/**////<summary>

102///设计器支持所需的方法-不要使用代码编辑器修改

103///此方法的内容。

104///</summary>

105privatevoidInitializeComponent()

106{

107this.richTextBox1=newSystem.Windows.Forms.RichTextBox();

108this.button1=newSystem.Windows.Forms.Button();

109this.SuspendLayout();

110//

111//richTextBox1

112//

113this.richTextBox1.Dock=System.Windows.Forms.DockStyle.Top;

114this.richTextBox1.Name="richTextBox1";

115this.richTextBox1.Size=newSystem.Drawing.Size(416,256);

116this.richTextBox1.TabIndex=0;

117this.richTextBox1.Text="";

118//

119//button1

120//

121this.button1.Location=newSystem.Drawing.Point(152,264);

122this.button1.Name="button1";

123this.button1.TabIndex=1;

124this.button1.Text="启动服务器";

125this.button1.TextAlign=System.Drawing.ContentAlignment.TopCenter;

126this.button1.Click+=newSystem.EventHandler(this.button1_Click);

127//

128//Form1

129//

130this.AutoScaleBaseSize=newSystem.Drawing.Size(8,18);

131this.ClientSize=newSystem.Drawing.Size(416,296);

132this.Controls.AddRange(newSystem.Windows.Forms.Control[]{

133this.button1,

134this.richTextBox1});

135this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.FixedDialog;

136this.Name="Form1";

137this.StartPosition=System.Windows.Forms.FormStartPosition.CenterScreen;

138this.Text="服务器";

139this.ResumeLayout(false);

140

141}

142#endregion

143

144/**////<summary>

145///应用程序的主入口点。

146///</summary>

147[STAThread]

148staticvoidMain()

149{

150Application.Run(newForm1());

151}

152//开始服务器端的运行。

153privatevoidbutton1_Click(objectsender,System.EventArgse)

154{

155thdSvr=newThread(newThreadStart(this.ThreadServer));

156thdSvr.Start();

157button1.Enabled=false;

158}

159}

160}

161

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。