Home > Net >  C # Socket communication problems
C # Socket communication problems

Time:02-24

I wrote a server can connect multiple clients, the question now is when more than one client connection, each client the first communication no problem, but the second communication if only the first connection to be successful communication, and when I shut down the server's button, do not close the connection, disconnect one or two, also please teach teach great god!!!!!
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 us

CodePudding 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:

reference datafansbj reply: 3/f
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 code,

Demand is that a server can connect n client, then you need to implement is a client sends information to the server is connected, the server then mass for each connection client, now I tested TCP& DUP test tool so there is no client code is when connecting multiple clients will appear after the first client can connect other can only conduct a communication has been communication, when the other can't communication server can the client receives the first connection information but not sent out

CodePudding user response:

What you need is to require the TCP two-way communication, that is, the server and the client's role in change at any time, this scenario USES a TCP session to do, you must use the asynchronous way, because you need to deal with sending and receiving of parallel operations, synchronization mode, the receiver is blocked, when receiving waiting to send operation,

If the network is small (all the devices on the same network segment), consider using UDP multicast, but the result is not reliable,
  •  Tags:  
  • C#
  • Related