Click here to Skip to main content
13,090,446 members (42,559 online)
Rate this:
Please Sign up or sign in to vote.
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..

Private Sub btnconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconnect.Click
       client = New TcpClient
       client.Connect("", 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))
   End Sub
Posted 28-Jun-13 21:03pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 29 Jun 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100