gpt4 book ai didi

c - Sendto:参数无效错误

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

我正在为一个大学项目创建一个并发 UDP 套接字。客户端等待 stdin 命令,然后使用函数 child_job 创建一个新进程和一个新套接字。服务器接收来自客户端的命令,创建一个新进程来初始化新套接字,然后尝试将字符串发送回客户端。问题是服务器的第一个 sendto 创建了错误:

Invalid argument

我不明白为什么。有人可以帮我吗?

客户端代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>


void child_job(int sockfd, struct sockaddr_in servaddr, char* buffer){

int n = sendto(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
if(n < 0){

perror("Error while sending roba to server\n");
exit(1);

}

printf("Connected to server\n");


char bufferaccio[128];

int length = sizeof(struct sockaddr);

int m = recvfrom(sockfd, bufferaccio, sizeof(bufferaccio), 0, (struct sockaddr*)&servaddr, (socklen_t *)&length);

if(m < 0){

perror("Error while receiving from server\n");
exit(1);

}



char stringa[128] = "I am a string from client";

m = sendto(sockfd, stringa, strlen(stringa), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));

if(m < 0){

perror("Error while sending stringa to server\n");
exit(1);

}

exit(1);

}


int main(int argc, char** argv){

int sockfd;
struct sockaddr_in str;

if(argc < 3){

fprintf(stderr, "Usage: %s portno ip\n", argv[0]);
exit(1);

}

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0){

perror("Error while creating socket\n");
exit(1);

}


memset((void*)&str, 0, sizeof(str));
str.sin_family = AF_INET;
str.sin_port = htons(atoi(argv[1]));

if(inet_pton(AF_INET, argv[2], &str.sin_addr)< 0){

perror("Error while inet_pton\n");
exit(1);

}

pid_t pid;

char buffer[128];

printf("Write something to send\n");
scanf("%s", buffer);

pid = fork();

if(pid == 0){

printf("I am a children with pid %d\n", getpid());

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0){

perror("Error while creating socket in the child process\n");
exit(1);

}

child_job(sockfd, str, buffer);



}else if(pid < 0){

perror("Error while creating child process\n");
exit(1);

}

wait(NULL);

exit(1);


}

服务器代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/types.h>

void initialize_newSocket(int* sock_fd, struct sockaddr_in *servaddr){

int sockfd;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0){

perror("Error while creating new socket\n");
exit(1);

}

int ret = bind(sockfd, (struct sockaddr *)servaddr, sizeof(*servaddr));
if(ret < 0){

perror("Error while binding in son\n");
exit(1);

}

*sock_fd = sockfd;


}


void child_server_job(){

struct sockaddr_in addr;

int sockfd;

memset((void *)&addr, 0, sizeof(addr));

addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(0);

initialize_newSocket(&sockfd, &addr);

char string[128] = "I am a string";

socklen_t fromlen = sizeof(addr);

int n = sendto(sockfd, string, strlen(string), 0, (struct sockaddr *)&addr, fromlen);

if(n < 0){

perror("Error while sending something\n");
exit(1);

}

int lenght = sizeof(struct sockaddr);


n = recvfrom(sockfd, string, strlen(string), 0, (struct sockaddr *)&addr, (socklen_t *)&lenght);

if(n < 0){

perror("Error while receveing data from client\n");
exit(1);

}

printf("I received this string %s\n", string);

exit(1);

}


int main(int argc, char** argv){

if(argc < 2){

fprintf(stderr, "Usage: %s portno\n", argv[0]);
exit(1);

}

int sockfd;
struct sockaddr_in addr;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0){

perror("Error while creating socket\n");
exit(1);

}

addr.sin_family = AF_INET;
addr.sin_port = htons(atoi(argv[1]));
addr.sin_addr.s_addr = INADDR_ANY;

int retBind = bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
if(retBind < 0){

perror("Error while binding socket\n");
exit(1);

}

char bufferone[129];

int length = sizeof(struct sockaddr);

int n = recvfrom(sockfd, bufferone, sizeof(bufferone), 0, (struct sockaddr*)&addr, (socklen_t *)&length);

if(n < 0){

perror("Error while receiving roba from client\n");
exit(1);

}

printf("Printing: %s\n", bufferone);

pid_t pid = fork();

if(pid == 0){

printf("I am the child with pid %d\n", getpid());

child_server_job(sockfd, addr);


exit(1);


}else if(pid < 0){

perror("Error while forking new process\n");
exit(1);

}

wait(NULL);

exit(1);



}

客户端输出:

Write something to send
string
I am a children with pid 5756
Connected to server

服务器输出:

Printing: string
I am the child with pid 5757
Now it's my turn to send something
Error while sending something
: Invalid argument

最佳答案

函数“child_server_job”不使用传递的参数!

更改child_server_job以提供参数并使用传递的参数。

编辑

void child_server_job(){

struct sockaddr_in addr;

int sockfd;

memset((void *)&addr, 0, sizeof(addr));

addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(0);

initialize_newSocket(&sockfd, &addr);

致:

void child_server_job(int* sock_fd, struct sockaddr_in *servaddr){

struct sockaddr_in addr = *servaddr;

int sockfd = *sock_fd;

/*memset((void *)&addr, 0, sizeof(addr));

addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(0);

initialize_newSocket(&sockfd, &addr); */

并且

child_server_job(sockfd, addr);

致:

child_server_job(&sockfd, &addr);

关于c - Sendto:参数无效错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51810233/

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