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 :
And the receiving code isCode: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
When I look at the elements of Buffer they all seem to hold 0.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
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?




Reply With Quote