gpt4 book ai didi

c++ - UDP服务器无法发送回复,并且sendto返回-1,并且errno为22

转载 作者:行者123 更新时间:2023-12-01 14:40:13 26 4
gpt4 key购买 nike

UDP客户端/服务器代码出现此问题。此代码是从https://www.geeksforgeeks.org/udp-server-client-implementation-c/复制并进行了最小限度的修改,以修复编译错误并在服务器代码中显示errno。

// Client side implementation of UDP client-server model 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define PORT 8080
#define MAXLINE 1024

// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from client";
struct sockaddr_in servaddr;

// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}

memset(&servaddr, 0, sizeof(servaddr));

// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY;

int n; socklen_t len;

sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &servaddr,
sizeof(servaddr));
printf("Hello message sent.\n");

n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, (struct sockaddr *) &servaddr,
&len);
buffer[n] = '\0';
printf("Server : %s\n", buffer);

close(sockfd);
return 0;
}

// Server side implementation of UDP client-server model 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>

#define PORT 8080
#define MAXLINE 1024

// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];

struct sockaddr_in servaddr, cliaddr;

// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}

memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));

// Filling server information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);

// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr,
sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}

int n; socklen_t len;
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, ( struct sockaddr *) &cliaddr,
&len);
buffer[n] = '\0';
printf("Client : %s\n", buffer);
int str_len = 0;
sprintf(buffer, "%s", "hello from server");
str_len = strlen(buffer);
printf("Sendinger buffer: <%s>, size=%d\n", buffer, (int) (str_len+1));
n = sendto(sockfd,buffer, (int)(str_len+1),
MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
len);

printf("n=%d, errno=%d\n", n, errno);

printf("Hello message sent.\n");

return 0;
}

我用简单的命令编译了服务器和客户端:
g++ udpserv.cpp -o udpserv
g++ client.cpp -o client

我所看到的是,客户端成功地将消息发送到服务器,服务器从客户端打印了消息,并且当服务器将回复发送回客户端时,它从sendto得到-1,而errno为22。客户端保持挂起状态等待回复信息。

这是服务器的输出:
[user@localhost udpserver]$ ./udpserv 
Client : Hello from client
Sendinger buffer: <hello from server>, size=18
n=-1, errno=22
Hello message sent.
[user@localhost udpserver]$

这是客户端的输出:
[user@localhost client]$./client 
Hello message sent.

最佳答案

错误22EINVAL,这意味着您传递了无效的参数。

在这种情况下,无效参数可能是未初始化的len参数:在调用sizeof cliaddr之前,需要将其初始化为recvfrom

也可能是传递给MSG_CONFIRMsendto标志,在您的应用程序中没有意义。发送时传递零标志。

关于c++ - UDP服务器无法发送回复,并且sendto返回-1,并且errno为22,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59254012/

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