471,122 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

Recieving zero's instead of string through tcp

I work in visual basic,and have 2 forms programs.

One acts as a client, the other as a server. I send two things from client to server;
1: a string describing which service of the server is needed. The server enters a sub based on this string.

2: data needed for the sub the server is in.

However, apparently the second transmission results in 8000+ zero's being recieved instead of the string i sent..

Code: Client

Expand|Select|Wrap|Line Numbers
  1.     'will send the info
  2.     Public Sub sendInfo()
  3.         'check connection
  4.         If Not connected Then
  5.             Throw New Exception("There is no connection to the server.")
  6.         End If
  7.  
  8.        'send message
  9.         Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("SERVICE_1")
  10.         networkStream.Write(sendBytes, 0, sendBytes.Length)
  11.  
  12.         Dim a As String = "string to be sent for the service_1 sub of the server"
  13.  
  14.         Dim sendBytes2 As [Byte]() = Encoding.ASCII.GetBytes(a)
  15.         networkStream.Write(sendBytes2, 0, sendBytes2.Length)
  16.     End Sub

Code: server
Expand|Select|Wrap|Line Numbers
  1.     'will serve a client
  2.     Private Sub serveClient(ByVal client As TcpClient)
  3.         'recieve message
  4.         Dim bytes(client.ReceiveBufferSize) As Byte
  5.         client.GetStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
  6.         Dim request As String = Encoding.ASCII.GetString(bytes)
  7.  
  8.         'check needed service
  9.  
  10.         If matches(request, "SERVICE_1") Then
  11.             service1(client)
  12.         ElseIf matches(request, "SERVICE_2") Then
  13.             service2(client)
  14.         ElseIf matches(request, "SERVICE_3") Then
  15.             service3(client)
  16.         End If
  17.     End Sub
  18.  
  19.  
  20.     Private Sub service1(ByVal client As TcpClient)
  21.  
  22.         Dim bytes(client.ReceiveBufferSize) As Byte
  23.         client.GetStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
  24.         Dim clientdata As String = Encoding.ASCII.GetString(bytes)
  25.         Dim s() As String = clientdata.Split(";")
  26.  
  27.         If (s.Length < 3) Then
  28.             MsgBox("string didn't contain ';' zero's recieved")
  29.         End If
  30.  
  31.         dosomething(s(0), s(1), s(2))
  32.  
  33.     End Sub
so I repeat, the server does recieve the string "SERVICE_1" correctly, but it does NOT recieve the needed string in service1 sub. What am I doing wrong?

(when I putted breakpoints on all lines, and ran through them, apparently it did recieve it correctly, but it does not do that without breakpoints)

Thanx
Aug 3 '08 #1
4 1069
*bump*
someone, please?
Aug 4 '08 #2
Plater
7,872 Expert 4TB
I would say that your first read on the server side is probably reading in BOTH messages, leaving nothing for the 2nd read to read.
If you were doing proper sending and receiving, you would be checking the "actual number of bytes read/written" and validating you have read the correct ammount.
Aug 4 '08 #3
I've put a breakpoint only on the line right after the server read the string.

Instead of it being: "SERVICE_1" , it was "SERVICE_1.
(without the ending parenthesis) What's up with that?

Do you have an example of a better way of doing this?
Aug 5 '08 #4
Plater
7,872 Expert 4TB
Well I would start by using delimenators or come up with some other messaging format.
With a deliminator you can tell your first read to only read up until the deliminator charater(s). Making sure the data is still there following it.

Also, you tell the send and receive functions to send/receive the size of their buffer, and not the size of data you actually want, try only sending as many bytes as are in your byte[] and see if that helps.
Aug 5 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

25 posts views Thread by Mantorok Redgormor | last post: by
33 posts views Thread by gk245 | last post: by
3 posts views Thread by Candace | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.