1, create a console program SocketTcpServer, note references using System.Net; using System.Net.Sockets;
namespace SocketTcpServer { class Program { static List <Client> = clientList new new List <Client> (); // a plurality of client set static void the Main ( String [] args) { // . 1, create Socket the Socket tcpserver = new new the Socket ( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // address family, socket type, protocol // 2, bind the local ip and port number (a card there is a ip, computers may have multiple ip) tcpServer.Bind ( new new IPEndPoint (IPAddress.Parse ( " 192.168.3.3 " ),2000 )); // 3, starts listening tcpServer.Listen ( 10 ); // maximum number of connections Console.WriteLine ( " Server started successfully! " ); The while ( to true ) // enable interfacing to multiple clients { the Socket clientSocket tcpServer.Accept = (); // pauses the current thread, the code until after the client connection, only carried out after Console.WriteLine ( " client connection success! " ); // custom class client client = new new client ( clientSocket); //And each of the logical (messaging) client communication is processed into Client class clientList.Add (Client); // add clients connected to the collection } } } }
New Class Client.cs, used for communication (specific interaction logic) with the client . Note references using System.Net; using System.Net.Sockets; using System.Threading ;
namespace SocketTcpServer { /// <Summary> /// client communications do /// </ Summary> class Client { Private the Socket the clientSocket,; Private the Thread T; // received control information whether or not Private byte [] = Data new new byte [ 1024 ]; // the received message memory it needs public client (the Socket S) // constructor { the clientSocket, = S; // start a thread monitor information from the client terminal T = new newThe Thread (ReceiveMessage); t.start (); } Private void ReceiveMessage () { the while ( to true ) // information has been received client { // before receiving the information, whether the socket connection is disconnected (OFF will always receive the null character) IF (clientSocket.Poll ( 10 , SelectMode.SelectRead)) // if the connection has been closed, returns true, the frequency of 10ms { BREAK ; // out of the loop, the thread terminates } int length = clientSocket.Receive (Data); // receiving a byte array string= Encoding.UTF8.GetString Message (Data, 0 , length); // display string Console.WriteLine ( " received the message: " + Message); } } } }
2, a program created WinForm form SocketTcpClient_WinForm (moving up the interface shown in FIG.), Note that reference using System.Net; using System.Net.Sockets;
namespace SocketTcpClient_WinForm { public partial class the Form1: Form1 { public String IP = " 192.168.3.3 " ; public int Port = 2000 ; the Socket the clientSocket,; // connected to the remote host the Socket Private void the ConnectToServer () // connection server { // 1 Create Socket the clientSocket, = new new the Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 2, connected to the remote host IP address and port number clientSocket.Connect ( new new the IPEndPoint (IPAddress.Parse (IP), Port)); } Private void the SendMessage ( String Message) // send { byte [] = Data Encoding.UTF8.GetBytes (Message); clientSocket.Send (Data ); // send a byte array } public the Form1 () { the InitializeComponent (); the ConnectToServer (); // connection server } Private void the button1_Click ( Object SENDER, EventArgs E) { The SendMessage (textBox1.Text); // send } Private void Form1_FormClosed ( Object SENDER, FormClosedEventArgs E) { clientSocket.Close (); // close the connection. The client must have a code that sentence , or the server will prompt an error or has been receiving a null character. } } }
3, start the server, and then start the client.
[Note] a phenomenon first start the server, and then start the client. Directly close the client, or will send a null character. That break did not immediately jump out of the while loop, after the execution of the code.