간단한 thread 질문입니다;;;

jehun0717의 이미지

클라이언트->서버
입력, 삭제 등, 서버에서 결과값을 클라이언트로 보내지 않는 기능은 정상 작동합니다.
하지만
클라이언트->서버->클라이언트
위와같이, 서버에서 처리한 값을 클라이언트로 보내야 하는, 출력, 검색 등에선
가장 마지막에 연결된 클라이언트만 동작하고 있습니다.
그 전에 연결되어 있던 정보가 날아가는거 같은데...;;;

도와주세요~

///////////////////////////////////// main.c
#include "main.h"
#define BUFSIZE 60
int serv_sock;
int clnt_sock;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
pthread_t t_id;
pthread_mutex_t mutx;

socklen_t clnt_addr_size;
void error_handling(char *message);
int main(int argc, char **argv)
{
printf("SERVER START\n");
SetMC();
SetFMC();
if(argc != 2)
{
printf("Usage : %s \n", argv[0]);
exit(1);
}
Set_Sock(argc, argv);
while(1)
{
Set_Listen();
}
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}

///////////////////////////////////// tcp_mgmt.c
extern int clnt_sock;
extern int serv_sock;
extern socklen_t clnt_addr_size;
extern struct sockaddr_in serv_addr;
extern struct sockaddr_in clnt_addr;
FILE *fp;
MC *HEAD;
MC *HEADtemp;
int clnt_clnts[MAX_CNT];
int clnt_cnt = 0;
pthread_t t_id;
pthread_mutex_t mutx;
void *thr_ret;
void Set_Sock(int argc, char *argv[])
{
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
#include "main.h"
#define BUFSIZE 60
#define MAX_CNT 256
extern int clnt_sock;
extern int serv_sock;
extern socklen_t clnt_addr_size;
extern struct sockaddr_in serv_addr;
extern struct sockaddr_in clnt_addr;
FILE *fp;
MC *HEAD;
MC *HEADtemp;
int clnt_clnts[MAX_CNT];
int clnt_cnt = 0;
pthread_t t_id;
pthread_mutex_t mutx;
void *thr_ret;
void Set_Sock(int argc, char *argv[])
{
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if(serv_sock == -1)
{
error_handling("Socket Error...!!!\n");
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
bind(serv_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if(serv_sock == -1)
{
error_handling("Bind Error...!!!\n");
}
}
void Set_Listen(void)
{
listen(serv_sock, 5);
if(serv_sock == -1)
{
error_handling("Error Listen...!!!\n");
}
pthread_mutex_init(&mutx, NULL);
clnt_addr_size = sizeof(clnt_addr);
while(1)
{
clnt_sock = accept(serv_sock, (struct sockaddr *)&clnt_addr, &clnt_addr_size);
pthread_mutex_lock(&mutx);
clnt_clnts[clnt_cnt++] = clnt_sock;
pthread_mutex_unlock(&mutx);
pthread_create(&t_id, NULL, S_Recv, (void *)&clnt_sock);
if(clnt_sock == -1)
{
error_handling("Accept Error...!!!\n");
}
printf("Connected Client IP : %s\n", inet_ntoa(clnt_addr.sin_addr));
}
}

void *S_Recv(void *arg)
{
int type;
int length;
int TL;
int Thread_clnts = *((int *)arg);
// Thread_clnts = malloc(sizeof(int));
char buf_in[BUFSIZE];
char buf_out[BUFSIZE];
MC *p;
MC *c;
MC *SHead = HEAD->R;
MC *DataTemp;
MC *current;
MC *next;
MC *temp;
MC *Delete;
MC *s;
char pName[20];
MC *curr = HEAD->R;
MC *prev = HEAD;
char SDName[20];
while(1)
{
memset(buf_in, 0x00, BUFSIZE);
memset(buf_out, 0x00, BUFSIZE);
printf("User input wait...\n");
recv(Thread_clnts, buf_in, 8, 0);
memcpy(&TL, buf_in, 4);
type = ntohl(TL);
memcpy(&TL, buf_in+4, 4);
length = ntohl(TL);
if(type == 1 && length >= 8)
{
printf("Server Input Start\n");
c = HEAD;
p = (MC *)malloc(sizeof(MC));
recv(Thread_clnts, buf_in, length - 8, 0);
memcpy(p, buf_in, length-8);
pthread_mutex_lock(&mutx);
while(c->R != NULL)
{
c = c->R;
}
c->R = p;
p->L = c;
c->R = p;
p->R = NULL;
pthread_mutex_unlock(&mutx);
S_FileW(HEAD);
printf("Server Input End\n");
}
else if(type == 2 && length <= 8)
{
printf("Server Output Start\n");
for(; SHead != NULL; SHead = SHead->R)
{
printf("돌긴 도냐?\n");
DataTemp = (MC *)malloc(sizeof(MC));
strcpy (DataTemp->S_Name, SHead->S_Name);
strcpy (DataTemp->S_Age, SHead->S_Age);
strcpy (DataTemp->S_Score, SHead->S_Score);
if (HEADtemp->R == NULL)
{
HEADtemp->R = DataTemp;
DataTemp->L = HEADtemp;
DataTemp->R = NULL;
}
else
{
current = HEADtemp;
next = current->R;
while(next != NULL)
{
if(atoi(DataTemp->S_Score) > atoi(next->S_Score))
{
current->R = DataTemp;
DataTemp->L = current;
DataTemp->R = next;
next->L = DataTemp;
break;
}
else
{
if (next->R == NULL)
{
next->R = DataTemp;
DataTemp->L = next;
DataTemp->R = NULL;
next->L = current;
break;
}
}
current = next;
next = next->R;
}
}
}
pthread_mutex_lock(&mutx);
temp = HEADtemp->R;
Delete = temp;
while(temp != NULL)
{
printf("전송 하긴 해?\n");
type = 22;
TL = htonl(type);
memcpy(buf_out, &TL, 4);
length = sizeof(MC);
TL = htonl(length);
memcpy(buf_out+4, &TL, 4);
memcpy(buf_out+8, temp, sizeof(MC)-8);
printf("temp : %s %s %s\n", temp->S_Name, temp->S_Age, temp->S_Score);
S_Send(buf_out, length);
temp = temp->R;
}
while(temp != NULL)
{
Delete = temp;
temp = temp->R;
free(Delete);
}
HEADtemp->R = NULL;
HEADtemp->L = NULL;
pthread_mutex_unlock(&mutx);
type = 9;
TL = htonl(type);
memcpy(buf_out, &TL, 4);
length = 8;
TL = htonl(length);
memcpy(buf_out+4, &TL, 4);
S_Send(buf_out, length);
printf("Server Output End\n");
}
else if(type == 3 && length >=8)
{
printf("Server Search Start\n");
recv(Thread_clnts, buf_in, length, 0);
memcpy(pName, buf_in, sizeof(pName));
s = HEAD->R;
pthread_mutex_lock(&mutx);
while(strcmp(s->S_Name, pName) && s != NULL)
{
if(s->R == NULL)
{
type = 9;
TL = htonl(type);
memcpy(buf_out, &TL, 4);
length = 8;
TL = htonl(length);
memcpy(buf_out+4, &TL, 4);
S_Send(buf_out, length);
s = NULL;
break;
}
s = s->R;
}
pthread_mutex_unlock(&mutx);
if(s != NULL)
{
type = 33;
TL = htonl(type);
memcpy(buf_out, &TL, 4);
length = sizeof(MC)-8;
TL = htonl(length);
memcpy(buf_out+4, &TL, 4);
memcpy(buf_out+8, s, length);
S_Send(buf_out, length);
}
}
else if(type == 4 && length >= 8)
{
printf("Server Delete Start\n");
recv(clnt_sock, buf_in, length, 0);
memset(SDName, 0x00, sizeof(SDName));
memcpy(SDName, buf_in, sizeof(SDName));
pthread_mutex_lock(&mutx);
while(curr != NULL)
{
if (strcmp(curr->S_Name, SDName) == 0)
{
prev->R = curr->R;
if(curr->R != NULL)
{
curr->R->L = prev;
}
else if(curr->R == NULL)
{
curr->R = NULL;
}
free (curr);
curr = NULL;
break;
}
else if(curr->R == NULL)
{
printf("등록된 사원이 없습니다.\n");
break;
}
prev = curr;
curr = curr->R;
}
pthread_mutex_unlock(&mutx);
S_FileW(HEAD);
printf("Server Delete End\n");
}
else if(type == 5)
{
pthread_detach(t_id);
S_Close();
}
}
}
void S_Send(char *buf_out, int length)
{
send(clnt_sock, buf_out, length, 0);
}
void S_Close(void)
{
printf("Client close\n");
close(clnt_sock);
Set_Listen();
}

댓글 달기

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