리눅스 쓰레드 기반 채팅 프로그램을 작성 중인데 질문 드립니다.

pnukk12의 이미지

채팅 프로그램을 작성 하려고 하는데요
멀티쓰레드 기반으로 하고 있습니다.

서버에서는 클라이언트가 접속할때마다 쓰레드를 생성하고 받을 문자열을 모든 쓰레드의 클라이언트 소켓 으로 보내는 방식인데요

클라이언트가 서버에 접속을하였고 글을 보냈는데 다른 클라이언트가 쓴 내용은 보이지 않네요,.

혹시나 서버에서 못받았나 싶어서 확인햇는데 서버에서는 모든 클라이언트의 대화가 보입니다.

그래서 서버에서 다시보내거나 각각의 쓰레드에서 다시 받는 코드가 잘못됬나 싶어 확인하는데 어디가 잘못됬는지 모르겠네요 ㅠㅠ

void* send_message(void* arg)
58 {
59 int sock = (int) arg;
60 char name_message[NAMESIZE+BUFSIZE];
61 while(1) {
62 fgets(message, BUFSIZE, stdin);
63 if(!strcmp(message, "q\n")) {
64 close(sock);
65 exit(0);
66 }
67 sprintf(name_message,"%s %s", name, message);
68 write(sock, name_message, strlen(name_message));
69 }
70 }
71
72 void* recv_message(void* arg)
73 {
74 int sock = (int)arg;
75 char name_message[NAMESIZE+BUFSIZE];
76 int str_len;
77 while(1) {
78 str_len = read(sock, name_message, NAMESIZE+BUFSIZE-1);
79 if(str_len == -1) exit(1);
80 name_message[str_len] = 0;
81 fputs(name_message, stdout);
82 }

클라이언트 에서 두개의 쓰레드를 생성하여 각각 보내고 받는 기능을 하였고요

void* clnt_connection(void *arg)
68 {
69 int clnt_sock = (int)arg;
70 int str_len = 0;
71 char message[BUFSIZE];
72 int i;
73 char test[] = "here\n";
74
75 while( (str_len = read(clnt_sock, message, sizeof(message)) != 0)){
76 send_message(message, str_len);
77 }
78
79 pthread_mutex_lock(&mutx);
80 for(i=0 ; i 81 if(clnt_sock == clnt_socks[i]) {
82 for( ; i 83 clnt_socks[i] = clnt_socks[i+1];
84 break;
85 }
86 }
87 clnt_number--;
88 pthread_mutex_unlock(&mutx);
89
90 close(clnt_sock);
91 return 0;
92 }

서버에서 쓰레드로 쓰이는 함수 입니다 읽어서 send_message 함수를 호출하고요

94 void send_message(char *message, int len)
95 {
96 int i;
97 pthread_mutex_lock(&mutx);
98 for(i=0; i < clnt_number; i++)
99 write(clnt_socks[i], message, len);
100 pthread_mutex_unlock(&mutx);
101 }

위 코드로 각각의 소켓에 문자열을 보내줍니다

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.