1. Operation Effect
Figure 1 Starting the service side
Figure 2 Starting the client
Figure 3 Customer Message
Figure 4 Service-side messaging
Figure 5 Customer active shutdown, service segment print exception details
2. Server-side source code
Both the server and the client have to add namespace:
Using System.Net;
Using System.Net.Sockets;
Using System.Threading;
Source:
class Program { private static string serverip = "192.168.3.42";//native IP address private static int serverPort = 8888; private static int bufferSize = 1024; private static int count = 0;//= Dialog Number static void main (String[] args) { IPAddress ip = Ipaddress.parse (ServerIP); ipendpoint ipe = new ipendpoint (Ip, serverport); sockEt s = new socket (addressfamily.internetwork, sockettype.stream, PROTOCOLTYPE.TCP); try { s.bind (IPE); s.listen (Ten); console.writeline ("Waiting for Connection ..."); } catch (Exception ex) { Console.WriteLine (ex); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; } thread maintrd = new thread (Run); maintrd.start (s); } /// <summary> /// Start the socket for the server. /// here to change to a multi-user connection, a recmsg thread is new for each new user, and Dictionary<t1, T2> store every pair of Ipe and sockets /// </summary> /// <param name= "O" > Incoming socket Object </param> private static void run (Object o) { socket socket = o as socket; try { socket connsocket = socket. Accept (); // Client and server connection succeeded. Console.WriteLine ("{0}" successfully connected to this machine. ", connsocket.remoteendpoint); //the next thing to the session thread thread recth = new thread (recmsg); &Nbsp; recth.start (Connsocket); thread sendth = new thread ( SENDMSG); Sendth.start (Connsocket); } catch (Exception ex) { console.writeline (ex); throw; } } private statiC void recmsg (Object o) { socket connsocket = o as socket ; while (True) { byte[] buffer = new Byte[bufferSize]; try { int Length = connsocket.receive (buffer); byte[] realbuffer = new byte[length]; array.copy (Buffer, 0, realbuffer, 0, length); string str = System.Text.Encoding.Default.GetString (Realbuffer); console.write ("[{0}] ", count++); console.writeline ("{0} said: {1}.", connsocket.remoteendpoint, str); } catch (Exception ex) { console.writeline (ex); console.readkey (); break; } } } private static&Nbsp;void sendmsg (Object o) { socket connsocket = o as socket ; while (True) { string str = console.readline (); if (str != string . Empty) { byte[] buffer = encoding.default.getbytes (str); connsocket.send (Buffer, buffer. Length, socketflags.none); } } } }
3. Client source Code
Class program { private static string host = "192.168.3.42"; private static int port = 8888; private static int buffersize = 1024; static Ipaddress ip = ipaddress.parse (host); static ipendpoint ipe = new ipendpoint (Ip, port); private static int count = 0;//= Dialog Number static void main (String[] args) { &nBsp; socket s = new socket (addressfamily.internetwork, sockettype.stream, PROTOCOLTYPE.TCP); try { s.connect (IPE); console.writeline ("successfully connected to {0}.") ", s.remoteendpoint); console.writeline ("Please enter the message you want to send to the server, press ENTER.) "); } catch (Exception ex) { & NBsP; debug.writeline (ex); throw; } thread clientth = new thread (Sendtoserver); Clientth.start (s); thread recth = new thread (recmsg); Recth.start (s); } private static void recmsg (Object o) { socket connsocket = o as socket; while (True) { byte[] buffer = new byte[buffersize]; try { int length = connsocket.receive (buffer); byte[] realbuffer = new byte[length]; &nbSp; array.copy (buffer, 0, realbuffer, 0, length); string str = system.text.encoding.default.getstring (Realbuffer); console.write ( "[{0}] ", count++); console.writeline ("{0}" said: {1}. ", connsocket.remoteendpoint, STR); } catch ( EXCEPTION&NBSP;EX) { Console.WriteLine (ex); console.readkey (); break; } } } private static void sendtoserver (Object o) { Socket socket = o as Socket; &nbsP; while (True) { try { string str = Console.ReadLine (); if (str != string. Empty) { &Nbsp; byte[] bt = encoding.default.getbytes (str); socket. Send (BT,&NBSP;BT. length, 0); } } catch (Exception ex) { console.writeline ("Send failed. "); &nbSp; debug.writeline (ex); throw; } } } }
C # Socket communication implements chat between two consoles