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

Receive socket with connection left open

P: 1
I'm working on an application in VB2010 Express. This application is a tcp connection between a PC and a remote IP device, displaying the data from the remote device's data packets in a text box.

My initial attempt is in the code snippet below, and it works - but... The remote device connection is through a network that has, between two routers, a 9600 baud modem phone line connection. Because of this very slow connection I need to reduce as much as possible the "extraneous" data packets. The way I want to do this is to initially make a network connection to the remote device, then leave the connection in place while the PC application is running, dropping the connection once the application is closed. This will remove the "extra" connect and dis-connect data packets sent over the network.

I've tried commenting out the "socket1.Close()" command but then only the first received data packet from the remote device is displayed in the text box. All subsequent data packets are not displayed, even though I can see that they are being sent from the remote device to the PC via Wireshark.

Any help or pointing in the right direction would be appreciated.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim tcplistener_remote As New TcpListener(IPAddress.Any, ip_port_remote)
  3.  
  4. Dim Main_Timer As Int16 = 10        ' 10 milli-seconds
  5. Dim Keep_Alive_Timer As Int32 = 5000    ' keep alive timer set to 5 seconds
  6.  
  7. Dim counter As Int32 = 0
  8.  
  9.  
  10. Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  11.  
  12.     tcplistener_remote.Start()
  13.  
  14.     Timer1.Interval = Main_Timer        ' set the main timer interval
  15.     Timer1.Enabled = True            ' start the timer
  16.  
  17.     Timer2.Interval = Keep_Alive_Timer        ' set up the timer
  18.     Timer2.Enabled = True            ' and start the timer
  19.  
  20. End Sub         ' end of Form1_Load
  21.  
  22.  
  23. Public Sub ListenToEthernet()
  24.     If tcplistener_remote.Pending = True Then                 ' Check to see if there is data coming from a remote site
  25.  
  26.         Dim socket1 = tcplistener_remote.AcceptSocket()        ' set up a socket called 'socket1'
  27.  
  28.         Dim k_val As Integer = 0
  29.         Dim num_bytes(socket1.ReceiveBufferSize) As Byte    ' obtain the number of bytes in the incoming socket and then
  30.         '                                move that number of bytes over to the array 'num_bytes'
  31.  
  32.         Try
  33.             k_val = socket1.Receive(num_bytes)
  34.  
  35.             Catch se As Exception
  36.                 ' if there is an exception do something
  37.             Exit Try
  38.         End Try
  39.  
  40.         Dim returndata As String
  41.         returndata = Encoding.ASCII.GetString(num_bytes)
  42.  
  43.         Dim array1(90) As Char
  44.         array1 = returndata.ToCharArray()
  45.  
  46.         TextBox1.Text = array1
  47.  
  48.         socket1.Close()    ' close the socket!!!
  49.  
  50.     End If
  51.  
  52. End Sub    ' end of ListenToEthernet
  53.  
  54.  
Sep 5 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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