无法弄清楚为什么我的服务器不继续侦听客户端发送的数据

本文关键字:客户端 数据 继续 弄清楚 为什么 我的 服务器 | 更新日期: 2023-09-27 18:19:41

我正在学习如何使用异步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
        {
        }
    }

无法弄清楚为什么我的服务器不继续侦听客户端发送的数据

去掉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
        {
        }
    }

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