connect() 上的参数 errno 无效
我正在编写一个基本上执行服务器-客户端关系的程序。 当我运行客户端时(使用相关参数,当服务器已经运行时),我收到以下 errno 消息:
无效参数
这是相关代码的一部分:
if(argc != NUM_OF_ARGS)
{
fprintf(stderr,"usage: Client <CLIENT NAME> <SERVER ADDRESS> <SERVER PORT>\n");
exit(1);
}
int serverPort = atoi(argv[3]);
if(serverPort == 0){
cerr << serverPort<<endl;
fprintf(stderr,"bad arguments\n");
exit(1);
}
//update the local machine's addr
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(serverPort);
inet_aton(argv[2],&(server_addr.sin_addr));
memset(&(server_addr.sin_zero),'\0',8);
int fd = socket(PF_INET,SOCK_STREAM,0);
if(fd < 0){
fprintf(stderr,strerror(errno));
exit(1);
}
if(connect(fd,(sockaddr*)&server_addr,BUFFERSIZE) < 0){
fprintf(stderr,strerror(errno));//My program gets here and exits.
exit(1);
}
另一个注意事项:我作为参数给出的地址的格式为:132.65.151.68
我在这里做错了什么?
I'm writing a program that basically perform server-client relationship.
When i run my client (with the relevant params, when the server is already running), i get the following errno message:
Invalid argument
Here's a part of the relevant code:
if(argc != NUM_OF_ARGS)
{
fprintf(stderr,"usage: Client <CLIENT NAME> <SERVER ADDRESS> <SERVER PORT>\n");
exit(1);
}
int serverPort = atoi(argv[3]);
if(serverPort == 0){
cerr << serverPort<<endl;
fprintf(stderr,"bad arguments\n");
exit(1);
}
//update the local machine's addr
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(serverPort);
inet_aton(argv[2],&(server_addr.sin_addr));
memset(&(server_addr.sin_zero),'\0',8);
int fd = socket(PF_INET,SOCK_STREAM,0);
if(fd < 0){
fprintf(stderr,strerror(errno));
exit(1);
}
if(connect(fd,(sockaddr*)&server_addr,BUFFERSIZE) < 0){
fprintf(stderr,strerror(errno));//My program gets here and exits.
exit(1);
}
Another note: the address i'm giving as an argument is of the format: 132.65.151.68
What am i doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
connect 期望第二个参数指向的结构体的大小作为第三个参数。有关详细信息,请参阅
man 2 connect
。connect
expects as third parameter the size of the struct that the second argument points to. Seeman 2 connect
for details.