c - C 套接字代码生成 ERRNO 24 消息
问题描述
我编写了一个 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);
}
}
你有它。这段代码在收到的前几千条消息中运行良好,然后吐出 ERRNO 24。有人知道我做错了什么吗?
解决方案
在退出函数 readMsgAndReply 之前,您需要关闭套接字。
close(connection->sock);
推荐阅读
- javascript - 使用 JAVASCRIPT 提交表单后显示 DIV
- excel - 扩展数组的函数
- java - 如何修复此索引超出范围错误
- django - Django:多个对象的 ModelForm save() 方法在一个日期范围内
- c# - C# Blazor 服务器:使用 INotifyPropertyChanged 显示实时数据
- ios - 可访问性返回到第一个元素 ios swift
- node.js - Mongoose:更新后获取文档的新版本
- azure - Azure IoT 中心 - 无法创建分层部署
- android - MediaCodec自适应播放Failed to get port format错误
- python - 在分区日期集上使用 Dask 时 _metadata 文件的重要性