Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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("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 28-Jun-13 22:03pm

1 solution

Rate this: bad
good
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.
 
Regards,
  Permalink  
Comments
HArdik_Shah1234 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,142
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 29 Jun 2013
Copyright © CodeProject, 1999-2014
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