TCP 서버에서 클라이언트 수 제한하는 방법이 있나요?

rpit1412의 이미지

안녕하세요,

지금 TCP 서버를 만들어서 클라이언트에서 전달한 메세지를 처리하는 것을 만들고 있는데요.
하나의 연결이 맺어질 경우 다른곳에서의 연결은 차단하고 싶습니다. (단일 세션으로 동작)
개발 환경은 리눅스입니다.

코드는 처음에 연결 된 클라이언트만 동작하는데, 추가로 다른 클라이언트로 접속을 하면
연결은 되네요.. listen을 1로 해놨는데도 동일합니다.
이거 막을수 있는 방법이 있나요??

netstat으로 포트 확인해봤는데 하나가 ESTABLISHED 되어있어도 localhost는 따로 계속 LISTEN 하고있네요

현재 작업중인 소스 첨부드립니다. 코드는 엉망이지만 한번 봐주세요..

void recv_msg_proc(void)
{
    int read_size=0;
    char buffer[2000]={0,};
    char recv_cmd[2000]={0,};
 
    while( (read_size = recv(clnt_sock, buffer, 2000, 0)) > 0 )
    {
        strcpy( recv_cmd, buffer );
 
        recv_cmd[strlen(recv_cmd)-1] = 0x00;
        recv_cmd[strlen(recv_cmd)-1] = 0x00;
 
        // message processing : TODO
 
        memset( buffer, 0, sizeof(buffer) );
        memset( recv_cmd, 0, sizeof(recv_cmd) );
 
    }
 
    if(read_size == 0)
    {
        printf("Client disconnected\n");
    }
    else if(read_size == -1 )
    {
        printf("Received failed\n");
    }
    return((void *) 0);
}
 
int create_recv_sock(int port)
{
    int sock_desc = -1, isockopt;
    int res = 0;
    struct sockaddr_in sa;
    struct linger l;
 
    sock_desc = socket(AF_INET, SOCK_STREAM, 0);
    if( sock_desc < 0 )
    {
        printf("Could not create socket\n");
        res = -1;
    }
    else{
        isockopt = 1;
 
        l.l_onoff = 1;
        l.l_linger = 0;
        if( setsockopt( sock_desc, SOL_SOCKET, SO_LINGER, (char *)&l, sizeof(l) ) == -1 ){
            res = -1;
        }
        else{
            memset( &sa, 0, sizeof( struct sockaddr_in ) );
 
            sa.sin_family = AF_INET;
            sa.sin_addr.s_addr = INADDR_ANY;
            sa.sin_port = htons( port );
 
            if( bind(sock_desc, (struct sockaddr *)&sa, sizeof(sa) ) < 0 ){
                close(sock_desc);
                res = -1;
            }
            else{
                if( listen(sock_desc, 1) == -1 )
                {
                    printf("listen failed......\n");
                    res = -1;
                }
                else
                {
                    cli_cfg.recv_sock= sock_desc;
                }
            }
        }
    }
 
    return res;
}
 
int main(int argc, char *argv[])
{
    int ret = 0;
    int client_sock, c, read_size;
    char client_message[2000]={0,};
    struct sockaddr_in client;
 
    ret = create_recv_sock(DEFAULT_CONNECTION_PORT);
 
    c = sizeof(struct sockaddr_in);
 
    while(1)
    {
        client_sock = accept(cli_cfg.recv_sock, (struct sockaddr *)&client, (socklen_t*)&c);
        if( client_sock < 0 )
        {
            printf("accept failed\n");
            return 1;
        }
        else
        {
            printf("Connection accepted\n");
            recv_msg_proc();
        }
    }
    return ret;
}
shint의 이미지

1. 일단 접속하고. 별도로. IP를 확인해서. 같은게 있으면. 연결 해제
2. 일단 접속하고. 별도로. 인증 키를 전송해서. 같은게 있으면. 연결 해제

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

rpit1412의 이미지

답변 감사드립니다.

제가 원하는 방향은 클라이언트측에서 "Connection Refused"가 되는 것입니다.
말씀해주신 방법은 정상 접속종료같은데 연결 거부가 될 수 있는건가요??

rpit1412의 이미지

해결 했습니다.

accept가 되면 listen한 socket을 close 시키고 accept 된 socket을 사용하니까 기존 연결은 끊기지 않고
연결이 종료 되면 listen socket부터 다시 생성해서 쓰니까 single session으로 동작하네요.

생각해보면 별거 아닌거 같은데 저런 생각을 왜 못했을까요.. ㅎㅎ

댓글 달기

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