0

我正在学习如何使用异步 tcp 套接字服务器/客户端

无法弄清楚为什么我的服务器没有继续监听客户端发送的数据...消息框只出现一次,然后我需要重新打开服务器

这是我的代码(客户端)

public partial class Form1 : Form
{
    private Socket client;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client.BeginConnect(new IPEndPoint(IPAddress.Loopback, 8888), new AsyncCallback(ConnectCallback), null);
        }
        catch
        {

        }
    }

    private void ConnectCallback(IAsyncResult AR)
    {
        try
        {
            client.EndConnect(AR);      
        }
        catch
        {

        }
    }

    private void SendCallback(IAsyncResult AR)
    {
        try
        {
            client.EndSend(AR);
        }
        catch
        {

        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        byte[] buffer = Encoding.ASCII.GetBytes("Hello World!");
        client.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallback), null);
    }
}

服务器

private Socket sck, sckc;
    private byte[] buffer;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sck.Bind(new IPEndPoint(IPAddress.Any, 8888));
            sck.Listen(0);
            sck.BeginAccept(new AsyncCallback(AcceptCallback), null);
        }
        catch
        {

        }
    }

    private void AcceptCallback(IAsyncResult AR)
    {
        try
        {
            while (true)
            {
                sckc = sck.EndAccept(AR);
                buffer = new byte[sckc.ReceiveBufferSize];
                sckc.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
            }
        }
        catch
        {

        }
    }

    private void ReceiveCallback(IAsyncResult AR)
    {
        try
        {
            string text = Encoding.ASCII.GetString(buffer);
            MessageBox.Show(text);
        }
        catch
        {

        }
    }
4

1 回答 1

1

摆脱 while 循环。在接收回调中,您必须再次调用 BeginReceive() 以便侦听下一组数据包:

    private void AcceptCallback(IAsyncResult AR)
    {
        try
        {
            sckc = sck.EndAccept(AR);
            buffer = new byte[sckc.ReceiveBufferSize];
            sckc.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
        }
        catch
        {

        }
    }

    private void ReceiveCallback(IAsyncResult AR)
    {
        try
        {
            string text = Encoding.ASCII.GetString(buffer);
            MessageBox.Show(text);

            sckc.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
        }
        catch
        {

        }
    }

如果您打算拥有多个连接,那么您需要为每个连接维护一个单独的缓冲区

于 2013-06-24T00:40:35.793 回答