|
-
Mar 17th, 2010, 03:59 PM
#3
Thread Starter
New Member
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
'***************************************************
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|