2014-03-04 102 views
-1

我試圖修改下面的代碼,這是一個簡單的多線程套接字服務器,如果客戶端發出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)之前;我添加了 close(socket_desc); 以及關閉連接。這會產生一個編譯器錯誤,並且服務器到達這一點時會崩潰。

com.c:在函數 'connection_handler':com.c:134:5:警告:傳遞 說法1 '關閉' 時將指針整數,未作鑄造 [默認啓用]

+1

我沒有看到休息。顯示你的真實代碼,而不是一個例子。 – deviantfan

+0

無論socket_desc是什麼,對free()和close()都不是不合法的參數。那麼,它是一個'int'文件描述符,還是指向某些信息結構的指針?和-1發佈一堆代碼,其中不包括導致您所詢問的編譯警告的行。 –

回答

0

由於(在你的代碼的第二塊),你似乎可以用sock_desc作爲void指針,你知道什麼是真正的int,並sock如提領所獲得的int,你應該叫

close(sock); 

由於close()函數與其他使用的套接字函數類似,它將一個文件描述符(可以認爲是系統提供的稍微任意「文件編號」)作爲其參數,而不是指針。