通过TCP / P接收数据 [英] Recevie data through TCP/P

查看:147
本文介绍了通过TCP / P接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我从TCP / IP接收数据的代码。它只读取10个字符的数据,我希望它显示超过10个字符.Plz帮帮我..



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

推荐答案

Hello Hardik,



您正在尝试读取原始套接字。没有任何特定的协议,您将很难读取正确的数据。如果您阅读GetStream.Read方法的文档,您将意识到,它只是读取指定的字节数。如果您的数据长度小于ReceiveBufferSize,那么您将接收完整数据,否则您将接收部分数据到ReceiveBufferSize。为了能够读取整个消息,您要么知道整个消息长度是非常重要的。例如,消息的前4个字节代表总消息长度,或者您设计一个协议(协议),以便所有消息都被终止两个CrLf(例如HTTP Protocl)。通过这种方式,您可以将读取的代码包装在while循环中,并能够读取整个消息。



问候,
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,


这篇关于通过TCP / P接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