소켓 프로그램 좀 봐주세요;

newhope의 이미지

기존에 프로그램에 새로운 소켓을 추가해서 관리하려는데 영 감이 안 오네요.
main 함수에서 cps_fe_server_start_bk(50000)을 호출해서 thread를 이용해서 cps_server_process_bk를 이용해서 특정 입력이 들어오면 처리한 후 종료메시지를 보내고 소켓을 종료하는 프로그램인데.
thread로 소켓을 전달하는 것 까지 좋은데 메시지 보내고 나서 소켓을 없앨려고 하는데 뭐가 좋을지 모르겠네요
대안 좀 주세요;

int
cps_fe_server_start_bk(int32_t Port)
{
int csock, sock; /* Socket Descriptor */
int csockArr[SOCKMAX];
struct sockaddr_in ClientAddrArr[SOCKMAX];
int ClientNum=0;
int ret;
int i = 0; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
struct sockaddr_in ClientAddr;
int ClientAddrSize = sizeof(ClientAddr);

u_int8_t *msg;
struct cpshdr *hdr;

/* Create a reliable, stream socket using TCP */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
printf("socket Create Error");
}

/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Server IP address */
echoServAddr.sin_port = htons(Port); /* Server port */

/* Bind Socket */
if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
{
printf( "Socket Bind Error");
}

/* Listen to the Socket*/
if (listen(sock, 5) < 0)
{
printf( "Socket Listen Error");
}

while (1)
{

/* Accept */
if ((csock = accept(sock, (struct sockaddr *) &ClientAddr, &ClientAddrSize)) < 0)
{
printf( "Socket Accept Error");
}
ClientNum= ClientNum + 1;
csockArr[ClientNum]=csock;

thread_add_read(cpsm, cps_server_process_bk, (void *)NULL, mdsGetSocketDesc(csockArr[ClientNum])); // 상용 Tool의 thread

}

int
cps_server_process_bk(struct thread * t)
{
int ret = 0;
u_int8_t RecvMsg[BUF_MAX_SIZE]; /* Recv Msg Buffer*/
int strLen = 0;
struct cpdhdr *cpdphdr;
int k = 0;

memset(RecvMsg, 0 sizeof(RecvMsg));

while ((strLen = recv(csockArr[ClientNum], RecvMsg, BUF_MAX_SIZE,0))!=0)
{
cpdphdr = (struct cpdphdr *)RecvMsg[MDS_HEADER_LEN];

switch (cpdphdr->cpdp_type)
{
case FP_NP_RESTART :
printf( "Received(A->B) the RESTART message\n");
cps_server_send_bk();
break;
default :
printf( "Received(A->B) unknown NP message\n");

}
}

}

int
cps_server_send_bk()
{
//보내는 함수 추가
}

jos77의 이미지

상용툴 thread 인가요? thread 내부 구조를 모르는 이상 효과적인 답변은 기대하기 힘들 것 같네요
개인적으로 socket 을 thread 내부에서 생성할게 아니라면 전역 변수로 관리하면서 사용하는 걸 추천합니다.

-----
안녕하세요 소프트웨어 공학센터 장원석 책임입니다.
http://www.software.kr

댓글 달기

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