473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

TCPClient Thread

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

Sep 16 '08 #1
1 3586
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.

"Michel Racicot" <mr******@hotmail.comwrote in message
news:ua**************@TK2MSFTNGP02.phx.gbl...
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


Sep 18 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Abubakar | last post by:
hi, I work on a computer that is part of a network and uses proxy to connect to net. I cant connect to servers outside my proxy with simple ConnectTo code. I need to know how to make my requests go...
5
by: Jeff Weber | last post by:
First, my question: Should I use Write or BeginWrite (sync or async) to stream data to my clients over a TCPClient connection. Details: On the server I have a custom circular data buffer that...
3
by: Danny Tuppeny | last post by:
Hi all, I'm trying to send a null character as a string delimiter through a TcpClient (code below). It's to connect to this poker bot room: http://games.cs.ualberta.ca/webgames/poker/bots.html...
5
by: Jim W | last post by:
I have an ASP.NET app that has very slow connect times using TCPClient, or the Socket class, or even calling a COM object that uses C socket calls on certain similar XP SP2 boxes. On those...
1
by: hamil | last post by:
I am having trouble using the TcpListener and TcpClient classes. At the end of this post is server code that runs, and a class whose purpose is described below. I need to know when the client...
7
by: david.topham | last post by:
Hi The code below demostrates an issue I'm having with with NetworkStream: using System; using System.Net.Sockets; namespace TCPCTest { class Class1
3
by: Ryan Liu | last post by:
Hi, I use Server: Use an endless thread to lisiten to clients requests: while(true) { TcpClient client = myListener.AcceptTcpClient();
2
by: craigkenisston | last post by:
I'm creating a TcpClient instance in an object which is created in a thread. I have a loop that goes about 6-7 cycles, creating the container object in each cycle and each object contains a...
1
by: WildBill | last post by:
I am using a tcpclient and a network stream to communicate with the TCP port on a printer for testing the printer firmware. The code I have works perfectly the first time I run it. The printer...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.