发送到:无效参数错误
Posted
技术标签:
【中文标题】发送到:无效参数错误【英文标题】:Sendto: invalid argument error 【发布时间】:2018-08-12 15:27:32 【问题描述】:我正在为一个大学项目创建一个并发 UDP 套接字。客户端等待stdin
命令,然后使用函数child_job 创建一个新进程和一个新套接字。服务器从客户端接收命令,创建一个初始化新套接字的新进程,然后尝试将字符串发送回客户端。问题是服务器的第一个sendto创建错误:
参数无效
我不知道为什么。谁能帮帮我?
客户端代码:
#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
【问题讨论】:
【参考方案1】:函数“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);
【讨论】:
刚刚编辑,如果我从 memset 评论初始化我有:“地址已在使用中”,如果我不评论我有相同的“无效参数”错误 这是因为你上次跑步!使用套接字后,您应该关闭套接字以避免在将来运行时出现此错误。使用“关闭(sockfd);”当您不再需要套接字时。在等待几秒钟让操作系统有时间释放地址后尝试运行程序。 如果您在同一台主机上运行客户端和服务器,请注意您不应将相同的端口号作为输入。使用不同的端口!以上是关于发送到:无效参数错误的主要内容,如果未能解决你的问题,请参考以下文章
facebook 发送 API 错误代码:100 API 错误描述:无效参数错误消息:“链接”无效
类型错误:使用 flask_sqlalchemy 时发送到 create_engine() 的参数“pool_size”无效