【发布时间】:2016-05-25 10:58:07
【问题描述】:
我正在尝试编写一个简单的聊天应用程序,其中客户端从服务器接收字符串并发送一条新消息,该消息将传递给下一个连接的客户端。我是套接字编程的新手,我真的不明白为什么会发生这个错误。客户端发送他的缓冲区后,服务器通知accept() 上的错误(返回-1)和错误的文件描述符。这是在服务器上运行的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg) {
perror(msg);
exit(1);
}
int main (int argc, char *argv[] ) {
if ( argc < 2 ) {
error("ERROR, no port provided\n");
}
int sockfd;
struct sockaddr_in serv_addr, cli_addr;
int cli_len, portno;
char message[256];
pid_t pid;
int n;
portno = atoi( argv[1] );
bzero( (char*)&serv_addr, sizeof(serv_addr) );
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons( portno );
sockfd = socket( AF_INET, SOCK_STREAM, 0 );
n = bind ( sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr) );
if ( n < 0 ) {
error("ERROR on binding\n");
}
listen( sockfd, 5 );
do {
cli_len = sizeof(cli_addr);
int newsockfd;
newsockfd = accept( sockfd,(struct sockaddr*)&cli_addr,(socklen_t *)&cli_len);
if ( newsockfd < 0 ) {
printf("%i", newsockfd);
error("ERROR on accept\n");
}
pid = fork();
if( pid == 0 ) {
close( sockfd );
sockfd = -1;
n = send( newsockfd, message, strlen(message), 0);
if ( n < 0 ) error("ERROR sending\n");
bzero(message, 256);
n = recv( newsockfd, message, 256, 0);
if ( n < 0 ) error("ERROR receiving\n");
close( newsockfd );
newsockfd = -1;
}
}
while( 1 );
return 0;
}
【问题讨论】:
-
cli_len 应该是 socklen_t 类型,而不是 int。类型转换隐藏了 x86_64 上的潜在问题,其中 int 可以是 32 位,但其他与大小相关的类型可以是 64 位。
-
您没有正确报告错误。您需要致电
perror()或strerror()在 做任何其他可能改变errno的事情,例如printf()。修复,重新测试,... -
问题2,完成后孩子应该调用exit,否则孩子和父母都会在accept()中争吵;-)