473,385 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

TCPClient problems

I am using a tcpclient and a network stream to communicate with the TCP
port on a printer for testing the printer firmware.
The code I have works perfectly the first time I run it. The printer
reacts correctly.
If I run it again there is a long delay before the the printer reacts.

When we used VB6 sockets to do this we had no problems.
Below is the code I am using in Visual Studio 2005
Any help would be appreciated.

Public Function SendReceive(ByVal strMechCommand As String, _
ByRef strReturnedData As
String) As Boolean
'--- Create a TcpClient ---
Try
printer = New TcpClient(mIPAddress, Port)
printer.NoDelay = True
mConnected = True
'--- Get a client stream for reading and writing ---
stream = printer.GetStream()

Catch er As Exception
mErrorMessage = er.Message
Try
stream.Dispose()
printer.Close()
Catch err As Exception
mErrorMessage = mErrorMessage & vbCrLf & "No
Connections to close."
End Try
End Try

'--- If the printer is connected
If printer.Connected And stream.CanWrite And stream.CanRead
Then
Try
Dim mechCommand As [Byte]() =
System.Text.Encoding.ASCII.GetBytes(strMechEnter & strMechCommand &
strMechExit)
stream.Write(mechCommand, 0, mechCommand.Length)
Thread.Sleep(300)
'--- If return data is expected then go get it ---
If strReturnedData <"SendOnly" Then
'--- Create variable to create a Buffer to
store the response bytes ---
Dim received_data = New [Byte](1024) {}
'--- Create a string to store the response
ASCII representation ---
Dim received_string As [String] =
[String].Empty
'--- Create a variable to store the number of
bytes received ---
Dim bytes As Int32
'--- Create a counter variable ---
Dim count As Integer = 0
'--- Loop that waits for upto 4 seconds for
data to be returned --
While stream.DataAvailable = False
Thread.Sleep(100)
count += 1
If count 40 Then
mErrorMessage = "Timeout while waiting
for data."
Disconnect()
Exit Function
End If
End While
'--- Loop that reads data 1024 bytes at a time
till all data received ---
While stream.DataAvailable
bytes = stream.Read(received_data, 0,
received_data.Length)
received_string +=
System.Text.Encoding.ASCII.GetString(received_data , 0, bytes)
Thread.Sleep(500)
End While
Disconnect()
strReturnedData = received_string
Else
Disconnect()
strReturnedData = ""
End If
Return True
Catch err As Exception
mErrorMessage = err.Message
Disconnect()
Return False
End Try
Else
mErrorMessage = "SendReceive method: No Connection to
printer found"
mConnected = False
Disconnect()
Return False
End If
End Function

Dec 4 '06 #1
1 1988
What code is in the Disconnect() method?
"WildBill" <ws*****@gmail.comwrote in message
news:11*********************@73g2000cwn.googlegrou ps.com...
>I am using a tcpclient and a network stream to communicate with the TCP
port on a printer for testing the printer firmware.
The code I have works perfectly the first time I run it. The printer
reacts correctly.
If I run it again there is a long delay before the the printer reacts.

When we used VB6 sockets to do this we had no problems.
Below is the code I am using in Visual Studio 2005
Any help would be appreciated.

Public Function SendReceive(ByVal strMechCommand As String, _
ByRef strReturnedData As
String) As Boolean
'--- Create a TcpClient ---
Try
printer = New TcpClient(mIPAddress, Port)
printer.NoDelay = True
mConnected = True
'--- Get a client stream for reading and writing ---
stream = printer.GetStream()

Catch er As Exception
mErrorMessage = er.Message
Try
stream.Dispose()
printer.Close()
Catch err As Exception
mErrorMessage = mErrorMessage & vbCrLf & "No
Connections to close."
End Try
End Try

'--- If the printer is connected
If printer.Connected And stream.CanWrite And stream.CanRead
Then
Try
Dim mechCommand As [Byte]() =
System.Text.Encoding.ASCII.GetBytes(strMechEnter & strMechCommand &
strMechExit)
stream.Write(mechCommand, 0, mechCommand.Length)
Thread.Sleep(300)
'--- If return data is expected then go get it ---
If strReturnedData <"SendOnly" Then
'--- Create variable to create a Buffer to
store the response bytes ---
Dim received_data = New [Byte](1024) {}
'--- Create a string to store the response
ASCII representation ---
Dim received_string As [String] =
[String].Empty
'--- Create a variable to store the number of
bytes received ---
Dim bytes As Int32
'--- Create a counter variable ---
Dim count As Integer = 0
'--- Loop that waits for upto 4 seconds for
data to be returned --
While stream.DataAvailable = False
Thread.Sleep(100)
count += 1
If count 40 Then
mErrorMessage = "Timeout while waiting
for data."
Disconnect()
Exit Function
End If
End While
'--- Loop that reads data 1024 bytes at a time
till all data received ---
While stream.DataAvailable
bytes = stream.Read(received_data, 0,
received_data.Length)
received_string +=
System.Text.Encoding.ASCII.GetString(received_data , 0, bytes)
Thread.Sleep(500)
End While
Disconnect()
strReturnedData = received_string
Else
Disconnect()
strReturnedData = ""
End If
Return True
Catch err As Exception
mErrorMessage = err.Message
Disconnect()
Return False
End Try
Else
mErrorMessage = "SendReceive method: No Connection to
printer found"
mConnected = False
Disconnect()
Return False
End If
End Function

Dec 4 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christopher H. Laco | last post by:
I'm having a problem with the TcpClient that I can only conclude is either a feature, or a complete misunderstanding of the docs on my part. In a nutshell, I'm simply performing the following...
3
by: מורדי | last post by:
Hi, I'm writing a client/server application in which the client send a series of screenshots to the server to be saved using the tcpclient. in most cases the first screenshot is transmitted ok...
15
by: jin | last post by:
hi, i'm trying using the tcpClient to get a html file from net, instead of using WebClient or WebRequest, the main part of the source code is like this: private void tcpconnect() { tcp=new...
7
by: David Dvali | last post by:
How can I check if TcpClient is connected (or not connected)?
5
by: Jim W | last post by:
I have an ASP.NET app that has very slow connect times using TCPClient, or the Socket class, or even calling a COM object that uses C socket calls on certain similar XP SP2 boxes. On those...
2
by: Noozer | last post by:
Hi! Trying to get my head around working with network connectivity in VB.Net. I've gotten a fairly decent control based on the sample in the 101VBSamples available from Microsoft.What I'm having...
1
by: Anders Berg | last post by:
Hi! I'm developing a very simple chat application in VB.NET and I've stumbled into a problem. For simplicity I'm using the TcpListener and TcpClient classes. However, at one point I discovered...
10
by: Zytan | last post by:
I have a TcpClient. I set the read/write timeouts at 1 minute (in milliseconds). I get a NetworkStream from it and confirm the timeouts still exist. I do a NetworkStream.Write() and then a...
2
by: Zytan | last post by:
I just had the problem occur again, with NetworkStream.Write() doing its thing with a timeout... and it just sits and waits and waits and waits... it never times outs. So, I shut the server down...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.