vc++socket通信数据丢失问题!!

vc++socket通信问题,tcp协议!服务端是java,当服务端发送很多数据,数据在一个协议包里!在这个时候,客户端会丢失一部分数据,为什么会这样!客户端是vc++写... vc++socket通信问题,tcp协议!服务端是java,当服务端发送很多数据,数据在一个协议包里!在这个时候,客户端会丢失一部分数据,为什么会这样!客户端是vc++写的!我试着扩大缓冲区,一样不行!

sock_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
addr_sev.sin_family = AF_INET;
addr_sev.sin_port = htons(PORT_SERVER);
addr_sev.sin_addr.s_addr = inet_addr(IPADDR);
connect(sock_client, (sockaddr *)&addr_sev, sizeof(addr_sev));
char recvBuf[60000]={NULL};
while(true){
//recvfrom(sock_client, recvBuf,sizeof(recvBuf), 0);
recvfrom(sock_client,recvBuf,sizeof(recvBuf),0,(SOCKADDR*)&addr_sev,&len);
if(strlen(recvBuf)>1){
string temp = recvBuf;
EnterCriticalSection(&zlLock);
//zl.push_back(temp);
long a = temp.size();
memset(recvBuf,0,60000);
LeaveCriticalSection(&zlLock);
}
}
展开
 我来答
dkhnikcos46
2011-06-27 · TA获得超过365个赞
知道小有建树答主
回答量:454
采纳率:100%
帮助的人:0
展开全部
//取出数据,这里发现数据接收不完整。。。。
int nRecv = recv(cliSock, buf, sizeof(CStock)*(N+2), 0);
你没有判断到底返回了多少数据,而只是检查了《=0.你怎么知道数据是否接受完毕?
另外你还要确定,对方是否采取了优雅关闭。否则有可能丢失数据。
你可以参考一下MSDN的示例:
----------------------------------------------------------------
#include <winsock2.h>
#include <stdio.h>

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int __cdecl main() {

//----------------------
// Declare and initialize variables.
WSADATA wsaData;
int iResult;

SOCKET ConnectSocket;
struct sockaddr_in clientService;

char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;

//----------------------
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}

//----------------------
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return 1;
}

//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( 27015 );

//----------------------
// Connect to server.
iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
if ( iResult == SOCKET_ERROR) {
closesocket (ConnectSocket);
printf("Unable to connect to server: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}

// Send an initial buffer
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

printf("Bytes Sent: %ld\n", iResult);

// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

// Receive until the peer closes the connection
do {

iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());

} while( iResult > 0 );

// cleanup
closesocket(ConnectSocket);
WSACleanup();

return 0;
}
3284王
2011-06-24 · 超过15用户采纳过TA的回答
知道答主
回答量:386
采纳率:0%
帮助的人:109万
展开全部
//取出数据,这里发现数据接收不完整。。。。
int nRecv = recv(cliSock, buf, sizeof(CStock)*(N+2), 0);
你没有判断到底返回了多少数据,而只是检查了《=0.你怎么知道数据是否接受完毕?
另外你还要确定,对方是否采取了优雅关闭。否则有可能丢失数据。
你可以参考一下MSDN的示例:
----------------------------------------------------------------
#include <winsock2.h>
#include <stdio.h>

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int __cdecl main() {

//----------------------
// Declare and initialize variables.
WSADATA wsaData;
int iResult;

SOCKET ConnectSocket;
struct sockaddr_in clientService;

char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;

//----------------------
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}

//----------------------
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return 1;
}

//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( 27015 );

//----------------------
// Connect to server.
iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
if ( iResult == SOCKET_ERROR) {
closesocket (ConnectSocket);
printf("Unable to connect to server: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}

// Send an initial buffer
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

printf("Bytes Sent: %ld\n", iResult);

// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

// Receive until the peer closes the connection
do {

iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());

} while( iResult > 0 );

// cleanup
closesocket(ConnectSocket);
WSACleanup();

return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wiwimo
2011-06-20 · TA获得超过261个赞
知道答主
回答量:167
采纳率:0%
帮助的人:84.5万
展开全部
这个读法好别扭,应该是按每次缓冲区数据长度来读数据,直到缓冲区数据长度为0为止就退出读取数据吧。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2011-06-21
展开全部
你的程序有问题。
用strlen来判断接收的长度很危险,如果一次接收的数据中间没有0,就会访问越界造成异常,为什么不直接用recvfrom的返回值?
而且如果数据的开头或中间有0,你就会把后面的数据丢弃。

另外,没有看到你如何判断并退出接收,似乎是个死循环。应该判断接收长度是否小于等于0作为结束条件。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式