|
-
Jun 14th, 2011, 09:39 PM
#1
Thread Starter
Member
[RESOLVED] TCP/IP Problem
Hi Guys! Good Day. Need to ask some help again. 
I am developing a client side application that writes a COMMANDS and listens a RESULT.
Now, the server side is made from perl codes..
The task is simple...
Declare Global Variable:
Code:
Private _TcpClient As New TcpClient
Private _NetStream As NetworkStream
1. Connect to the Server
Code:
Private Sub Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connect.Click
Try
_TcpClient = New TcpClient
_TcpClient.Connect("10.0.28.99", 7890)
Catch ex As Exception
Console.WriteLine(Err.ToString())
End Try
End Sub
2. Sends Command
Code:
Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click
_NetStream = Nothing
_NetStream = _TcpClient.GetStream
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("22,REGIST,000012\n")
_NetStream.Write(sendBytes, 0, sendBytes.Length - 1)
End Sub
3. Receive Result automatically
Code:
Private Sub ThreadOnRecieve
Do
Dim bytes(_TcpClient.ReceiveBufferSize) As Byte
_NetStream.Read(bytes, 0, CInt(_TcpClient.ReceiveBufferSize))
Dim returndata As String = Encoding.ASCII.GetString(bytes)
ReturnLabel.Text = returndata
Loop
End Sub
4. Disconnect to the Server
Code:
Private Sub Disconnect
_TcpClient.Close()
End Sub
Now, the problem are:
1. I Am definitely a newbie about TCP/IP
2. I could not send a command until i do this:
Code:
_NetStream.Flush()
_NetStream.Close()
but, when i did that. my stream are gone and i am not able to send another command. And when i tried to reconnect again, the server no longer accepts me... seems that my previous connection was still up..
Hope you can enlightens me.. Thanks guys!
-
Jun 14th, 2011, 11:48 PM
#2
Re: TCP/IP Problem
First up, this is from the documentation for the NetworkStream.Flush method:
The Flush method implements the Stream.Flush method; however, because NetworkStream is not buffered, it has no affect on network streams. Calling the Flush method does not throw an exception.
It sounds like the issue is more likely at the receiving end, like it is waiting for a specific terminator or the like. I've used a TcpClient and its NetworkStream before and I definitely didn't have to close the stream to receive data at the other end.
-
Jun 15th, 2011, 12:38 AM
#3
Thread Starter
Member
Re: TCP/IP Problem
Hi JM,
I was actually expecting not to close my stream.. but nothing happens when i issued my Command until I close the Stream or TCPClient.
Yes, i think the receiver is still expecting more until it was closed.
But I have tested it on using Telnet and I have no issues about it, it works fine...
Any work around we can do??
-
Jun 15th, 2011, 12:40 AM
#4
Re: TCP/IP Problem
Have you tried sending a \r\n on the end of your message, rather than just \n?
-
Jun 15th, 2011, 12:46 AM
#5
Re: TCP/IP Problem
Ah, now I see the issue. I didn't even notice that "\n" in the string until I read EG's post.
The issue is exactly as I said: the receiver is waiting for a terminator and you're not sending one. What you're sending there are the two characaters '\' and 'n', not the one character '\n'. VB doesn't understand those C-style escape sequences. That "\n" would work in C# but not in VB. If you want to send a line terminator then you would use the ControlChars class:
vb.net Code:
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("22,REGIST,000012" & ControlChars.Lf)
ControlChars.Lf is equivalent to "\n". For "\r\n" you can use ControlChars.CrLf or ControlChars.NewLine.
-
Jun 15th, 2011, 01:13 AM
#6
Thread Starter
Member
Re: TCP/IP Problem
WOW! Thanks JM and Evil_Girrafe!
I have no idea about the ControlChars.Lf..
haha! it worked!
Now the next step is how will i get the reply?
for every command i send there is a corresponding reply.
should i run a Thread or what?
-
Jun 15th, 2011, 01:18 AM
#7
Re: TCP/IP Problem
If sending and receiving the messages is quite quick then it's probably not necessary to use multiple threads. If it's likely to take a while then it might not be a bad idea. The NetworkStream supports multi-threading inherently, via its asynchronous methods.
-
Jun 15th, 2011, 01:29 AM
#8
Thread Starter
Member
Re: TCP/IP Problem
Ok! I'll try that asych method. Thanks! 
I'll keep you guys posted.
-
Jun 15th, 2011, 01:34 AM
#9
Re: TCP/IP Problem
If you want to see an example of asynchronous methods in action with a TcpClient, follow the CodeBank link in my signature and check out my Asynchronous TCP thread.
-
Jun 15th, 2011, 01:48 AM
#10
Thread Starter
Member
Re: TCP/IP Problem
Yes JM, I've already done my research and checked your CodeBank even before I created the thread here.. 
Very useful! Keep it up!
-
Jun 15th, 2011, 02:04 AM
#11
Thread Starter
Member
Re: TCP/IP Problem
Finished! Thanks a lot JM and Evil_Giraffe!
Resolved!
Tags for this Thread
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
|