Raggaer
Raggaer

Reputation: 3318

Cant figure out why my server is not continue listening to data sent from the client

Im learning how to use Async tcp sockets server/client

Cant figure out why my server is not continue listening to data sent from the client... The messagebox just appear once, then I need to re-open the server

This is my code ( Client )

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

Server

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
        {

        }
    }

Upvotes: 0

Views: 115

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39152

Get rid of the while loop. In the receive callback, you have to call BeginReceive() again so you can listen for the next set of packets:

    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
        {

        }
    }

If you intend on having multiple connections then you need to maintain a separate buffer for each connection.

Upvotes: 1

Related Questions