C # socket server how to put the client 1 to send the message to a client 2
CodePudding user response:
Client if both within their own Intranet, so need a server, the server deployment on the public network, two client connected to the first server, maintaining connections, and then forward
CodePudding user response:
Are in a local area network (LAN), but this is don't know how to forward
CodePudding user response:
refer to the second floor qq_42108220 response: are within a local area network (LAN), but this is don't know how to forward Is 1 to send a message to the client, such as by 16 and distributing 02 01, 02, 03 04 05 first 02 said client number also is to the client 2 server receives this information to check the first one is 02 2 here just to send this message to the client message format to see how do you decide to let the server recognized to which the client is OK CodePudding user response:
Is one of my client hardware esp8266 this is directly send information is not specified, the specified need server specified sent to another server CodePudding user response:
Learn about HPSocket CodePudding user response:
UDP approach! CodePudding user response:
Don't know this, before the class teacher speak with socket communication CodePudding user response:
Using System; Using System. Collections. Generic; Using System. Linq; Using System. The Text; Using System. The Threading; Using System.Net.Sockets; Using System.Net; The namespace SocketServerAcceptMultipleClient { Public class SocketServer { //create a socket and client communication The static Socket socketwatch=null; //define a collection, storage client information The static Dictionary ClientConnectionItems=new Dictionary {}; Public static void Main (string [] args) { //define a socket listens for messages from the client, consists of three parameters (IP4 addressing agreement, streaming connection, Tcp) Socketwatch=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp); //the server sends a message to an IP address and port number IPAddress address=IPAddress. Parse (" 127.0.0.1 "); //to bind the IP address and port number to the network node point IPEndPoint point=new IPEndPoint (address, 7777); //this port is dedicated to the listening //monitored binding network node Socketwatch. Bind (point); //limit socket listening to the queue length of 20 Socketwatch. Listen (20); //is responsible for monitoring the client thread: create a listener thread Thread threadwatch=new Thread (watchconnecting); //sets the thread form to synchronize with the background, as the main thread ends and end Threadwatch. IsBackground=true; //start the thread Threadwatch. Start (); Console. WriteLine (" open listening,,, "); Console. WriteLine (" click to enter any data enter exit procedures,,, "); The Console. ReadKey (); Console. WriteLine (" exit to monitor, and close the program, "); } //to monitor the client request message from The static void watchconnecting () { The Socket connection=null; //continued to monitor the client request message from While (true) { Try { The connection=socketwatch. The Accept (); } The catch (Exception ex) { //hint socket listening abnormal Console. WriteLine (ex. Message); break; } //get the client's IP and port number IPAddress clientIP=(connection. RemoteEndPoint as IPEndPoint.) Address. Int clientPort=(connection. RemoteEndPoint as IPEndPoint). The Port; //let the customer show "connected" to the success of information String sendmsg="connect server success! \ r \ n "+" local IP: "local port + clientIP +", "+ clientPort. ToString (); Byte [] arrSendMsg=Encoding. UTF8. GetBytes (sendmsg); Connection. The Send (arrSendMsg); No.//client network node String remoteEndPoint=connection. RemoteEndPoint. ToString ();//IP and port //display and client connection Console. WriteLine (" success and "+ remoteEndPoint +" client connection! \ t \ n "); //add the client information ClientConnectionItems. Add (remoteEndPoint, connection); //IPEndPoint netpoint=new IPEndPoint (clientIP, clientPort); IPEndPoint netpoint=connection. RemoteEndPoint as IPEndPoint; //create a communication thread ParameterizedThreadStart pts1=new ParameterizedThreadStart recv (); //ParameterizedThreadStart pts2=new ParameterizedThreadStart (send); Thread thread1=new Thread (pts1); //Thread thread2=new Thread (pts2); //set to background threads, as the main thread to exit out Thread1. IsBackground=true; //start the thread //thread2 IsBackground=true; //start the thread //thread2. Start (connection); Thread1. Start (connection); } } The static string messag=null; ///& lt; Summary> ///receiving information from the client, the client socket object ///& lt;/summary> ///& lt; Param name="socketclientpara & gt;" The static void recv (object socketclientpara) { The Socket socketServer=socketclientpara as Socket; While (true) { //create a memory buffer, the size of 1024 bytes Byte [] arrServerRecMsg=new byte [1024]. //will receive the information into the memory buffer, and returns the length of the byte array Try { Int length=socketServer. The Receive (arrServerRecMsg); //transfer machine received byte array is string can read String strSRecMsg=Encoding. The ASCII. Get string (arrServerRecMsg, 0, length); //will send information about the string attached to the text box txtMsg Console. WriteLine (" the client: "+ socketServer. RemoteEndPoint +", the time: "+ GetCurrentTime () +" \ r \ n "+ strSRecMsg +" \ r \ n \ n "); Console. WriteLine (" * * * * * * * * * * * * * * * * * * * * * * "+ strSRecMsg +" \ r \ n \ n "); SocketServer. Send (arrServerRecMsg, length, 0). // } The catch (Exception ex) { ClientConnectionItems. Remove (socketServer. RemoteEndPoint. ToString ()); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull