간단한 멀티 스레드 소켓 서버 인 다음 코드를 수정하려고합니다. 클라이언트가 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;
}
는
무료 (socket_desc) 전에 다음 (작동) 연결 핸들러 스레드 while 루프로 나누기를 삽입하는 것입니다; 를 추가했습니다. close (socket_desc); 도 연결을 닫습니다. 이로 인해 컴파일러 오류가 발생하고이 시점에 도달하면 서버가 중단됩니다.
는com.c : 기능의 'connection_handler'com.c : 134 : 5 :주의 : 인수 1 통과가 '확대'[기본적으로 사용] 캐스트 없이 포인터의 정수한다
휴식 시간이 없습니다. 예제가 아닌 실제 코드를 보여주십시오. – deviantfan
socket_desc가 무엇이든 free()와 close()에 유효한 인수가 될 수 없습니다. 그래서 그것은 "int"파일 디스크립터인가, 아니면 어떤 정보 구조에 대한 포인터인가? 컴파일 경고를 일으키는 행을 포함하지 않는 코드 묶음을 게시하면 -1이됩니다. –