Implemented, LAN client-to-client communication. This is actually a client-side service.
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;namespaceAnalog customer service end 01{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); Textbox.checkforillegalcrossthreadcalls=false;//Prohibit multithreading check} Socket socket_my; //actively establish a connection socketSocket Socket_listen;//Listening socketsSocket socket_accept;//Receive socketsThread thread_receive;//Receive ThreadThread thread_accept;//Send Thread Public voidAccept () { while(true) {socket_accept= Socket_listen.accept ();//Listen socket receive request return receive socketTextboxmag.appendtext ("Client Connection succeeded! "+ Socket_accept.remoteendpoint +"\ r \ n"); Thread_accept=NewThread (Receive); Thread_accept.isbackground=true; Thread_accept.start (); } } Public voidReceive () { while(true) { Try { byte[] B =New byte[1024x768]; Socket_accept.receive (B);//receive socket receive information stringMessage = Encoding.UTF8.GetString (B,0, b.length); if(!string. IsNullOrEmpty (message)) {Textboxmag.appendtext ( message+"\ r \ n"); } } Catch(Exception) {Textboxmag.appendtext ("the connection is off."+"\ r \ n"); Btn_conncet.enabled=false; Break; } } } Private voidStartlisten_click (Objectsender, EventArgs e) {Socket_listen=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Socket_listen.bind (NewIPEndPoint (Ipaddress.any,int. Parse (Textboxmyport.text)); Socket_listen.listen (Ten); Textboxmag.appendtext ("Start listening ."+"\ r \ n"); Thread_receive=NewThread (Accept); Thread_receive.isbackground=true; Thread_receive.start (); Startlisten.enabled=false; } Private voidBtn_conncet_click (Objectsender, EventArgs e) { Try{socket_my=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Socket_my.connect (NewIPEndPoint (Ipaddress.parse (Textboxip.text),int. Parse (Textboxport.text)); Btn_conncet.enabled=false; } Catch(Exception ex) {Textboxmag.appendtext ("Connection not on"+"\ r \ n"); } } Private voidButton_send_click (Objectsender, EventArgs e) { Try{socket_my.send (Encoding.UTF8.GetBytes (Textboxsend.text+"\ r \ n")); } Catch(Exception) {Textboxmag.appendtext ("Send failed"+"\ r \ n"); } } }}
Here is the port number, and the IP, next, do a service-side record it said that there is a client's IP communication port is good.
Socket Learning (iii). 1