Problems with TCPClient

J

Jerry Spence1

I am trying to get a TCP client working. I am using an example code as
follows:


Dim tcpCli As New TcpClient("192.168.0.6", 1001)
' Retrieve the stream that can send and receive data.
Dim ns As NetworkStream = tcpCli.GetStream
' Send a request to my Tibbo Ethernet device to give me the version number.
Dim sw As New StreamWriter(ns)
sw.WriteLine(chr(255) & chr(2) & "V" & vbcrlf)
sw.Flush()
' Receive data from the server application and display it.
Dim sr As New StreamReader(ns)
Dim result As String = sr.ReadLine()
Console.WriteLine(result)
' Release resources.
sr.Close()
sw.Close()
ns.Close()

Explanation:

On this line I am asking for the Tibbo Ethernet device to send back the
version number of the software:

sw.WriteLine(chr(255) & chr(2) & "V" & vbcrlf)

I am expecting to get a response back. Looking at an Ethernet analyser I do
get the response back, but when I get to the next line

Dim result As String = sr.ReadLine()

It just sits there waiting to receive something. When I send some more data
from the server when it is at this line, then it it received OK.

I can' t quite understand what is happening.

Hope I have explained it OK

-Jerry
 
J

Jerry Spence1

Actually I think there may be something wrong with:

sw.WriteLine(chr(255) & chr(2) & "V" & vbcrlf)

When I look at what is actually being sent out on the analyser it is 3F 02
instead of FF 02. Am I misusing this line? Is it stripping off bits somehow?

-Jerry
 
J

Jerry Spence1

Yes there was. I changed it to
Dim sw As New StreamWriter(ns, System.Text.Encoding.Default)

and now I get the FF 02 correctly. However I still don't read the response
correctly at

Dim result As String = sr.ReadLine()

It still hangs, rather than returning the response that is in the buffer

-Jerry
 
G

Guest

Jerry Spence1,
Just guess: It seems TCP response ended with vbLf. sr.ReadLine() is ended
with vbCrLf. Try use NetworkStream.Read Method directly, My guess is right
if NetworkStream.Read >0

Good luck
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top