断开连接后重新连接c#套接字

时间:2015-12-18 09:34:44

标签: c# sockets

我已经创建了一个异步客户端,如果我尝试在插槽的previus断开连接的同一会话中连接到服务器,我会收到此异常:

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 

创建连接我使用此代码:

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上我创建消息并将它们发送到服务器):

Senddisc(client [0]);
sendDone.WaitOne();
client[0].close();
client=new Socket[1];

0 个答案:

没有答案