Exec
Exec

Reputation: 407

I'm only receiving the first packet, why?

I searched for a working TCP library, found this one: https://codereview.stackexchange.com/questions/24758/tcp-async-socket-server-client-communication

I implemented the six refactored classes from @Jesse C. Slicer's answer, modified it to allow an arbitrary port number (Added an int Port parameter to the StartListening() method).

I'd opened a connection with telnet/PuTTY, yet I'm only receiving the first typed character/init information from PuTTY, and no more. The server does not stop, since I can open new connections and do receive each of those single messages.

I tried the following code (There's a bit problem with the Async part, it actually blocks the rest of the program, that's why I use BackgroundWorker):

private BackgroundWorker serverThread;
    private IAsyncSocketListener server;
    public Connection()
    {
        server = AsyncSocketListener.Instance;
        serverThread = new BackgroundWorker();
        serverThread.DoWork += ServerThread_DoWork;
        serverThread.RunWorkerAsync();
    }
    private void Server_OnReceived1(int id, string msg)
    {
        System.Diagnostics.Debug.WriteLine($"String: {msg}");
        System.Diagnostics.Debug.WriteLine($"Bytes: {string.Join(", ",msg.Select(x=>(int)x))}");
    }

    private void ServerThread_DoWork(object sender, DoWorkEventArgs e)
    {
        server.MessageReceived += Server_OnReceived1;
        server.StartListening(1093);
    }

Upvotes: 0

Views: 139

Answers (1)

In your callback (Server_OnReceived1), if you are waiting more data you should call again BeginReceive. If you don't do that your callback will not be called (beacause you have not notified that you are ready for more data) although there is data for it.

In the code of the link you provide, something like:

state.listener.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);

Upvotes: 1

Related Questions