【问题标题】:Socket server close the connection for command套接字服务器关闭命令的连接
【发布时间】:2014-03-04 20:52:21
【问题描述】:

我尝试修改以下代码,它是一个简单的多线程套接字服务器,如果客户端发出 QUIT 命令,它实际上会关闭连接。

/*
    C socket server example, handles multiple clients using threads
*/

#include<stdio.h>
#include<string.h>    //strlen
#include<stdlib.h>    //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>    //write
#include<pthread.h> //for threading , link with lpthread

//the thread function
void *connection_handler(void *);

int main(int argc , char *argv[])
{
    int socket_desc , client_sock , c , *new_sock;
    struct sockaddr_in server , client;

    //Create socket
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8888 );

    //Bind
    if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
    {
        //print the error message
        perror("bind failed. Error");
        return 1;
    }
    puts("bind done");

    //Listen
    listen(socket_desc , 3);

    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);


    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);
    while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
    {
        puts("Connection accepted");

        pthread_t sniffer_thread;
        new_sock = malloc(1);
        *new_sock = client_sock;

        if( pthread_create( &sniffer_thread , NULL ,  connection_handler , (void*) new_sock) < 0)
        {
            perror("could not create thread");
            return 1;
        }

        //Now join the thread , so that we dont terminate before the thread
        //pthread_join( sniffer_thread , NULL);
        puts("Handler assigned");
    }

    if (client_sock < 0)
    {
        perror("accept failed");
        return 1;
    }

    return 0;
}

/*
 * This will handle connection for each client
 * */
void *connection_handler(void *socket_desc)
{
    //Get the socket descriptor
    int sock = *(int*)socket_desc;
    int read_size;
    char *message , client_message[2000];

    //Send some messages to the client
    message = "Greetings! I am your connection handler\n";
    write(sock , message , strlen(message));

    message = "Now type something and i shall repeat what you type \n";
    write(sock , message , strlen(message));

    //Receive a message from client
    while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
    {
        //Send the message back to client
        write(sock , client_message , strlen(client_message));
    }

    if(read_size == 0)
    {
        puts("Client disconnected");
        fflush(stdout);
    }
    else if(read_size == -1)
    {
        perror("recv failed");
    }

    //Free the socket pointer
    free(socket_desc);

    return 0;
}

我所做的是在连接处理程序线程(有效)的 while 循环中插入一个中断,然后在
免费(socket_desc); 我添加了 关闭(socket_desc); 以及关闭连接。这会产生一个编译器错误,并且当它到达这一点时服务器也会崩溃。

com.c:在函数“connection_handler”中:com.c:134:5:警告:通过 'close' 的参数 1 从指针生成整数而不进行强制转换 [默认启用]

【问题讨论】:

  • 我看不到休息时间。显示您的真实代码,而不是示例。
  • 无论 socket_desc 是什么,都不能成为 free() 和 close() 的有效参数。那么它是一个int 文件描述符,还是指向某些信息结构的指针? -1 用于发布一堆代码,其中不包含导致您询问的编译警告的行。

标签: c linux sockets


【解决方案1】:

因为(在您的第二个代码块中)您似乎使用 sock_desc 作为 void 指向您所知道的实际上是 int 的指针,而 sock 作为通过取消引用获得的 int ,你应该打电话

close(sock);

与您使用的其他套接字函数一样,close() 将文件描述符(您可以将其视为系统提供的稍微任意的“文件编号”)作为其参数,而不是指针。

【讨论】:

    猜你喜欢
    • 2013-02-17
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2014-08-11
    相关资源
    最近更新 更多