0

我目前正在研究异步 TCP 程序,但遇到以下问题:

  • - 当服务器发送一些流时,客户端首先接收一个空缓冲区,然后它接收流。
  • - 在第一个流发送之后,如果我正在发送另一个流,则客户端的缓冲区包含发送的第一个流和当前流。
  • - 如果我正在发送另一个流,缓冲区将包含我之前发送的流和当前的流......等等......

这真的很烦人,我不知道问题出在哪里。这是我的客户代码:

Socket CLIENT;
byte[] _buffer;

private void button1_Click(object sender, EventArgs e)
{
    Start();
}
void Start()
{
    CLIENT = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    CLIENT.BeginConnect(IPAddress.Parse("the IPaddress"), 1234, new AsyncCallback(ConnectionCallBack), null);
}
private void ConnectionCallBack(IAsyncResult ar)
{
    try
    {
         CLIENT.EndConnect(ar);
         _buffer = new byte[CLIENT.ReceiveBufferSize];
         CLIENT.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), null);
    }
    catch (Exception Er)
    {
        MessageBox.Show("[[[[ERROR:1]]]]=>" + Er.Message);
    }

}
private void ReceiveCallBack(IAsyncResult ar)
{
    try
    {
        int lenght = CLIENT.EndReceive(ar);
        if (lenght == 0)  { return; }
        Array.Resize(ref _buffer, lenght);
        string stream = ASCIIEncoding.ASCII.GetString(_buffer);
        Array.Resize(ref _buffer, CLIENT.ReceiveBufferSize);

        CLIENT.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), null);

    }
    catch (Exception ex) { MessageBox.Show("[[[[hoERROR:2]]]]=>" + ex.Message); }
}

这是服务器:

private void button2_Click(object sender, EventArgs e)
        { 
            try
            {
                Socket socket = (Socket)listW.SelectedItems[0].Tag; //contains the socket
                socket.BeginSend(_buffer,0,_buffer.Length,SocketFlags.None,new AsyncCallback(SendingCallBack),socket);
                ServerSocket.BeginAccept(new AsyncCallback(AcceptCallBack),null);
            }
            catch (Exception er) { MessageBox.Show("[[[[ERROR:]]]]=>" + er.ToString()); }
        }
        private void SendingCallBack(IAsyncResult ar)
        {
            try
            {
                Socket socket = (Socket)ar.AsyncState;
                _buffer = ASCIIEncoding.ASCII.GetBytes(tx1.Text);
                socket.BeginSend(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(FinalCallBack),socket);
            }
            catch (Exception er) { MessageBox.Show("[[[[ERROR:3]]]]=>" + er.ToString()); }
        }
        private void Final(IAsyncResult ar)
        {
            Socket client = ar.AsyncState as Socket;
            client.EndSend(ar);
        }
4

1 回答 1

0

I think you have a few problems. One seems to be the stream in ReceiveCallback. You get a string from the buffer then do nothing with it. Resizing your buffer down then resizing if back up to the original size seems like a waste. Might was well make a copy of the data then call GetString and you can leave your buffer alone.

In your server, you call BeginSend to send _buffer, when that completes you call BeginSend again in SendingCallback with the binary representation of tx1.Text but you don't call EndSend, when that completes you call EndSend on the 2nd BeginSend; but still haven't called it for the 1st.

于 2013-04-07T20:39:53.437 回答