TCPClient Thread

  • Thread starter Thread starter Michel Racicot
  • Start date Start date
M

Michel Racicot

I'm quite new to TCP/IP programming, so feel free to tell me that I have the
wrong approach.

So far, I have a WinForm application that can be the client or the client
and server at the same time.

If the application is the server (and client at the same time), I have three
threads (plus my main WinForm thread): A TCPListener, a TCPServer and a
TCPClient connected to the server.

When the Server receive a message, it sends it to all connected client
(including the local client).

If the application is only a client, I have only one thread (plus my main
WinForm thread).

Some message that the TCP Client can receive might indicate that a Window
must be refreshed (example: a new client has been connected and we should
add his name in a listbox for example).

The problem is that since the TCP Client can receive stuff at any moment, I
made it a thread that verify DataAvailable and then read on the Network
Stream and deserialize it to an object. However, threads other than the
main WinForm thread can NOT call methods of WinForms.

One of the suggestion I had, was to put on queue all received messages and
then, on a timer installed on the WinForm thread, unqueue them and threat
them accordingly.

Is that the right thing to do? How is it done by the experts?

If I'm not clear enough, feel free to ask for more infos...

Thank you
 
I use vb.net and use a background thread,
(System.ComponentModel.BackgroundWorker), for my tcpip clients and servers.

The DoWork is where the listener is. A bActive loop is wrapped around the
listening code. Using the nature of the background thread, I can pop out of
the thread using ReportProgress. ReportProgress has two parms, first is a
integer and the second an object. Using the integer as a data-content type,
error happened, client event, server event, UI updates etc, you can report
on the status of your client/server.

All this code is contained in a class I call Server_Class.vb. Inside
ReportProgress, appropriate events are raised from a "Select Case
e.ProgressPercentage" block.
 
Back
Top