当前位置: 首页 > 知识库问答 >
问题:

C++/Winsock TCP在客户机/服务器之间发送/Recv问题

贺立果
2023-03-14

案例A:

Client                Server
send(...);----------->While(recv(...)>0){
send(...);-----------> print(message);
send(...);----------->}
recv(...);----------->Send(...); 

服务器接收到这3条消息并发送最后一个答案,但是client上的recv失败,SOCKET_ERROR值为10060。使这种情况起作用的唯一方法是在客户端上的最后一次发送之后添加一个shutdown(...,SD_SEND)。

为什么A例会有这种行为?为什么只有在添加shutdown()命令时才工作?

Client                Server
send(...);----------->While(recv(...)>0){
recv(...);-----------> send(...);
send(...);-----------> ...
recv(...);-----------> ...
send(...);-----------> ...
recv(...);----------->}
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

#define DEFAULT_BUFLEN 1024
#define DEFAULT_PORT "27015"

int main() {
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
ADDRINFOA *ptr = NULL, *result = NULL, hints;
char *ans, *sendbuf = "message\0";
char recvbuf[1024];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
ans=new char[1024];

// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
printf("--> Initializing Winsock...\n*** Version: %s\n", wsaData.szDescription);
if (iResult != 0) {
    printf("*** Could not initialize Socket.\n*** Error code: %d", iResult);
    return 1;
}

ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

// Resolve the server address and port
iResult = getaddrinfo("127.0.0.1", DEFAULT_PORT, &hints, &result);
printf("--> Setting server address...\n");
printf("--> local ip: 127.0.0.1 at port: %s...\n",DEFAULT_PORT);
if ( iResult != 0 ) {
    printf("*** Error in setting server address.\n*** Error code: %d", iResult);
    WSACleanup();
    return 1;
}

// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
    // Create a SOCKET for connecting to server
    printf("--> Creating client socket object...\n");
    ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
    if (ConnectSocket == INVALID_SOCKET) {
        printf("*** Error creating socket.\n*** Error code: %d\n",WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }
    if(setsockopt(ConnectSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)new int(1000), sizeof(int))){
        WSACleanup();
        //strcpy(recvbuf, "EX_95");
        return -5;    //    Error setting recv timeout.
    }
    // Connect to server.
    iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        closesocket(ConnectSocket);
        ConnectSocket = INVALID_SOCKET;
        continue;
    }
    printf("*** Client ready *** \n\n");
    break;
}

freeaddrinfo(result);

if (ConnectSocket == INVALID_SOCKET) {
    printf("*** Unable to connect to server!\n");
    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);

    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);


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 with error: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}*/

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 with error: %d\n", WSAGetLastError());


// cleanup
closesocket(ConnectSocket);
WSACleanup();
system("pause");
return 0;
}

服务器:

// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#include <iostream>
#include <Winsock2.h>
#include <Ws2tcpip.h>
#include <string>

#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

const char* DEFAULT_PORT = "27015";
const int DEFAULT_BUFLEN = 1024;

using namespace std;

int main(){
//Connection Variables.
WSADATA wsaData;
ADDRINFOA *result = NULL, hints;
SOCKADDR *clientInfo = NULL;
int iResult;
//Receive Variables.
char recvBuff[DEFAULT_BUFLEN];
int recvBuffLen = DEFAULT_BUFLEN;
int iSendResult;
//Server/Client sockets.
SOCKET ListenSocket = INVALID_SOCKET; //SOCKET for the server to listen for client connections.
SOCKET ClientSocket = INVALID_SOCKET;

//Initialize Winsock
iResult = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
printf("--> Initializing Winsock...\n*** Version: %s\n", wsaData.szDescription);
if ( iResult != 0 ){
    printf("*** Could not initialize Socket.\n*** Error code: %d", iResult);
    return 1;
}
//Initialize hints allocated memory.
ZeroMemory( &hints, sizeof( hints ) );

hints.ai_family   = AF_INET;     //AF_INET is used to specify the IPv4 address family.
hints.ai_socktype = SOCK_STREAM; //SOCK_STREAM is used to specify a stream socket.
hints.ai_protocol = IPPROTO_TCP; //IPPROTO_TCP is used to specify the TCP protocol.
hints.ai_flags    = AI_PASSIVE;  //AI_PASSIVE The socket address will be used in a call to the bind function.   

printf("--> Getting address info from server...\n");
iResult = getaddrinfo( NULL, DEFAULT_PORT, &hints, &result );
if( iResult != 0 ){
    printf("*** Error in getting address info from server.\n*** Error code: %d", iResult);
    WSACleanup();
    return 2;
}

printf("--> Creating server socket object...\n");
//printf("Create socket to ip: %s at port: %s\n", inet_ntoa(((SOCKADDR_IN*)(result->ai_addr))->sin_addr),DEFAULT_PORT);
ListenSocket = socket( result->ai_family, result->ai_socktype, result->ai_protocol );
if( ListenSocket == INVALID_SOCKET ){
     printf("*** Error creating socket.\n*** Error code: %d\n",WSAGetLastError());
    freeaddrinfo( result );
    WSACleanup();
    return 3;
}

//BIND()->Associates a local address to a socket.
printf("--> Bind listen object to local ip: 127.0.0.1 at port: %s...\n",DEFAULT_PORT);
iResult = bind( ListenSocket, result->ai_addr, result->ai_addrlen );
if( iResult == SOCKET_ERROR ){
    printf("*** Binding failed.\n*** Error code: %d\n", WSAGetLastError());
    closesocket( ListenSocket );
    freeaddrinfo( result );
    WSACleanup();
    return 4;
}
freeaddrinfo(result);

printf("*** Server ONLINE: Listening...\n\n");
iResult = listen( ListenSocket, SOMAXCONN );
if( iResult == SOCKET_ERROR ){
    printf("*** Failed start listening.\n*** Error code: %d\n",WSAGetLastError());
    closesocket( ListenSocket );
    freeaddrinfo( result );
    WSACleanup();
    return 5;
}

for(;1;){   
   ClientSocket = accept( ListenSocket,clientInfo, NULL );
   if( ClientSocket == INVALID_SOCKET ){
        printf("*** Failed accepting connection from client.\n*** Error code: %d\n",WSAGetLastError());
        continue;
   }
   else{
        printf("--> Connection accepted from client.\n");
        iResult=1;
        while(iResult > 0){
            iResult = recv( ClientSocket, recvBuff, recvBuffLen, 0 );
            if( iResult > 0 ){
                printf("--> Message received: %s\n--> Total: %d\n", recvBuff, iResult);
            }
        }
        iSendResult = send( ClientSocket, "Answer\0", DEFAULT_BUFLEN, 0 );
        if( iSendResult == SOCKET_ERROR ){
            printf("*** Sending data failed.\n*** Error code: %d\n",WSAGetLastError());
            continue;
        }
        else{
            printf("--> Sent: %d bytes\n", iSendResult);
        }
        printf("*** Closing connection... \n\n");
        iResult = shutdown( ClientSocket, SD_BOTH );
        if( iResult == SOCKET_ERROR ){
            printf("*** Shutdown client failed.\nError code: %d\n",WSAGetLastError());
            closesocket( ClientSocket );
            WSACleanup();
            return 9;
        }
  }
 }
 closesocket( ClientSocket );
 WSACleanup();
 system("pause");
 return 0;
}

