winsock 服务器同时发送和接收
Posted
技术标签:
【中文标题】winsock 服务器同时发送和接收【英文标题】:winsock server send and receive simultaniously 【发布时间】:2021-02-10 18:23:08 【问题描述】:我是 WinSock 的新手,我正在尝试一些东西。我有相互通信的客户端和服务器程序。如果客户端键入了某些内容,服务器只会将其回显。我希望它们同时接收和发送,所以我将客户端置于非阻塞模式,它工作得还不错。但是当我尝试将服务器置于非阻塞状态时,它会崩溃说recv() == SOCKET_ERROR
。
所以问题是:为什么客户端可以非阻塞工作,而服务器不能?我该如何解决?
TCP_SERVER:
#include <iostream>
#include <WS2tcpip.h>
#include <Windows.h>
#pragma comment (lib,"ws2_32.lib")
using namespace std;
string receive(SOCKET clientSocket, char* buf)
ZeroMemory(buf, 4096);
int bytesReceived = recv(clientSocket, buf, 4096, 0);
string bufStr = buf;
cout << "bytes received: " << bytesReceived << endl;
if (bytesReceived == SOCKET_ERROR)
cerr << "error met recv() in de reciev() functie" << endl;
exit(EXIT_FAILURE);
if (bytesReceived == 0)
cout << "client disconnected" << endl;
exit(EXIT_FAILURE);
return bufStr;
void main()
//initialize winsock
WSADATA wsData;
WORD ver = MAKEWORD(2, 2);
int wsOk = WSAStartup(ver, &wsData);
if (wsOk != 0)
cerr << "can't initialize winsock ABORT";
return;
//create socket
SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
if (listening == INVALID_SOCKET)
cerr << "cant create socket ABORT" << std::endl;
//bind IP adress and port to socket
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(54000);
hint.sin_addr.S_un.S_addr = INADDR_ANY; //could also inet_pton
bind(listening, (sockaddr*)&hint, sizeof(hint));
//tell winsock the socket is for listening
listen(listening, SOMAXCONN);
//wait for connection
sockaddr_in client;
int clientSize = sizeof(client);
SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize);
if (clientSocket == INVALID_SOCKET)
cerr << "somthing went wrong with client socket accept ABORT";
exit(EXIT_FAILURE);
char host[NI_MAXHOST]; //client remote name
char service[NI_MAXSERV]; //service (i.e port) the client is connected on
ZeroMemory(host, NI_MAXHOST);
ZeroMemory(service, NI_MAXSERV);
if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
cout << host << " connected on port " << service << endl;
else
inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
cout << host << " connected on port " << ntohs(client.sin_port) << endl;
//close listening socket
closesocket(listening);
//non blocking socket leads to error
u_long mode = 1; // 1 to enable non-blocking socket
ioctlsocket(clientSocket, FIONBIO, &mode);
//non blocking socket
//while loop: accept and echo message to client
char buf[4096];
string inputTxt;
while (true)
inputTxt = receive(clientSocket,buf);
send(clientSocket, buf, inputTxt.size() + 1, 0);
closesocket(clientSocket);
WSACleanup();
TCP_CLIENT:
#include <iostream>
#include <WS2tcpip.h>
#include <Windows.h>
#pragma comment (lib,"ws2_32.lib")
using namespace std;
string receive(SOCKET clientSocket, char* buf)
ZeroMemory(buf, 4096);
int bytesReceived = recv(clientSocket, buf, 4096, 0);
string bufStr = buf;
cout << "bytes received: " << bytesReceived << endl;
if (bytesReceived == SOCKET_ERROR)
cerr << "error met recv() in de reciev() functie" << endl;
exit(EXIT_FAILURE);
if (bytesReceived == 0)
cout << "client disconnected" << endl;
exit(EXIT_FAILURE);
return bufStr;
void main()
//initialize winsock
WSADATA wsData;
WORD ver = MAKEWORD(2, 2);
int wsOk = WSAStartup(ver, &wsData);
if (wsOk != 0)
cerr << "can't initialize winsock ABORT";
return;
//create socket
SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
if (listening == INVALID_SOCKET)
cerr << "cant create socket ABORT" << std::endl;
//bind IP adress and port to socket
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(54000);
hint.sin_addr.S_un.S_addr = INADDR_ANY; //could also inet_pton
bind(listening, (sockaddr*)&hint, sizeof(hint));
//tell winsock the socket is for listening
listen(listening, SOMAXCONN);
//wait for connection
sockaddr_in client;
int clientSize = sizeof(client);
SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize);
if (clientSocket == INVALID_SOCKET)
cerr << "somthing went wrong with client socket accept ABORT";
exit(EXIT_FAILURE);
char host[NI_MAXHOST]; //client remote name
char service[NI_MAXSERV]; //service (i.e port) the client is connected on
ZeroMemory(host, NI_MAXHOST);
ZeroMemory(service, NI_MAXSERV);
if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
cout << host << " connected on port " << service << endl;
else
inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
cout << host << " connected on port " << ntohs(client.sin_port) << endl;
//close listening socket
closesocket(listening);
/*
//non blocking socket leads to error
u_long mode = 1; // 1 to enable non-blocking socket
ioctlsocket(clientSocket, FIONBIO, &mode);
//non blocking socket
*/
//while loop: accept and echo message to client
char buf[4096];
string inputTxt;
while (true)
inputTxt = receive(clientSocket,buf);
send(clientSocket, buf, inputTxt.size() + 1, 0);
closesocket(clientSocket);
WSACleanup();
【问题讨论】:
【参考方案1】:您没有处理 send()
/recv()
由于 WSAEWOULDBLOCK
错误而失败的情况,这不是致命错误。这只是表示此时没有工作要做,稍后再试。
对于recv()
,这意味着没有可从套接字的接收缓冲区读取的字节。当有字节可供读取时,套接字将处于可读状态,或者对等方已执行优雅断开连接。
对于send()
,表示对端的接收缓冲区已满,无法接收新字节,直到对端读取一些字节以清理缓冲区空间。任何未发送的字节都必须稍后再次传递给send()
。当新字节可以发送到对等方时,套接字将处于可写状态,而当对等方的缓冲区已满时,套接字将处于可写状态。
当您的服务器接受客户端并尝试从该客户端发送 receive()
时,recv()
将继续以 WSAEWOULDBLOCK
失败,直到客户端实际发送某些内容。
因此,您需要正确处理WSAEWOULDBLOCK
并根据需要重试。或者更好的是,使用select()
(或WSAAsyncSelect()
,或WSAEventSelect()
,或Overlapped I/O)来检测套接字的实际状态以了解何时send()
/recv()
可以安全调用而不会导致 WSAEWOULDBLOCK
错误。
【讨论】:
以上是关于winsock 服务器同时发送和接收的主要内容,如果未能解决你的问题,请参考以下文章
如何用UDP 实现服务器广播发送,并接收客户端点对点回复,用winsock实现,写出关键代码,谢谢~
将 JavaScript WebSocket 连接到 C winsock