Socket 编程,一个服务器,多个客户端,互相通信(测试通过)(国外英文资料).doc
文本预览下载声明
Socket 编程,一个服务器,多个客户端,互相通信(测试通过)(国外英文资料)
Socket programming, a server, multiple clients, communicating with each other
The basic code of a Scoket communication and a basic communication
Implementing a server and an n-client chat
Server:
Using System;
Using System. Collections. Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
Using System.Net; / / the Endpoint
Using System.Net.Sockets; / / contains sockets
Using System. The Text;
Using System. Windows. Forms;
Using System. The Threading;
Using System. IO;
The namespace Server
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
TextBox. CheckForIllegalCrossThreadCalls = false; / / close the cross-thread changes control check
}
The Socket sokWatch = null; / / the socket that listens for the client segment connection requests
Thread threadWatch = null; / / responsible for invoking the socket and executing the thread that listens for the request
/ / start the listening button
Private void btnStartListen_Click (object sender, EventArgs e)
{
/ / instantiated socket (ip4 addressing protocol, streaming, TCP)
SokWatch = new Socket (ernetwork, SocketType.)
/ / create IP objects
The IPAddress address = IPAddress.parse (txtIP.text. Trim ());
/ / create network node objects that contain IP and port
The IPEndPoint endpoint = new IPEndPoint (address, int.parse (txtport.text.trim ()));
/ / the listening socket is bound to the corresponding IP and port
SokWatch. Bind (endpoint);
/ / set the listening queue length of 10 (and can handle 10 connection requests at the same time)
SokWatch. Listen (10);
ThreadWatch = new Thread (StartWatch);
ThreadWatch. IsBackground = true;
ThreadWatch. Start ();
Txtshow.appendtext ( start server success... \ r \ n );
}
Dictionary string, ConnectionClient dictConn = new Dictionary string, ConnectionClient ();
Bool isWatch = true;
Region 1. Listens on the connection port by the thread
/ / summary
/ / the listener connection port is invo
显示全部