스레드로 만든 서버인데 자꾸 죽네요..

ljksky의 이미지

스레드로 간단 한 에코서버를 만들어 봤는데요..
여러 클라이언트가 접속후 데이타를 주고 받다가..
그중 하나의 클라이언드를 종료 하면 서버도
같이 죽네요...

뭐가 문제인지.......ㅠ.ㅠ

소스 올립니다..한번 봐주세요..

#include <sys/stat.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/file.h>
#include <stdio.h>
#include <sys/socket.h>
#include <strings.h>
#include <signal.h>
#include <netdb.h>
#include <time.h>

void *do_chld(void *);

int main(int argc, char *argv[]){
int server_sockfd, client_sockfd;
int state, client_len;
int pid;
int count=0;

struct sockaddr_in clientaddr, serveraddr;
pthread_t chld_thr;

char buf[255], line[255];
char szBuff[200];

if(argc != 2){
printf("사용법 : %s [port number]\n", argv[0]);
exit(0);
}

memset(szBuff, 0x00, sizeof(szBuff));
memset(line, 0x00, sizeof(line));
state = 0;

client_len = sizeof(clientaddr);
if( (server_sockfd = socket(AF_INET, SOCK_STREAM, 0) ) < 0){
perror("socket error : ");
exit(0);
}

bzero(&serveraddr, sizeof(serveraddr) );
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(atoi(argv[1]) );

state = bind(server_sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
if(state == -1){
perror("bind error : ");
exit(0);
}

state = listen(server_sockfd, 10);
if(state == -1){
perror("listen error : ");
exit(0);
}

signal(SIGCHLD, SIG_IGN);

while(1){
client_sockfd = accept(server_sockfd, (struct sockaddr*)&clientaddr, &client_len);
if(client_sockfd == -1){
perror("Accept error : ");
exit(0);
}
pthread_create(&chld_thr, NULL, do_chld, (void*)&client_sockfd);

}

close(client_sockfd);
}

void *do_chld(void *arg){
int client_sockfd = *((int*)arg);
char buf[255];

while(1){
memset(buf, '0x00', sizeof(buf));
if(read(client_sockfd, buf, 255) <= 0){
close(client_sockfd);
exit(0);
}
printf(": [%s]\n", buf);

write(client_sockfd, buf, 255);

}
}

송지석의 이미지

void *do_chld(void *arg){
        int client_sockfd = *((int*)arg);
        char buf[255];

        while(1){
                                memset(buf, '0x00', sizeof(buf));
                                if(read(client_sockfd, buf, 255) <= 0){
                                        close(client_sockfd);
                                        exit(0);=========>요기
                                }
                                printf(":  [%s]\n", buf);

                                write(client_sockfd, buf, 255);

        }
}

exit(0)가 문제겠네요. 쓰레드는 한 쓰레드가 exit해버리면 다 같이 끝납니다.
return;으로 바꿔주시면 될 듯.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.