Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异步客户端套接字未接收数据_C#_Sockets - Fatal编程技术网

C# 异步客户端套接字未接收数据

C# 异步客户端套接字未接收数据,c#,sockets,C#,Sockets,我尝试用异步套接字通信实现我的应用程序。它完美地连接并发送了请求,但我没有从服务器(Java服务器)收到任何数据。插座连接是 client.BeginConnect(hostname, port, new AsyncCallback(ConnectCallback), client); connectDone.WaitOne(); private static void ConnectCallback(IAsyncResult ar) { try {

我尝试用异步套接字通信实现我的应用程序。它完美地连接并发送了请求,但我没有从服务器(Java服务器)收到任何数据。插座连接是

client.BeginConnect(hostname, port,
            new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();

  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);

        Console.WriteLine("Socket connected to {0}",
            client.RemoteEndPoint.ToString());

        // Signal that the connection has been made.
        connectDone.Set();
    } catch (Exception e) {
        Console.WriteLine(e.ToString());
    }
}

private static void Receive(Socket client)
{
    try
    {
        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = client;

        // Begin receiving the data from the remote device.
        client.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None,
            new AsyncCallback(ReceiveCallback), client);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}

private static void ReceiveCallback(IAsyncResult ar)
{
    try
    {
        // Retrieve the state object and the client socket 
        // from the asynchronous state object.
        StateObject state = (StateObject)ar.AsyncState;
        Socket client = state.workSocket;

        // Read data from the remote device.
        int bytesRead = client.EndReceive(ar);
        Console.WriteLine(response);
        if (bytesRead > 0)
        {
            // There might be more data, so store the data received so far.
            state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

            // Get the rest of the data.
            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReceiveCallback), state);
        }
        else
        {
            // All the data has arrived; put it in response.
            if (state.sb.Length > 1)
            {
                response = state.sb.ToString();
            }
            // Signal that all bytes have been received.
            receiveDone.Set();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}

如果有任何帮助,我们将不胜感激。

您的代码基本正常,但有一个严重的错误。在BeginReceive中,将客户机套接字实例作为状态对象。在ReceiveCallback函数中,将其强制转换为StateObject,而不是套接字。这将导致控制台出现异常

尽管如此,也会触发ReceiveCallback函数开头的断点。你试过这个吗?无论如何都应该抛出异常


在断点未触发的情况下,您应该独立检查是否确实从服务器发送了内容。当然,正如您所说的,所有这些都假设连接是有效的。

那么,
ConnectCallback
丢失了-另外:您如何知道您已连接?您是否调试了此代码(在
ReceiveCallback
?)中设置断点)-是否尝试通过telnet连接?当然,如果您开始接收数据,但数据还没有到达,您可能会看到
0
字节,并停止一切连接=true;此属性在调试模式下显示。我在receivecallback方法中设置了断点,但它没有执行。它一直在调试,直到beginreceive,然后从调试模式结束。我没有尝试过telnet。你也可以考虑检查一下。