![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||