請幫我找出什麼是錯在客戶端服務器通信...發送和接收的所有數據(C++插座)
客戶端每秒使用此功能向服務器發送JPEG幀從相機25倍:
int SendAll(SOCKET &client_socket, string &data){
int result, bytes_sent, sent_total, bytes_remain, package_size;
result = bytes_sent = sent_total = package_size = 0;
bytes_remain = data.size();
while (bytes_remain && !result){
package_size = (bytes_remain<KILOBYTE)?bytes_remain:KILOBYTE;
bytes_sent = send(client_socket, &data[sent_total], package_size, 0);
if (bytes_sent > 0){
bytes_remain-= bytes_sent;
sent_total+= bytes_sent;
}
else if(bytes_sent == SOCKET_ERROR){
result = WSAGetLastError();
break;
}
}
return result;
}
在線程循環:
while(CAMERA.Stream){
OperationStartTime = GetTickCount();
//RecvBuffer.resize(512);
result = SendAll(CAMERA.Socket, CameraFrame);
if(result != 0){
cout << "error in send - " << result;
CAMERA.Stream = false;
break;
}
if (recv(CAMERA.Socket, &RecvBuffer, 1, 0) < 0){
CAMERA.Stream = false;
break;
}
while(OperationStartTime + StreamRate > GetTickCount()){
if (!CAMERA.Stream){break;}
Sleep(1);
}
}
有時候我看到 「錯誤發送 - 10053」(在本地主機上測試)
Server使用此功能,接收的所有數據:
int RecvAll(SOCKET &client_socket, string &data){
data = "";
int bytes_received = 0;
int result = 0;
char* recv_buffer;
do {
recv_buffer = (char*)malloc(KILOBYTE);
bytes_received = recv(client_socket, recv_buffer, KILOBYTE, 0);
if (bytes_received > 0){
data.append(recv_buffer, bytes_received);
if (bytes_received < KILOBYTE || data.size() >= 512*KILOBYTE){
bytes_received = result = 0;}
}
else if(bytes_received == SOCKET_ERROR){
data.clear();
result = WSAGetLastError();
}
free(recv_buffer);
}
while (bytes_received > 0);
return result;
}
而在服務器端沒有錯誤。
SendAll
和RecvAll
函數有什麼問題以及如何實現穩定的數據傳輸?
PS:有時客戶端停止,只有重新連接有幫助。但是如果要重新連接,服務器會顯示另一個連接的套接字 - 這意味着CAMERA.Socket是阻止的,我不明白爲什麼
你問我們調試程序的嗎? –
你試過查找錯誤代碼嗎? –
@BoundaryImposition,根本不用調試,但也許有人會看到邏輯上的錯誤 – Iceman