gpt4 book ai didi

c++ - 为什么我的 C++ Winsock 服务器无法从客户端接收数据?

转载 作者:行者123 更新时间:2023-11-28 03:16:57 25 4
gpt4 key购买 nike

我尝试运行一个简单的客户端和服务器程序。 Therin,我试图从客户端向服务器发送消息并让服务器回显该消息。问题是每次我都会收到 10057 错误。我不知道为什么。

这是服务器代码

#include <stdio.h>
#include <winsock2.h>
#include <stdlib.h>

#define MYBUFSIZE 128


void shutdown_close(SOCKET s)
{

// Tell the operating system the socket is
// about to be closed
shutdown(s, SD_BOTH);

// close the socket….
closesocket(s);
}

void shutdown_close_and_exit(SOCKET s)
{
shutdown_close(s);
exit(1);
}

void main(int argc, char *argv[])
{
SOCKET srvr_socket;
struct sockaddr_in srvr_addr;
struct sockaddr_in clnt_addr; // Client address
int addr_len = 0;
WSADATA wsaData;
char recv_buf[MYBUFSIZE];
int recv_msg_len;
short portnum;


if (argc == 2)
{
portnum = atoi(argv[1]);
printf("Setting port number to %d\n", portnum);

} else
{
fprintf(stderr, "Usage: %s port_num_to_listen_at\n",
argv[0]);
exit(1);
}

// Init Winsock
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
{
fprintf(stderr, "Error: WSAStartup() failed");
exit(1);
}

// Create UDP datagram socket for incoming connections
if ((srvr_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR)
{
fprintf(stderr, "Error: socket() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(srvr_socket);
}

// Construct local address structure
memset(&srvr_addr, 0, sizeof(srvr_addr));
srvr_addr.sin_family = AF_INET;
srvr_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Accept messages on any network interface.
srvr_addr.sin_port = htons(portnum); // Bind the port number specified on the command line.

/* Bind to the local address */
if (bind(srvr_socket, (struct sockaddr *) &srvr_addr, sizeof(srvr_addr)) == SOCKET_ERROR)
{
fprintf(stderr, "Error: socket() failed with error %d", WSAGetLastError());
shutdown_close_and_exit(srvr_socket);
}

listen(srvr_socket, SOMAXCONN);

// Loop forever
for (;;)
{
// Set the size of the in-out parameter, where the client address
// and port number will be stored by the OS
addr_len = sizeof(clnt_addr);

if(accept(srvr_socket,(SOCKADDR *) &srvr_addr, &addr_len) == SOCKET_ERROR)
{
fprintf(stderr, "Error: accept() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(srvr_socket);
}

// Receive message from client
if ((recv_msg_len = recvfrom(srvr_socket, recv_buf, MYBUFSIZE, 0,
(struct sockaddr *) &clnt_addr, &addr_len )) == SOCKET_ERROR) {

fprintf(stderr, "Error: recvfrom() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(srvr_socket);
} else {
printf("Received message of size %d: %s\n from client %s:%d\n",
recv_msg_len, recv_buf, inet_ntoa(clnt_addr.sin_addr),
ntohs(clnt_addr.sin_port));
}


// 'echo' message back to client
if ((recv_msg_len = sendto(srvr_socket, recv_buf, recv_msg_len, 0,
(struct sockaddr *) &clnt_addr, addr_len )) == SOCKET_ERROR) {

fprintf(stderr, "Error: sendto() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(srvr_socket);
} else {
printf("Echo'd message %s to client %s:%d\n",
recv_buf, inet_ntoa(clnt_addr.sin_addr),
ntohs(clnt_addr.sin_port));
}
}
}

这是客户端代码:

#include <stdio.h>
#include <winsock2.h>
#include <stdlib.h>

#define BUFSIZE 128 /* Size of receive buffer */


void shutdown_close(SOCKET s)
{

// Tell the operating system the socket is
// about to be closed
shutdown(s, SD_BOTH);

// close the socket….
closesocket(s);
}

void shutdown_close_and_exit(SOCKET s)
{
shutdown_close(s);
exit(1);
}

void main(int argc, char *argv[])
{
SOCKET servSock;
struct sockaddr_in srvr_addr;
struct sockaddr_in recv_addr;
int addr_len = 0;
WSADATA wsaData;
char mesg_buf[BUFSIZE];
char *mesg_to_send;
long server_IP;
short portnum;


if (argc == 4)
{
portnum = atoi(argv[1]);
printf("Setting port number to %d\n", portnum);

server_IP = inet_addr(argv[2]);
printf("Target server IP address is %s\n", argv[2]);

mesg_to_send = argv[3];

} else
{
fprintf(stderr, "Usage: %s server_port_num server_IP_address message_to_send\n",
argv[0]);
exit(1);
}

// Init Winsock
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
{
fprintf(stderr, "Error: WSAStartup() failed with error %d\n", WSAGetLastError());
exit(1);
}

// Create socket for incoming connections
if ((servSock = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR)
{
fprintf(stderr, "Error: socket() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(servSock);
}

// Construct local address structure
// with server address. This is like
// addressing the envelope of a letter.
memset(&srvr_addr, 0, sizeof(srvr_addr));
srvr_addr.sin_family = AF_INET;
srvr_addr.sin_addr.s_addr = server_IP;
srvr_addr.sin_port = htons(portnum);

if ( connect(servSock, (SOCKADDR*) &srvr_addr, sizeof(srvr_addr)) == SOCKET_ERROR)
{
fprintf(stderr, "Error: connect() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(servSock);
}

// Set the size of the in-out parameter
addr_len = sizeof(recv_addr);

if (sendto(servSock, mesg_to_send, strlen(mesg_to_send) + 1, 0,
(sockaddr *) &srvr_addr, addr_len) == SOCKET_ERROR)
{
fprintf(stderr, "Error: sendto() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(servSock);
}

printf("Send message %s to server at %s:%d\n", argv[3], argv[2], portnum);

// Sleep 1 full second to allow message to get to server and be sent back...
Sleep(1000);

addr_len = sizeof(recv_addr);

if (recvfrom(servSock, mesg_buf, BUFSIZE, 0,
(sockaddr *) &recv_addr, &addr_len) == SOCKET_ERROR)
{
fprintf(stderr, "Error: recvfrom() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(servSock);
}

printf("Received message %s from %s:%d\n", mesg_buf,
inet_ntoa(recv_addr.sin_addr), ntohs(recv_addr.sin_port));

// close socket gracefully
shutdown_close(servSock);

}

有人能看出代码有什么问题吗?

最佳答案

对于 SOCK_STREAM 套接字,您应该使用 recv,而不是 recvfrom

(但是那是一大堆代码,没有明显的迹象表明哪里出了问题,所以除此之外可能还有其他问题。)

关于c++ - 为什么我的 C++ Winsock 服务器无法从客户端接收数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16605317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com