Results 1 to 2 of 2

Thread: [RESOLVED] TCP Sockets Problem

  1. #1

    Thread Starter
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Resolved [RESOLVED] TCP Sockets Problem

    I'b dabbling with a client/server application which uses TCP sockets to communicate between a server and multiple clients.

    I'm working from various examples on other messageboards and the MSDN and have hit a problem.

    I'm working on a login function - the client sends a message comprised of a token "<L>" and the username and password (not worrying about encryption at the moment as its just a proof of concept)

    When the server receives a message with the <L> token it parses the username and password, checks them against its list of valid users and passwords and returns either <F> if it fails or <R> plus a number if it succeeds.

    The problem I've got is that the server receives the message OK, checks the username and password OK, formulates the string response OK, and sends the message back.

    When the client receives the message it appears to be getting a blank message, but of the correct length! By that I mean I check on the server and it is sending back a 14 character message, and the client is receiving a 14 character message but it is empty.

    The code on the server for sending is :
    Code:
         Public Sub SendMessage(ByVal Message As String)
    
            Dim _Client As New TcpClient(_ServerIPAdress, _ServerPort)
    
            Message = _ServerIPAdress & "::" & Message
    
            Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(Message)
            Dim stream As NetworkStream = _Client.GetStream()
    
            ' Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length)
    
            stream.Close()
            _Client.Close()
            _Client = Nothing
    
        End Sub
    And the receiving code is

    Code:
        Private Sub OnRead(ByVal AR As IAsyncResult)
    
            Dim DataSize As Integer = _InboundClient.GetStream.EndRead(AR)
            Dim Buffer(DataSize) As Byte
            Dim Message As String
    
            Message = Encoding.ASCII.GetString(Buffer, 0, DataSize)
    
            If Message <> "" Then Stop
    
            _InboundClient.GetStream.BeginRead(Buffer, 0, DataSize, AddressOf OnRead, Nothing)
    
        End Sub
    When I look at the elements of Buffer they all seem to hold 0.

    I'm sure its something really obvious but I'm just going blind looking at the same bit of code! Obviously the message sending is OK but there's something wrong with the way i'm retrieving it.

    Any ideas?
    Last edited by keystone_paul; Apr 8th, 2009 at 02:33 AM.

  2. #2

    Thread Starter
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: TCP Sockets Problem

    Resolved it - had a variable scoping problem - Buffer needed to be a module-level variable as it was being filled in a different procedure

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