Click here to Skip to main content
15,892,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my code to receive data from TCP/IP.It only reads 10 character of data and I want it to display more than 10 character.Plz help me..

VB
Private Sub btnconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconnect.Click
       client = New TcpClient
       client.Connect("192.168.0.100", portno)
       Dim data(client.ReceiveBufferSize) As Byte
       Dim readBytes As Integer = client.GetStream.Read(data, 0, CInt(client.ReceiveBufferSize))
       ListBox1.Items.Add(System.Text.Encoding.ASCII.GetString(data, 0, readBytes))
       'ListBox1.Items.Add(readBytes)
   End Sub
Posted

1 solution

Hello Hardik,

You are trying to read over a raw socket. Without any particular protocol it will be very difficult for you to read the correct data. If you read the documentation of GetStream.Read method you will realize that, it's merely reads the specified number of bytes. If your data length is less than ReceiveBufferSize then you are going to receive the full data otherwise you are going to receive the partial data upto ReceiveBufferSize. For you to be able to read the entire message it's very important that you either know the entire message length .e.g first 4 bytes of your message represents the total message length or you devise an agreement (protocol) such that all your messages are terminated by two CrLf (e.g. HTTP Protocl). This way you will be able to wrap your read code in a while loop and be able to read the entire message.

Regards,
 
Share this answer
 
Comments
HArdik_Shah1234 29-Jun-13 3:42am    
Thanks Prasad for answer..
In my case size is not fix..So can you elaborate how can i achieve this you devise an agreement (protocol) such that all your messages are terminated by two CrLf (e.g. HTTP Protocl).
Prasad Khandekar 29-Jun-13 4:17am    
Hello Hardik,

You can do it in two ways.

1. Terminate your message with a unique character sequence, eg. 4 CrLf or perhaps a boundary separator similar to one found in Multipart messages.
2. Message Signature bytes at the beginning of your message followed by Length of actual message and a CrLf. eg. HARDIK_MSG1024

Regards,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900