问题描述
我已经编写了一个C程序,希望将其用作基于TCP / IP的简单服务。该程序必须打开一个监听TCP套接字。每当有人向套接字写入内容时,我的程序就必须读取消息,进行一些处理,然后再发送不同的消息。该程序必须永远运行。
该程序编译并运行。 (我使用的是gcc 7.4.0)问题是,成功处理了几千封邮件而没有任何问题之后,程序开始打印以下内容:
...
Connection FAILED :: Value of errno: 24
Connection FAILED :: Value of errno: 24
Connection FAILED :: Value of errno: 24
Connection FAILED :: Value of errno: 24
...
Errno 24的意思是“打开了太多的文件描述符”,这使我想知道我的程序是否正在分配某些内容(套接字?内存?),而后又没有适当地对其进行释放。但是我无法发现我要去哪里。
让我告诉我我的代码。我遵循了我喜欢的教程,他们在一个结构中收集了所有相关的套接字信息:
typedef struct{
int sock;
struct sockaddr address;
socklen_t addr_len;
} connection_t;
main()
设置了套接字,然后在无限循环中侦听它:
int main(int argc,char ** argv) {
int sock = -1;
struct sockaddr_in address;
int port = 12345;
connection_t* connection;
// Create the listening socket
sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (sock <= 0){
fprintf(stderr,"%s: error: cannot create socket\n",argv[0]);
return -3;
}
// Bind socket to port
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
if (bind(sock,(struct sockaddr *)&address,sizeof(struct sockaddr_in)) < 0){
fprintf(stderr,"%s: error: cannot bind socket to port %d\n",argv[0],port);
return -4;
}
// Listen on the port
if (listen(sock,5) < 0){
fprintf(stderr,"%s: error: cannot listen on port\n",argv[0]);
return -5;
}
// We're ready to go...!
printf("%s: ready and listening\n",argv[0]);
while (1){
// Accept incoming connections
connection = (connection_t *)malloc(sizeof(connection_t));
connection->addr_len = 20;
connection->sock = accept(sock,&connection->address,&connection->addr_len);
if (connection->sock <= 0){
// ***********************************************
// *** This is where the error happens!
printf("Connection FAILED :: Value of errno: %d\n ",errno);
// ***********************************************
}
else{
printf("SERVER... New Msg received...\n");
readMsgAndReply( connection );
}
free(connection);
}
return 0;
}
您会注意到,当accept()
无法成功接受新的传入连接时,我会收到“连接失败”消息-我希望我知道为什么。
如果accept()
成功,我的代码将调用readMsgAndReply():
void readMsgAndReply( connection* conn ){
char* buffer;
char* reply = "Your Msg was received!";
int ret,len,replyLen;
long addr = 0;
// First call to read() measures the length of the sent message
ret = read(conn->sock,&len,sizeof(int));
if( ret < 0 ){
printf( "***readMsgAndReply() ERROR: read() error\n" );
}
if( len > 0 ){
addr = (long)((struct sockaddr_in *)&conn->address)->sin_addr.s_addr;
buffer = (char *)malloc((len+1)*sizeof(char));
buffer[len] = 0;
// Second call to read() actually reads the message from the socket
ret = read(conn->sock,buffer,len);
if( ret < 0 ){
printf( "***readMsgAndReply() ERROR: ret < 0\n");
}
else{
printf("***readMsgAndReply() message size :: %d\n",len);
printf("***readMsgAndReply() is :: \"%s\"\n",buffer);
}
// write reply message back to the client
replyLen = write(conn->sock,reply,strlen(reply));
if(replyLen > 0)
printf("===> Reply written,%d bytes sent!\n",replyLen);
else
printf("---> No reply written\n");
free(buffer);
}
}
那里有。这段代码可以很好地处理收到的前几千封邮件,然后发出ERRNO24。有人看到我在做什么错吗?
解决方法
在退出函数readMsgAndReply之前,您需要关闭套接字。
close(connection->sock);