Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Threading problem

Reply
 
Old 03-22-2009   #1 (permalink)
Eager2008


 
 

Threading problem

I have a device which can talk TCP. It can be sut up both as client and
server simultaneously.
If it is set up as a client, it will send some data when it wants to.
If it is set up as a server, it will respond to messages sent from the
Windows PC.

In my program I set up a thread for endless accepting incoming connections
(this is to handle the client mode of the device):

TcpListener tcpListener = new TcpListener(IPAddress.Any, listenerPortNo);
tcpListener.Start();
while (true)
{
TcpClient remoteTcpClient = tcpListener.AcceptTcpClient();
:
:
remoteTcpClient .Close();
}

I also have a thread for polling the device:

While(true)
{
TcpClient localClient = new TcpClient(deviceAddress, devicePort);
Stream deviceStream = localClient .GetStream();
StreamWriter deviceWriter = new StreamWriter(deviceStream);
deviceWriter .Write("A string which results in a response");
deviceWriter .Flush();
StreamReader deviceReader = new StreamReader(deviceStream);
string response = deviceReader.ReadLine();
tcpClient.Close();
}

The reason why the connections are not kept open is due to the device's
ability of multithreading (??), i.e. it can only handle one connection at a
time.
(And this makes the whole thing a lot less efficient, connections are
"expensive").
The PC software has no control on the timing for when incoming messages from
the device (remoteClient) are received and it shows up that sometimes
the polling thread throws an exception saying that the connection cannot be
made because the target refused it. I think this happens because the
remoteClient are working (a message has been received, i.e. the remoteClient
has been created, but not closed yet.

How can I synchronize these threads to make it safe ?
(Of course I may catch exceptions on the polling and re-try a little later,
but it is not optimal)




My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Threading Disappeared... Live Mail
Threading problem .NET General
WPF: threading problem .NET General
E-mail: threading Live Mail
Strict Threading Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46