从套接字读取数据时出现问题

时间:2018-10-31 08:10:12

标签: c# asyncsocket

我正在尝试将数据发送到服务器并在另一端接收它,但似乎无法传输数据 这是我的客户端代码:

 public void SendMessage(string MessageToSend)
        {
            MessageToSend += "#-*!!|#";
            byte[] buffer = Encoding.ASCII.GetBytes(MessageToSend);
        clientSocket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallback), clientSocket);

    }

    private void SendCallback(IAsyncResult ar)
    {
        Socket client = (Socket)ar.AsyncState;
        int bytesSent = client.EndSend(ar);
        Console.WriteLine("Sent {0} bytes to server.", bytesSent);
    }

这是我的服务器

 private void ReceiveCallBack(IAsyncResult ar)
        {
            StateObject state = (StateObject)ar.AsyncState;

            int received = state.workSocket.EndReceive(ar);

            if (received > 0)
            {
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, received));
                content = state.sb.ToString();
                Console.WriteLine("RECEIVED  : " + Encoding.UTF8.GetString(state.buffer, 0, received));
                if (content.IndexOf("#-*!!|#") != -1)
                {                        StaticVariables.ReqHandler.Requests.Add(content.Substring(0, content.Length - 7));
                    state.workSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallBack), state);

                    state.sb.Clear();
                    return;
                }
                else
                {
                    state.workSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallBack), state);
                }
            }

        }

问题是,如果收到的消息已完成,我将永远无法进入if块。有人可以告诉我我哪里做错了吗?

更新:当我使用断点并检查“ content”变量时,我看到很多\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0并继续正常显示时为空,这可能是因为我缓冲区大小或有关编码的内容?

更新2:当我在此行上带有断点时,我才意识到

state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, received));

我的state.buffer完全为空。我将编写代码的开头以及如何调用“ ReceiveCallBack”。也许我在代码的前面部分做错了。

 public void StartServer()
        {
            serverSocket.Bind(new IPEndPoint(IPAddress.Any, port));
            serverSocket.Listen(10);
            serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), null);
        }

        private void AcceptCallBack(IAsyncResult ar)
        {
            Socket socket = serverSocket.EndAccept(ar);
            clientSockets.Add(socket);
            StateObject State = new StateObject();
            State.workSocket = socket;
            socket.BeginReceive(buffer, 0, buffer.Length, 0, new AsyncCallback(ReceiveCallBack), State);
            serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), null);
        }

这是我启动服务器的方式。我用它来呼叫StartServer

StaticVariables.SocketObject = new SocketConnection();
            Thread ServerThread = new Thread(() => StaticVariables.SocketObject.StartServer());
            ServerThread.Start();

2 个答案:

答案 0 :(得分:0)

客户端使用ASCII编码,在服务器端使用UTF8。您是否尝试过使用相同的编码?

答案 1 :(得分:0)

我只是想知道出了什么问题。在这一行:

socket.BeginReceive(buffer, 0, buffer.Length, 0, new AsyncCallback(ReceiveCallBack), State);

我正在使用名为“ buffer”的变量,这很正常。这意味着数据将进入此变量内。但是然后在ReceiveCallBack上,我正在使用

state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, received));

我没有填充state.buffer,所以我只是编码一个空字节数组,这就是为什么我无法检索数据的原因。我不敢相信我已经好几个小时都看不到了。

无论如何,我都这样改变了beginreceive:

socket.BeginReceive(State.buffer, 0, State.buffer.Length, 0, new AsyncCallback(ReceiveCallBack), State);

所以现在我可以将数据填充到State对象中,并在Callback上使用它。.

简而言之,请确保您在哪里填充输入数据:|