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?