Results 1 to 6 of 6

Thread: TCP client

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    4

    TCP client

    I am communicating with a commercially made remote device( a cellular 3G digi WAN) configured as a server.
    My PC runs XP and the server sw is written in VB.net 2003. A code snippet is as follows:
    dim tcpclient as new tcpclient()
    tcpclient.connect ("abcdefg.org",1234)
    .
    .
    .
    dim netstream As NetworkStream = tcpClient.GetStream()
    in my send & receive routines.

    The application is working fine but if I close the client using tcpclient.close or if the server is turned off, loses connection or the net crahses for some reason, i cannot reconnect again without rebooting the client. I don't want to do this because the client will be unattended and will have to reconnect automatically.
    Some expert advice would be appreciated. i can post the whole client code if that helps.
    neil9

  2. #2

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    4

    Re: TCP client

    Thanks Cicatrix for your reply.
    Thats the problem I have been unable to do that. Here is a snippet of the code:
    The code works, in that I can connect to the remote device, send commands and copy the reply(in my textbox1). But if I call TCP close ( my button 6) or if for some reason the connection is lost, I cannot re-connect ( my button 5). Button 5 works when the program is run the first time..


    Imports system
    Imports System.io
    Imports System.IO.Directory
    Imports System.IO.DirectoryInfo
    Imports System.Object
    Imports Microsoft.VisualBasic
    Imports System.Windows.Forms.Form
    Imports System.text
    Imports System.Text.RegularExpressions
    Imports System.Net.Sockets
    Public Class Form1
    Dim tcpClient As New TcpClient
    Dim tstring As String 'TCP tx string
    Dim Rstring As String 'TCP received string
    '****************************************************
    Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Try
    tcpClient.Connect("*******.org", 2101)
    Call displayAndLog("TCP connect")
    Catch
    Call displayAndLog("TCP connect error")
    End Try

    End Sub
    '***************************************************
    Private Sub TCPSend(ByVal tstring)
    'TCP send data
    Try
    Dim netstream As NetworkStream = tcpClient.GetStream()
    If netstream.CanWrite Then
    Rxdata = Nothing
    rxdatacopy = Nothing
    TextBox1.Text = Nothing '
    Dim sendBytes As [Byte]() = Encoding.UTF8.GetBytes(tstring)
    netstream.Write(sendBytes, 0, sendBytes.Length)
    Else
    TextBox1.Text = " Can't write data to this stream."
    tcpClient.Close()
    ' Closing the tcpClient instance does not close the network stream.
    netstream.Close()
    Return
    End If
    Catch ex As Exception
    TextBox1.Text = "TCPSend Error "
    tcpClient.Close()
    netStream.Close()

    End Try
    End Sub
    '*********************************************************
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    'TCP close

    Try
    Dim netstream As NetworkStream = tcpClient.GetStream()
    netStream.Close()
    tcpClient.Close()

    Catch ex As Exception
    Call displayAndLog("close fault ")
    End Try
    End Sub
    '*********************************************
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TCP_ReceiveTimer.Tick
    'poll the tcp receiver
    Try
    Dim netStream As NetworkStream = tcpClient.GetStream
    If netStream.CanRead Then
    Do While netStream.DataAvailable = True
    Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    netStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
    Dim returndata As String = Encoding.UTF8.GetString(bytes)
    TextBox1.Text = TextBox1.Text + returndata
    Loop
    Else
    TextBox1.Text = "cannot read data from this stream."
    tcpClient.Close()
    netStream.Close()
    Return
    End If
    Rxdata = TextBox1.Text.ToString
    rxdatacopy = TextBox1.Text.ToString

    Call processRXdata(Rxdata)


    Catch ex As Exception
    Call displayAndLog("TCP Rx timed out")
    End Try

    End Sub
    '***************************************************

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: TCP client

    Try it. Maybe this would help to reset your tcp client.

    Code:
    tcpClient.Close
    tcpClient.Dispose
    tcpClient = New TcpClient
    Oh, by the way - naming your instance the same as the class is a bad practice.

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    4

    Re: TCP client

    Yes I know. Ill change it to myTCPclient.
    I'll try that. Thanks Cicatrix. Back to you shortly

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    4

    Re: TCP client

    Brilliant. That works. I can now close and open at will, provided I wait a bit between changes. What I am doing Cicatrix is polling the server which is 500miles away, every 15 minutes, and collecting some data.
    Should I leave the connection made and check each time if its still up, reconnect if its not, or - should I close the connection after every 15 minute transaction, and reopen it next time? I have the system running now on auto, polling every 5 minutes and I'm closing the connection after each poll, so we'll see how robust that is over the next few days. The microsoft web site says that there is no reliable way of determining whether a connection is made, other than trying to send something and seeing if it happened. Seems clumsey to me?
    Once again-thanks Cicatrx. Watch this space-I'll be back with more dumb questions I'm sure.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width