提前感谢!!!!尼古拉斯·米兰达S。

共有1个答案

逑沛
2023-03-14

当客户机等待响应时,服务器被阻塞在recv调用中,因此无法发送任何内容。您的接收方超时为1秒,因此1秒后客户机将生成超时错误(WSAETIMEDOUT==10060)。

关闭时,只指定SD_SEND,这样连接就不会关闭,但会导致服务器退出recv,从而能够发送响应。

注意:recv将阻塞,直到接收到流套接字(SOCK_STREAM)的内容。您应该查看select()函数,看看在调用recv之前如何“查看”是否有可用的数据。

下面是一个使用SELECT的示例:

    fd_set fds;
    timeval tv;
    tv.tv_sec = 5000;
    fds.fd_count = 1;
    fds.fd_array[0] = ClientSocket;

    int select_result = select(1, &fds, NULL, NULL, &tv);

如果select_result==0,则存在超时。否则,fd_set中的一个套接字已经准备好了数据。这里只有一个,它在select调用中被指定为readfds。

您需要重新安排应用程序,以便您有一些事件(例如,接收第三条消息,或者某个超时而没有接收任何内容,或者消息本身中的一些内容),从而导致服务器发送响应。您可以使用第二个线程作为响应,但这超出了我在一个简短的回答中所能显示的范围。

 类似资料:
  • 我试图用一个Java客户机建立一个简单的Boost ASIO服务器。 我能够发送和成功接收字符串之间的两个。然而,当我试图发送双值时,只有垃圾在另一端出来。 下面是显示基本设置的独立代码(使用C++Boost ASIO服务器和Java客户端)。当它们运行时,它们执行以下四个顺序任务: null

  • 我正在创建我的产品,并与这个问题。有一天,我设置了Socket.io,一切都很好。第二天,我将服务器和客户端从http迁移到HTTPS。迁移后客户端和服务器端仍然连接,但不能从客户端发射到服务器,从服务器发射到客户端。 我的ssl证书位于和中,它们加载正确。运行在上的服务器 我的示例react组件。我的react应用程序运行在上。HTTPS连接良好,工作良好。 我该怎么办?也许我在中错误地使用了s

  • 我对gRpc很陌生,已经开始探索它们的基础知识(在C语言中)。我想获得有关如何发送心跳以检查客户端/服务器是否仍处于连接状态以及在断开连接时采取恢复措施的指导。任何示例或参考任何文档/文章都将有助于我开始学习。谢谢

  • 本文向大家介绍C#利用服务器实现客户端之间通信,包括了C#利用服务器实现客户端之间通信的使用技巧和注意事项,需要的朋友参考一下 先来讲述下我自己对于整个Socket通信过程的理解,毕竟初学,说错见谅,知道错了会改正~  首先在服务端新建一个serverSocket,对其进行初始化(一般包含AddressFamily:IP地址类型,SocketType:Socket传输数据方式,ProtoType:

  • 我正在尝试使用react和express和socket.io建立一个聊天应用程序,但在尝试使用socket.io连接客户端和服务器时,我遇到了这个错误(我使用的是Firefox,我在所有其他浏览器上遇到了同样的错误) Firefox无法连接到ws:/localhost:5000/socket.io/?eio=4&transport=websocket处的服务器。 加载页面时,与ws://local

  • 为什么每个站点都解释说,在SSE中,客户端和服务器之间只有一个连接保持打开状态“在SSE中,客户端发送一个标准的HTTP请求,请求一个事件流,服务器最初以标准的HTTP响应进行响应,并保持连接打开” 然后,当服务器决定它可以向客户机发送数据时,我正在尝试实现SSE,我会看到每隔几秒钟发送一次fiddler请求 对我来说,这感觉像是长时间的轮询,没有一个连接保持打开。