Question

I am working with a TCP client to learn how to use the TCPSocket function in VS2010 C#. I can call the read() function to read the data. That part all works. What I am not understanding is how to set the client up to listen to the stream and post the incoming data to a text box without calling the function manually or using a timer. I would like to have this handled with an event handler, but at this point I have just completely confused myself and now I need some guidance.

I am using a client sample I found on MSDN to help me understand how the function works to do a basic read.

private static void Receive(Socket client) {
    try {
        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = client;

        // Begin receiving the data from the remote device.
        client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReceiveCallback), state);
    } catch (Exception e) {
        Console.WriteLine(e.ToString());
    }
}

On an other stackoverflow post I found Matt Davis provided an example of using a public event

public event EventHandler<DataRecivedEventArgs> DataRecieved;

However when I tried it, the "DataRecivedEventArgs" is not a recognized function of visual studio.

Can someone help explain to me how to use TCPclient to consistently listen for data and call a function when some data is received?

Était-ce utile?

La solution

For a given TCP connection, received data is buffered in the Kernel up to its buffer size limit (which is opaque to the application).

When an application wants to receive data it has to explicitly tell the Kernel how much data needs to be copied to the application buffer, because the buffer size is not infinite and that Kernel may be storing incoming payloads faster than your application could handle (don't forget your application is regularly preempted).

The only way for your application to receive data from a TCP socket is through recv(), recvfrom(), recvmsg() system calls.

In your case, in User Space, all you can do is call the functions that correspond to those syscalls so you can receive data. Delivery is on-demand by design. In addition, the application won't know if data has arrived until it calls recv(), recvfrom(), recvmsg(), select(), poll() or epoll().

Note: I am not a C# person. I know C and Kernel internals and that's pretty much it. I just wanted to point out the concept behind socket communcation.

Autres conseils

My answer is very likely too late for you. But since I recently had the same requirement, I have now published a nuget package for it. It provides an event that is triggered on incoming data. So you only have to subscribe to it.

PM> install-package Nager.TcpClient

https://github.com/nager/Nager.TcpClient

void OnDataReceived(byte[] receivedData)
{
}

using var cancellationTokenSource = new CancellationTokenSource(1000);

using var tcpClient = new TcpClient();
tcpClient.DataReceived += OnDataReceived;
await tcpClient.ConnectAsync("tcpbin.com", 4242, cancellationTokenSource.Token);
await tcpClient.SendAsync(new byte[] { 0x01, 0x0A });
await Task.Delay(400);
tcpClient.Disconnect();
tcpClient.DataReceived -= OnDataReceived;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top