blocking 소켓 non-blocking 소켓으로의 변환

sacredone의 이미지

블로킹을 넌블로킹으로 변환할때의 방법을 찾아보니까

fcntl() 함수를 사용하라는 글이있었고

ioctlsocket() 함수를 호출해야만 가능하다는 글도있고

헷갈리네요;;

게다가 소켓을 어떻게 변환했다쳐도

read/write, recv/send 등의 함수사용에도

(특히 recv/send) 인자값에 변화가 있을거같은데

지식좀 나눠주실수 있으신분 계실까요 ㅠ

먼저 제가짠 단순한 코드점 올려볼게요

먼저 서버프로그램

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <unistd.h>
  5 #include <arpa/inet.h>
  6 #include <sys/types.h>
  7 #include <sys/socket.h>
  8
  9 #define BUFF_SIZE 1024
 10
 11 int main(int argc, char **argv)
 12 {
 13     int     client_addr_size;
 14     int     client_socket;
 15     int     server_socket;
 16     int     flag;
 17     struct  sockaddr_in client_addr;
 18     struct  sockaddr_in server_addr;
 19     char    recv_buffer[BUFF_SIZE];
 20     char    send_buffer[BUFF_SIZE];
 21
 22     memset(&server_addr, 0, sizeof(server_addr));
 23
 24     server_socket = socket(PF_INET, SOCK_STREAM, 0);
 25     if (server_socket == -1){
 26         perror("failed to make socket");
 27         return -1;
 28     }
 29
 30     server_addr.sin_family = AF_INET;
 31     server_addr.sin_port = htons(4000);
 32     server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
 33
 34     if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1){
 35         perror("failed to bind");
 36         return -1;
 37     }
 38
 39     if (listen(server_socket, 5) == -1){
 40         perror("failed to listen");
 41         return -1;
 42     }
 43
 44     while (1){
 45         memset(recv_buffer, 0, BUFF_SIZE);
 46
 47         client_addr_size = sizeof(client_addr);
 48         client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_size);
 49         if (client_socket == -1){
 50             perror("failed to accept");
 51             return -1;
 52         }
 53         recv(client_socket, recv_buffer, BUFF_SIZE, 0);
 54         printf("%s\n", recv_buffer);
 55
 56         send(client_socket, recv_buffer, strlen(recv_buffer), 0);
 57     }
 58  }

이건 클라이언트 프로그램

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <unistd.h>
  5 #include <arpa/inet.h>
  6 #include <sys/types.h>
  7 #include <stdlib.h>
  8 #include <stdlib.h>
  9 #include <sys/socket.h>
 10
 11 #define BUFFER_SIZE 1024
 12
 13 int main(int argc, char **argv)
 14 {
 15     int     client_socket;
 16     struct  sockaddr_in server_addr;
 17     FILE    *input_fp;
 18     char    recv_buffer[BUFFER_SIZE];
 19     char    *send_buffer;
 20     size_t  recv_len;
 21     ssize_t recv_size;
 22
 23     send_buffer = NULL;
 24     recv_len = 0;
 25     memset(recv_buffer, 0, BUFFER_SIZE);
 26
 27     input_fp = fopen(argv[1], "r");
 28     if (input_fp == NULL){
 29         perror("failed to open file");
 30         return -1;
 31     }
 32
 33     client_socket = socket(PF_INET, SOCK_STREAM, 0);
 34     if (client_socket == -1){
 35         perror("failed to make socket");
 36         return -1;
 37     }
 38
 39     server_addr.sin_family = AF_INET;
 40     server_addr.sin_port = htons(4000);
 41     server_addr.sin_addr.s_addr = inet_addr("10.1.1.150");
 42
 43     if ((connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr))) == -1){
 44         perror("failed to connect");
 45         return -1;
 46     }
 47
 48     recv_size = getline(&send_buffer, &recv_len, input_fp);
 49     send(client_socket, send_buffer, strlen(send_buffer), 0);
 50
 51     recv(client_socket, recv_buffer, BUFFER_SIZE, 0);
 52     printf("%s\n", recv_buffer);
 53
 54     if (send_buffer != NULL){
 55         free(send_buffer);
 56     }
 57     fclose(input_fp);
 58     close(client_socket);
 59
 60     return 0;
 61 }

기능은 매우 단순하게
클라이언트 프로그램을 텍스트 파일명을 인자로 줘서 실행하면
그 파일의 한줄을 읽고 서버로 전송하고 다시 클라가 받는 에코서버클라 플로그램입니다

이걸 최대한 안바꾸고 non-blocking 으로 바꿔서
변화되는것들을 좀 공부하고싶은데
도움좀 부탁드려볼게요

댓글 달기

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