Home > Software engineering >  Socket asynchronous communication problems
Socket asynchronous communication problems

Time:10-08

 


Public class StateObject {
Public Socket workSocket=null;
Public const int BufferSize=1024;
Public byte [] buffer=new byte [BufferSize];
Public StringBuilder sb=new StringBuilder ();
}

Public class AsynchronousSocketListener {

Public static string data=https://bbs.csdn.net/topics/null;
Public static ManualResetEvent allDone=new ManualResetEvent (false);

Public AsynchronousSocketListener () {
}

Public static void StartListening () {
Byte [] bytes=new byte [1024].

IPHostEntry ipHostInfo=Dns. Resolve (Dns) GetHostName ());
IPAddress IPAddress=ipHostInfo. Expressions such as AddressList [0];
IPEndPoint localEndPoint=new IPEndPoint (ipAddress, 11000);

Socket listener=new Socket (AddressFamily InterNetwork,
SocketType Stream, ProtocolType. Tcp);

Try {
The listener. The Bind (localEndPoint);
Listener. Listen (100);

While (true) {
AllDone. Reset ();

Console. WriteLine (" Waiting for a connection... ");
Listener. BeginAccept (
New AsyncCallback (AcceptCallback),
The listener);

AllDone. WaitOne ();
}

} the catch (Exception e) {
Console. WriteLine (e. oString ());
}

Console. WriteLine (" \ nPress ENTER to continue... ");
The Console. The Read ();
}

Public static void AcceptCallback IAsyncResult (ar) {
AllDone. Set ();

The Socket listener=(Socket) ar. AsyncState;
The Socket handler=listener. EndAccept (ar);

StateObject state=new StateObject ();
State. WorkSocket=handler;
Handler. BeginReceive (state. The buffer, 0, StateObject. BufferSize, 0,
New AsyncCallback (ReadCallback), the state);
}

Public static void ReadCallback IAsyncResult (ar) {
The String content=String. The Empty;
StateObject state=(StateObject) ar. AsyncState;
The Socket handler=state. WorkSocket;

Int bytesRead=handler. EndReceive (ar);

If (bytesRead & gt; 0 {
State. Sb. Append (Encoding. The ASCII. Get string (
State. The buffer, 0, bytesRead));

The content=state. Sb. ToString ();
If (content. IndexOf (" & lt; EOF>" ) & gt; 1) {
Console. WriteLine (Read "{0} bytes from the socket. \ n Data: {1}",
Content. Length, content);
Send (handler, content);
} else {
Handler. BeginReceive (state. The buffer, 0, StateObject. BufferSize, 0,
New AsyncCallback (ReadCallback), the state);
}
}
}

Private static void the Send (Socket handler, String data) {
Byte [] byteData=https://bbs.csdn.net/topics/Encoding.ASCII.GetBytes (data);

Handler. BeginSend (byteData, 0, byteData Length, 0,
New AsyncCallback (SendCallback), handler);
}

Private static void SendCallback IAsyncResult (ar) {
Try {
The Socket handler=(Socket) ar. AsyncState;

Int bytesSent=handler. EndSend (ar);
Console. WriteLine (" Sent {0} bytes to the client. ", bytesSent);

Handler. Shutdown (SocketShutdown. Both);
Handler. The Close ();

} the catch (Exception e) {
Console. WriteLine (e. oString ());
}
}


Public static int Main (String [] args) {
StartListening ();
return 0;
}
}




Above is the MSDN about asynchronous SOCLET server-side code, my question is:
On the server after listening to a client connection, will create the user in the AcceptCallback function to deal with the client object, and specify the ReadCallback to handle receiving callback function,
So ReadCallback is thread-safe function functions in the process?? For example, if the first client connection came up, and is in ReadCallback processing, and within a short period of time will not end, at this time of the second client connection came up, so the second client processing is will wait until after the first has been completed into ReadCallback? Or directly into the ReadCallback also?? If the second client also enter ReadCallback for processing, then wash it will be the first client data??
Whether other callback function in c # processing mechanism is also start a new thread? That is calling the callback time, will start a new thread to handle the callback? But the callback is entrusted, in accordance with the truth, should not restart a thread, and the callback twice, then call should be unified in the same thread a callback,

This problem has puzzled me for a long time... please help to reassure,,,
Just on the BBS, near points is not much, please forgive me ~ ~ ~ ~

CodePudding user response:

Will, because at that time the data in the buffer, you didn't go to Receive the corresponding Socket, new thread does not have a chance,

CodePudding user response:

How to solve this problem?

CodePudding user response:

Callback function design for thread-safe, best not to global variables, object changes, had better use local variables, the thread local variable

CodePudding user response:

Like the DOWN a VSS, it's a pity that should be integral,
Have to give a free of charge!

CodePudding user response:

Clearly is VB.NET
  • Related