断开后重新连接c#插座
我已经创建了一个异步客户端,如果我尝试在断开套接字的同一会话中连接到服务器,我会收到以下异常:断开后重新连接c#插座,c#,sockets,C#,Sockets,我已经创建了一个异步客户端,如果我尝试在断开套接字的同一会话中连接到服务器,我会收到以下异常: System.Net.Sockets.SocketException: Socket is not connected and address is not provided when sending on a datagram socket using a sendto call. Request to send or receive data canceled. at System.Net.Soc
System.Net.Sockets.SocketException: Socket is not connected and address is not provided when sending on a datagram socket using a sendto call. Request to send or receive data canceled.
at System.Net.Sockets.Socket.BeginSend (System.Byte[] buffer, Int32 offset, Int32 size, SocketFlags socket_flags, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0
at AsynchronousClient.Sendconnection (System.Net.Sockets.Socket client, System.String data, System.String data2) [0x00016] in C:\Users\Alessio\Documents\New Unity Project 1\Assets\Standard Assets\Scripts\AsynchronousClient.cs:183
at AsynchronousClient.StartClient (System.Net.Sockets.Socket[] client, System.String email, System.String password) [0x00051] in C:\Users\Alessio\Documents\New Unity Project 1\Assets\Standard Assets\Scripts\AsynchronousClient.cs:82
好久没见了,但记得我有个问题。我不知道您连接的是哪种服务器,但在internet box上,我知道断开的端口会关闭几分钟,并且在几分钟内无法再次使用(其安全性)。因此,请检查您的逻辑,以便您的soket可以在一系列端口上进行尝试?“Socket未连接”-因此您需要再次连接。您不能在(TCP)之后重新打开套接字交易已经完成。套接字由使用的本地IP地址、本地端口和远程套接字组成。通过关闭TCP连接,远程套接字将与本地套接字一样关闭。因此,您不能使用关闭的套接字(远程套接字是关闭的,因此即使您成功地重新打开了端,远程机器也会丢弃您的包)。您需要重新建立连接或不关闭TCP连接。
public static String StartClient(Socket[] client,string email,string password)
{
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "albacremisi.no-ip.org".
IPHostEntry ipHostInfo = Dns.GetHostEntry("127.0.0.1");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
client[0] = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
/*if(!PingHost("127.0.0.1")){
return "Server unreachable";
}*/
client[0].BeginConnect(remoteEP,new AsyncCallback(ConnectCallback), client[0]);
connectDone.WaitOne();
int x = 0;
do
{
Sendconnection(client[0], email, password);
sendDone.WaitOne();
Thread.Sleep(100);
Receive(client[0]);
receiveDone.WaitOne();
x++;
} while (response != "AU+" && x < 3);
x=0;
return response;
}
catch (Exception e)
{
//Console.WriteLine(e.ToString());
return e.ToString();
}
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
//Console.WriteLine(e.ToString());
}
}
private static void Sendconnection(Socket client, String data, String data2)
{
// Convert the string data to byte data using ASCII encoding.
//byte[] byteData = Encoding.ASCII.GetBytes(data);
char [] user = data.ToCharArray();
char [] pass = data2.ToCharArray();
byte[] byteData = SendProtocols.AUTHENTICATION_RESPONSE(user,pass);
//try{
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0,new AsyncCallback(SendCallback), client);
/*}catch(Exception e){
Debug.Log(e.ToString());
}*/
}
Senddisc(client [0]);
sendDone.WaitOne();
client[0].close();
client=new Socket[1];