Results 1 to 11 of 11

Thread: [RESOLVED] TCP/IP Problem

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    41

    Resolved [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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    41

    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??

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: TCP/IP Problem

    Have you tried sending a \r\n on the end of your message, rather than just \n?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    41

    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?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    41

    Re: TCP/IP Problem

    Ok! I'll try that asych method. Thanks!

    I'll keep you guys posted.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    41

    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!

  11. #11

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    41

    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
  •  



Click Here to Expand Forum to Full Width