By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,868 Members | 1,974 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,868 IT Pros & Developers. It's quick & easy.

Sockets between two remote machines

P: 4
I hope this is simple enough. I have written a very simple little app that runs on a windows 2008 server machine. It simply listens for connections on a specific port. It then accepts any connection to this port and begins processing the data based on the messages. All of this works locally. I.e. when on the same machine using

The problem is that when I test from two remote machines the socket seems to drop (close) on its own. I am not sure if this is a server 2008 config issue or something else. The socket connects to the little app on the server and identifies itself. data is shared between the two connections. But after 10 seconds I do a ping and the socket is closed. (the ping is an internal process of the app, this ping works locally). I need the connection between the two machines to be continuous.

My hunch is this might be a firewall issue but not sure what to look for.

So to summarize - the app, when local, works. Does as expected.
The app when remote, does accept connections, does process the information and does send the initial data back to the client (which is the socket id).
The problem is that i use a time limited Read on the server (10 seconds). when no data is rx in 10 sec I send a ping to the connected PC to see if still alive. It should be. But is not. So it dropped somewhere in that 10 second window.
Apr 5 '13 #1
Share this Question
Share on Google+
3 Replies

P: 4
btw: this is using visual studio 2010. I was just wondering if anyone know of any extra setup on the server side to keep the socket from closing when using the read timeout method.. it appears this method may not be stable and can leave the socket in an unusable state. SO this could be the issue.
Apr 6 '13 #2

P: 4
here is the code:
Expand|Select|Wrap|Line Numbers
  1. again:
  2.         ns = sck.GetStream
  3.         ns.ReadTimeout = 5000
  4.         ns.InitializeLifetimeService()
  6.         Try
  7.             bytesread = ns.Read(readBuffer, 0, 255) <--- when 5 seconds is up this caused an exception.  But socket ns is also now closed because of this on windows 2008 server.  Not on windows 7 home.
  8.             Console.WriteLine("Bytes Read:" & bytesread)
  11.             If bytesread > 0 Then
  12.                 data = System.Text.Encoding.UTF8.GetString(readBuffer, 0, bytesread)
  13.                 message &= data
  14.                 Do While message.Contains("$")
  15.                     pos = message.IndexOf("$")
  16.                     If pos > -1 Then
  17.                         cmd = Mid(message, 1, pos)
  18.                         message = Mid(message, pos + 2)
  19.                         If ProcessCommand(sck, cmd) = False Then
  20.                             Console.WriteLine("Process Command returned FALSE on Command:" & cmd)
  22.                             'If isPrinterSCK Then PrintSckt = Nothing 'no longer a Printer PC connected
  23.                             sck.Close()
  24.                             totalconns -= 1
  25.                             Console.WriteLine("SOCKET HANDLE: " & sck.Client.Handle.ToInt32.ToString & " Has Disconnected" & "  Total Active Sockets[" & totalconns & "]")
  26.                             Exit Sub 'kill thread
  27.                         End If
  29.                     End If
  30.                 Loop
  31.             End If
  32.             GoTo again
  33.   Catch ex As Exception
  34.             Dim m As String = ex.Message
  36.             'time out occured
  37.             'check ex value here.
  38.             'if timout occured on read on PC Print Client then send PING command to see if connection still open.  If so resume wait.
  39.             ' Console.WriteLine("Exception occured.  PrintSckt Value:" & IsNothing(PrintSckt))
  40.             ' Console.WriteLine("Exception contain TIME:" & m.ToLower.Contains("time"))
  42.             If IsNothing(PrintSckt) = False Then
  43.                 '    Console.WriteLine("Value of PrintSckt: " & PrintSckt.Client.Handle.ToString & "  sck: " & sck.Client.Handle.ToString)
  46.                 If PrintSckt.Client.Handle = sck.Client.Handle And m.ToLower.Contains("time") Then
  47.                     Console.WriteLine("Socket Connection Status:" & sck.Connected)
  48.                     If SendSckMessage(sck, "PING#$") Then
  49.                         ' Console.WriteLine("Ping sent to Printer Client")
  50.                         GoTo again 'are we the Printer PC Connection?  if so we don't die until we are told to.  no time out here.
  51.                     End If
  52.                 Else
  53.                     Console.WriteLine("PING attempt failed to Printer Client")
  55.                     If IsNothing(Err) = False Then Console.WriteLine("Unexpexted error[" & Err.Number.ToString & "] :" & Err.GetException.ToString)
  56.                 End If
  57.             End If
  59.             If IsNothing(ex) = False Then Console.WriteLine("Application error:" & ex.Message)
  61.             totalconns -= 1
  62.             Console.WriteLine("SOCKET HANDLE: " & sck.Client.Handle.ToInt32.ToString & " Has Disconnected via Exception" & "  Total Active Sockets[" & totalconns & "]")
  63.         End Try
  65.         If PrintSckt.Client.Handle = sck.Client.Handle Then PrintSckt = Nothing : printsckid = ""
  66.         Try
  67.             sck.Close()
  68.         Catch ex As Exception
  70.         End Try
Apr 7 '13 #3

P: 4
Line #7 of the above code is the timed read that is in question. when it times out an exception is thrown. On line 48 we send a ping to the connected PC. To let it know we are still alive. This is where the error occurs. The routine that runs this states the socket is now closed. When I run this same code on a windows 7 box with both client/server on same machine the error never occurs. My thought now is to get rid of the timed read all together and have the client ping to let the server know it is still there. I have my reasons for doing it this way. It appears that the block read will exception out when a socket closes.
Apr 7 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.