c# Socket怎样才可以把数据接收完整啊? 10
c#Socket怎样才可以把数据接收完整啊?我现在是这样做的,但经常收不完整。publicstringGetSocket(stringstr){try{IPEndPoin...
c# Socket怎样才可以把数据接收完整啊?
我现在是这样做的,但经常收不完整。
public string GetSocket(string str)
{
try
{
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(db.server), serverPort);
client = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ipEndPoint);
string sendStr = str;
byte[] bs = Encoding.Unicode.GetBytes(sendStr);
SendData(bs, client);
recvStr = "";
byte[] recvBytes = new byte[15000000];
int bytes;
bytes = client.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
recvStr += Encoding.Unicode.GetString(recvBytes, 0, bytes);
//client.BeginReceive(dataBuffer, 0, dataBuffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallBack), client);
//receiveDone.WaitOne();
client.Close();
return recvStr;
//return response;
}
catch
{
return "";
}
} 展开
我现在是这样做的,但经常收不完整。
public string GetSocket(string str)
{
try
{
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(db.server), serverPort);
client = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ipEndPoint);
string sendStr = str;
byte[] bs = Encoding.Unicode.GetBytes(sendStr);
SendData(bs, client);
recvStr = "";
byte[] recvBytes = new byte[15000000];
int bytes;
bytes = client.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
recvStr += Encoding.Unicode.GetString(recvBytes, 0, bytes);
//client.BeginReceive(dataBuffer, 0, dataBuffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallBack), client);
//receiveDone.WaitOne();
client.Close();
return recvStr;
//return response;
}
catch
{
return "";
}
} 展开
展开全部
、、、这样,Receive的返回值是接收的字节数,当是0时表示传送结束,否则循环接收
缓冲区不用太大,8k或64k看情况
Byte[] RecvBytes = new Byte[1024*8];
int iBytes = 1;
string strAllHtml = "";
while (iBytes > 0)
{
iBytes = conn.Receive(RecvBytes, RecvBytes.Length, 0);
strAllHtml += Encoding.UTF8.GetString(RecvBytes, 0, iBytes);
}
缓冲区不用太大,8k或64k看情况
Byte[] RecvBytes = new Byte[1024*8];
int iBytes = 1;
string strAllHtml = "";
while (iBytes > 0)
{
iBytes = conn.Receive(RecvBytes, RecvBytes.Length, 0);
strAllHtml += Encoding.UTF8.GetString(RecvBytes, 0, iBytes);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询