Home > Net >  SocketException: because the socket is not connected and (when using a sendto call send a datagram s
SocketException: because the socket is not connected and (when using a sendto call send a datagram s

Time:09-24

The server to the client when sending a message to the wrong:
Error in://asynchronous messaging request
Server_socket. BeginSend (sendData publishes the event, 0, sendData publishes the event Length, SocketFlags. None, the new System. AsyncCallback (SendToServer), server_socket);

But this is my send message according to the client code a little bit of change should not have wrong, best can not reason, hope leaders help me!!!!!!!!!

Post code:
 using UnityEngine; 
Using System. The Collections;
Using System.Net.Sockets;
Using System.Net;
Using System. The Text;

Public class ServerScript: MonoBehaviour
{
After the receipt of message//delegate callback
Public delegate void ReceiveCallBack (string content);
Private string receive_str;
The Socket server.
String MSG="";
//the server received the message storage
Byte [] ReceiveBuffer=new byte [1024].
After receiving the message callback to entrust//server
Private ReceiveCallBack callback.
Private Socket server_socket;

Private readonly object locker=new object ();
//Use this for initialization
Void the Start ()
{
//initialize the server
InitServer ((string content)=& gt; {
Receive_str=content;
});
}

//initialize the server information
Public void InitServer ReceiveCallBack (cb)
{
Enclosing the callback=cb;
//1.
Server_socket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
//2.
IPEndPoint endPoint=new IPEndPoint (IPAddress. Any, 5001);
//3.
Server_socket. Bind (endPoint);
//4.
Server_socket. Listen (10);
//5. Start asynchronous waiting for the client's request link
Server_socket. BeginAccept (new System. AsyncCallback (Accept), server_socket);

This callback (" open server "+ endPoint. The ToString ());
}

Void OnGUI ()
{
If (receive_str!=null)
{
GUILayout. Label (receive_str);
}
MSG=GUI. TextField (new the Rect (0, 0, 500, 40), MSG);
If (GUI. The Button (new the Rect (0, 50, 100, 30), the "Send"))
{
The SendMessage (MSG);
}
}
//to accept links to the client request the callback function after
Void the Accept (System. IAsyncResult ar)
{
//to get working Socket object
The Socket Socket=ar. AsyncState as Socket;
//store the information of asynchronous operations, as well as user-defined data
The Socket worker=Socket. EndAccept (ar);
SocketError error;
//start asynchronous receive client sends the message content
Worker. BeginReceive (ReceiveBuffer, 0, ReceiveBuffer. Length, SocketFlags. None, the new System. AsyncCallback (Receive), the worker);
//continue to asynchronous waiting for new client link request
Socket. BeginAccept (new System. AsyncCallback (Accept), socket);
}
//the server after receiving the client message of the callback function
Void the Receive (System. IAsyncResult ar)
{
//to get working Socket object
The Socket worker=ar. AsyncState as Socket;
Int ByteRead=0;
Try
{
ByteRead=worker. EndReceive (ar);
}
The catch (System. Exception ex)
{
Enclosing the callback (ex. ToString ());
}
If (ByteRead & gt; 0)
{
The string Content=Encoding. The Default. Get string (ReceiveBuffer);
This callback (Content);
}
//continue to asynchronous request waiting for the client sends the message
Worker. BeginReceive (ReceiveBuffer, 0, ReceiveBuffer. Length, SocketFlags. None, the new System. AsyncCallback (Receive), the worker);
}
Public void SendMessage (string message)
{
If (message==null)
return;

Message +="\ r \ n";
Byte [] sendData publishes the event=https://bbs.csdn.net/topics/Encoding.UTF8.GetBytes (message);
//asynchronous messaging request
Server_socket. BeginSend (sendData publishes the event, 0, sendData publishes the event Length, SocketFlags. None, the new System. AsyncCallback (SendToServer), server_socket);
}
//send a message over the callback function
Void SendToServer (System. IAsyncResult ar)
{
The Socket worker=ar. AsyncState as Socket;
Worker. EndSend (ar);
}
}

CodePudding user response:

CodePudding user response:

This is you write a TCP server, although in unity to the TCP server is don't know what to do

Error is the socket is not connected, that is, without the client connect to the server to

This time can't send, otherwise your data sent to who

CodePudding user response:

TieZhu if the client and the server's IP address wrong?
  •  Tags:  
  • C#
  • Related