this is to monitor the connection code snippet
private void Startsocket ()
{
Try
{
IPAddress IP=IPAddress. Any;
//when to start listening to, create a responsible for the server IP address and port number of the Socket
SocketWatch=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
IPEndPoint point=new IPEndPoint (IP, 9090);
//listen
SocketWatch. Bind (point);
//number of connections for 10
SocketWatch. Listen (10);
}
Catch {throw; }
}
this is a client connection processing pieces
public void ListenClickConnect (object o)
{
As the Socket Socket socketWatch="o.
//wait for users to connect to create a responsible for the communication Socket
While (true)
{
//maximum number of connections
Int number=10;
If (disSocket. Count & gt; Number)
{
SocketWatch. Close ();
SocketWatch. The Dispose ();
ShowMsg (" connection number has reached upper limit ");
}
Try
{
//close the server jump out of the loop when
If (stop)
{
break;
}
//wait until the client a new connection
SocketSend=socketWatch. The Accept ();
//store the remote client IP address to a generic collection
DisSocket. Add (socketSend. RemoteEndPoint. ToString (), socketSend);
Thread th=new Thread (Receive);
Th. IsBackground=true;
Th. Start (socketSend);
ShowMsg (socketSend. RemoteEndPoint. ToString () + ":" + "connection success");
//timer. The Start ();
AddKJ ();
}
Catch {break; }
}
this is shut down the server code snippet
bool stop=false;
Private void Stop_Click (object sender, RoutedEventArgs e)
{
for (int i=0; I & lt;=namenumber; I++)
{
Image images=gridcontrol. FindName (" Image "+ I) as the Image;
The Label Label=gridcontrol. FindName (" Label "+ I) as the Label;
If (images!=null)
{
Gridcontrol. Children. Remove (images);
Gridcontrol. UnregisterName (" image "+ I);
Gridcontrol. Children. Remove (label);
Gridcontrol. UnregisterName (" label "+ I);
}
Image Image=gridcontrol. FindName (" Image ") as the Image;
Image. The Visibility=Visibility. Visible;
}
ShowMsg (" closing services ");
If (startandstop==false)
{
Startandstop=true;
Startbut. IsEnabled=true;
Stopbut. IsEnabled=false;
Startbut. Background=new SolidColorBrush ((Color) ColorConverter. ConvertFromString (" # FF5B93E4 "));
Stopbut. Background=new SolidColorBrush ((Color) ColorConverter. ConvertFromString (" # FFF7CDCD "));
}
//disconnect communication and close monitor
If (socketSend!=null & amp; & SocketSend. Connected)
{
SocketSend. Shutdown (SocketShutdown. Both);
SocketSend. Close ();
SocketSend. The Dispose ();
Stop=true;
SocketWatch. Close ();
SocketWatch. The Dispose ();
DisSocket. The Clear ();
}
The else
{
Stop=true;
SocketWatch. Close ();
SocketWatch. The Dispose ();
}
}
received from the client pieces of information
int erro=0;
Private void the Receive (object o)
{
As the Socket socketSend="o.
While (true)
{
Try
{
//after the success of the client connections from the server receives the message
Byte [] buffer=new byte [1024 * 1024 * 2];
//actually receiving effective bytes
Int r=socketSend. The Receive (buffer);
If (r==0)
{
Erro +=1;
Erroe (erro);
break;
}
String STR=Encoding. UTF8. Get string (buffer, 0, r);
ShowMsg (STR);
SendData publishes the event (STR);
}
Catch
{
break;
}
}
}
this is running interface
CodePudding user response:
No one reply usCodePudding user response:
Find a socket asynchronous communication sample code to digest, listening, Accept and Receive should use multi-thread asynchronous processing, different socket to work colleagues, he didn't seem to put any code, all don't watch carefully, 8 into a socket obstruction caused by multiple I had not done,CodePudding user response:
Didn't quite understand what you mean, the post code is not complete, only the server-side code, no client code, the code from the server is not all, can you demand said understand first, and then analysis the code,CodePudding user response: