好,字符串>8k
发送端:
int mm = strSend1.GetLength(); /////传送字符串
int y = mm / 1024;
CString sWantSend,send_y;
send_y.Format("%d",y);
char* chy = send_y.GetBuffer(0);
pSocket->Send(chy,send_y.GetLength(),0);//发送循环次数
Sleep(50);//延时
for ( int i = 0; i < y; i++ )
{
char szBuff[1024];
sWantSend = strSend1.Mid(0,1023);
strSend1 = strSend1.Mid(1024);
strncpy( szBuff, sWantSend, strlen( sWantSend ) );
pSocket->Send(szBuff,1024,0);
Sleep(50);
}
int yy = strSend1.GetLength();
CString uy;
uy.Format("%d",yy);
char* by = uy.GetBuffer(0);
pSocket->Send(by,uy.GetLength(),0);//发送剩余字符串1大小
Sleep(50);//发送剩余字符串
if ( yy != 0 )
{
char szBuff1[1024];
strncpy( szBuff1, strSend1, strlen( strSend1 ) );
pSocket->Send(strSend1,yy,0);
}
接收端:
CString strService;
char chy[5] ="\0";//接受循环次数
sockClient.Receive(chy,5,0);
int y = atoi(chy);
Sleep(50);
CString strBuf,strBuf1;//开始接受字符串
for ( int i = 0; i < y; i++ )
{
char buff[1024] = {0};
sockClient.Receive(buff,1024,0);
strBuf.Format("%s",buff);
strService += strBuf;
Sleep(50);
}
char bu[10] = "\0";//接收剩余字符串大小
sockClient.Receive(bu,10,0);
int size = atoi(bu);
Sleep(50);//接收剩余的字符串
if ( size != 0 )
{
char buf[1024] = {0};
sockClient.Receive(buf,size,0);
strBuf1.Format("%s",buf);
strService += strBuf1;
}接收后字符串保存到strService里
问题:运行后,接收到的数据,总不完整,中间总有错误字符。大伙看看是何出有问题,请指出。
我发现发送和接收的数量不一致。很头疼,发出来让大伙帮我看看。
发送端:
int mm = strSend1.GetLength(); /////传送字符串
int y = mm / 1024;
CString sWantSend,send_y;
send_y.Format("%d",y);
char* chy = send_y.GetBuffer(0);
pSocket->Send(chy,send_y.GetLength(),0);//发送循环次数
Sleep(50);//延时
for ( int i = 0; i < y; i++ )
{
char szBuff[1024];
sWantSend = strSend1.Mid(0,1023);
strSend1 = strSend1.Mid(1024);
strncpy( szBuff, sWantSend, strlen( sWantSend ) );
pSocket->Send(szBuff,1024,0);
Sleep(50);
}
int yy = strSend1.GetLength();
CString uy;
uy.Format("%d",yy);
char* by = uy.GetBuffer(0);
pSocket->Send(by,uy.GetLength(),0);//发送剩余字符串1大小
Sleep(50);//发送剩余字符串
if ( yy != 0 )
{
char szBuff1[1024];
strncpy( szBuff1, strSend1, strlen( strSend1 ) );
pSocket->Send(strSend1,yy,0);
}
接收端:
CString strService;
char chy[5] ="\0";//接受循环次数
sockClient.Receive(chy,5,0);
int y = atoi(chy);
Sleep(50);
CString strBuf,strBuf1;//开始接受字符串
for ( int i = 0; i < y; i++ )
{
char buff[1024] = {0};
sockClient.Receive(buff,1024,0);
strBuf.Format("%s",buff);
strService += strBuf;
Sleep(50);
}
char bu[10] = "\0";//接收剩余字符串大小
sockClient.Receive(bu,10,0);
int size = atoi(bu);
Sleep(50);//接收剩余的字符串
if ( size != 0 )
{
char buf[1024] = {0};
sockClient.Receive(buf,size,0);
strBuf1.Format("%s",buf);
strService += strBuf1;
}接收后字符串保存到strService里
问题:运行后,接收到的数据,总不完整,中间总有错误字符。大伙看看是何出有问题,请指出。
我发现发送和接收的数量不一致。很头疼,发出来让大伙帮我看看。
实际情况可能是并没有发送成功你自己程序认为发送成功了
所以才会出现接受端收到数据少于发送端。
当然,这不是解决的办法。send
The Windows Sockets send function sends data on a connected socket.int send(
SOCKET s,
const char FAR *buf,
int len,
int flags
);
Parameters
s
[in] Descriptor identifying a connected socket.
buf
[in] Buffer containing the data to be transmitted.
len
[in] Length of the data in buf.
flags
[in] Indicator specifying the way in which the call is made. 这是send函数的用法
如果你想看更多得了例子,去这里下载一个sample,学习一下巴:)
http://www.vckbase.com/code/listcode.asp?mclsid=9&sclsid=901祝你成功。