接收3144字节TCP / IP套接字在调试模式期间,但在执行1024字节期间 [英] Receiving 3144 bytes TCP/IP socket During debug mode, but during excecution 1024 byte

查看:141
本文介绍了接收3144字节TCP / IP套接字在调试模式期间,但在执行1024字节期间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过TCP / IP套接字接收3144字节在调试模式期间,但在执行期间收到1024字节

为什么会发生


IPEndPoint ipEnd = new IPEndPoint(ipAdd,ipPort);

套接字套接字=新套接字(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);


套接字。连接(ipEnd);

byte [] sendData = Encoding.ASCII.GetBytes(" msg");

socket.Send(sendData);


int byteLen = 4096;

byte [] RecData = new byte [byteLen];

DataLen = socket.Receive(RecData,0,RecData .Length,SocketFlags.None);

I am Receiving 3144 bytes through TCP/IP socket During debug mode, but during excecution 1024 byte were received
Why it is happen

IPEndPoint ipEnd = new IPEndPoint(ipAdd, ipPort);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

socket.Connect(ipEnd);
byte[] sendData = Encoding.ASCII.GetBytes("msg");
socket.Send(sendData);

int byteLen = 4096;
byte[] RecData = new byte[byteLen];
DataLen = socket.Receive(RecData, 0, RecData.Length, SocketFlags.None);

推荐答案

Raj,


你需要围绕你的接收呼叫建立一个循环。


由于您不知道在网络数据包中通过了多少字节,您将不得不等待整个消息通过。


这基本上是伪代码/ java组合。希望它会有所帮助。
Raj,

You need to build a loop around your call to Receive.

Since you have no idea of how many bytes make it through in a network packet, you''ll have to wait for the whole message to come through.

This is basically pseudo-code/java combined. Hopefully it''ll help.
展开 | 选择 | Wrap | 行号


是的,它运作良好。不使用socket.Available我们如何在套接字中找到可用的消息(数据)。


boolean msgComplete = false;

int start = 0;

while(!msgComplete)

{

int input = socket.Receive(RecData,start,(RecData.Length-start),SocketFlags 。没有);

if(input = -1)

抛出新的异常(socket disconnect);

else if(socket.Available!= 0)

start + = input;

else

msgComplete = true;

}
yes, it works good. Without using socket.Available how would we find available message(data) in the socket.

boolean msgComplete = false;
int start = 0;
while (! msgComplete)
{
int input = socket.Receive(RecData, start, (RecData.Length-start), SocketFlags.None);
if (input = -1)
throw new Exception("socket disconnect");
else if (socket.Available != 0)
start += input;
else
msgComplete = true;
}


如果不使用socket.Available,可以找到套接字中的可用字节。
How would the available bytes in the socket could be found without using "socket.Available".


这篇关于接收3144字节TCP / IP套接字在调试模式期间,但在执行1024字节期间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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