Receive socket with connection left open

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • trussell
    New Member
    • Sep 2012
    • 1

    #1

    Receive socket with connection left open

    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.

    Code:
    Dim tcplistener_remote As New TcpListener(IPAddress.Any, ip_port_remote)
    
    Dim Main_Timer As Int16 = 10		' 10 milli-seconds
    Dim Keep_Alive_Timer As Int32 = 5000	' keep alive timer set to 5 seconds
    
    Dim counter As Int32 = 0
    
    
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
    	tcplistener_remote.Start()
    
    	Timer1.Interval = Main_Timer		' set the main timer interval
    	Timer1.Enabled = True			' start the timer
    
    	Timer2.Interval = Keep_Alive_Timer		' set up the timer
    	Timer2.Enabled = True			' and start the timer
    
    End Sub		 ' end of Form1_Load
    
    
    Public Sub ListenToEthernet()
    	If tcplistener_remote.Pending = True Then			 	' Check to see if there is data coming from a remote site
    
    		Dim socket1 = tcplistener_remote.AcceptSocket()		' set up a socket called 'socket1'
    
    		Dim k_val As Integer = 0
    		Dim num_bytes(socket1.ReceiveBufferSize) As Byte	' obtain the number of bytes in the incoming socket and then
    		'								move that number of bytes over to the array 'num_bytes'
    
    		Try
    			k_val = socket1.Receive(num_bytes)
    
    			Catch se As Exception
    				' if there is an exception do something
    			Exit Try
    		End Try
    
    		Dim returndata As String
    		returndata = Encoding.ASCII.GetString(num_bytes)
    
    		Dim array1(90) As Char
    		array1 = returndata.ToCharArray()
    
    		TextBox1.Text = array1
    
    		socket1.Close()	' close the socket!!!
    
    	End If
    
    End Sub	' end of ListenToEthernet
Working...