아이고 형님들 bind 에러좀 고쳐주십쇼

tnckd2983의 이미지

11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19
20 #define BUF_SIZE 30
21
22 void error_handling(char* message);
23 void read_childproc(int sig);
24
25 int main(int argc, char** argv)
26 {
27 int option=1;
28 int optlen;
29
30 int fds1[2], fds2[2];
31 socklen_t clnt_adr_sz;
32 int serv_sock, clnt_sock; //socket용 accept용
33 char buf[BUF_SIZE]; //메제시 저장변수
34 pid_t pid;
35 struct sockaddr_in serv_adr, clnt_adr;
36 struct sigaction act; //시그널 구조체
37 int str_len, state;
38 pipe(fds1);
39 pipe(fds2);
40
41 if(argc != 2)
42 {
43 printf("Usage: %s \n",argv[0]);
44 exit(0);
45 }
46 act.sa_handler = read_childproc;
47 sigemptyset(&act.sa_mask);
48 act.sa_flags = 0;
49
50 state = sigaction(SIGCHLD, &act, 0);
51
52 serv_sock = socket(PF_INET, SOCK_STREAM, 0);
53
54 option = 1;
55 setsockopt(serv_sock, SOL_SOCKET, SO_REUSEADDR, (void*)&option, sizeof(option));
56
57 memset(&serv_adr, 0, sizeof(serv_adr));
58 serv_adr.sin_family = AF_INET;
59 serv_adr.sin_addr.s_addr = htonl(INADDR_ANY); //나중에 ip주소 바꿔야겠다. ㅋ
60 serv_adr.sin_port = htons(atoi(argv[1]));
61
62 if(bind(serv_sock, (struct sockaddr*)&serv_adr, sizeof(serv_adr)) == -1)
63 error_handling("bind() error");
64 if(listen(serv_sock, 2) == -1)
65 error_handling("listen() error");
66 //setsockopt(serv_sock, SOL_SOCKET, SO_REUSEADDR, (void*)&option, sizeof(option));
67
68 int i;
69 //그냥 한개는 accept하고 생성된 다음 부모프로세스엑서 accept준비해야겠디.
70 clnt_adr_sz = sizeof(clnt_adr);
71 clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_adr, &clnt_adr_sz);
72 if(clnt_sock == -1)
73 error_handling("accept() error");
74 else
75 printf("Client connected!\n");
76 //
77 pid = fork();
78
79 if(pid == -1)
80 {
81 error_handling("suspicious fork()");
82 }
83 if(pid == 0) //클라이언트1의 실행구간
84 {
85 char recv_buf[BUF_SIZE];
86 char send_buf[BUF_SIZE]; //프로세스 2개 만들어서 한개는 클라이언트의 데이터를 받는 프로세스 한개는 서버간의 프로세스를 통신할 프로세스
87 close(serv_sock); //복사되었으니 지웡
88 //read(fds1[0], recv_buf, BUF_SIZE); //수신할때 서버의 다른프로세스가 주는 데이터랑 클라이언트가 주는 데이터가 있네 ;; 음 .. 이것도 프로세슬르 만들어서 해야하나
89
90 pid = fork();
91
92 if(pid == 0) //서버관의 통신
93 {
94 //close(clnt_sock);
95 read(fds1[0], recv_buf, BUF_SIZE);
96 write(clnt_sock, recv_buf, BUF_SIZE);
97 }
98 else{ //클라이언트와의 통신
99 while((str_len = read(clnt_sock, send_buf, BUF_SIZE)) !=0)
100 {
101 write(fds2[1], send_buf,BUF_SIZE);
102 }
103 puts("Client disconnected!");
104 close(clnt_sock);
105 return 0;
106 }
107 }
108
109 else
110 { //나중에 여기 while문 넣어보자 접속이 끊어지고 다른 클라이언트가 접속할수 있게 ㅋ
111 close(clnt_sock);
112 clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_adr, &clnt_adr_sz); //접속할 때 까지 bloking상태 //클라이언트가 이름을 쓸 수 있게 해야지
113 //클라이언트 작성할 때 writev랑 readv 써서 클라이언트의 이름을 같이 보낼 수도 이름을 분리할 수 있게 만들자!
114 if(clnt_sock == -1)
115 {
116 error_handling("accept() error");
117 }
118 else
119 {
120 printf("Client connected!\n");
121 }
122 pid = fork();
123 if(pid == -1)
124 {
125 error_handling("suspicious fork()");
126 }
127 if(pid == 0) //클라이언트2의 실행구간
128 {
129 close(serv_sock);
130 char recv_buf[BUF_SIZE];
131 char send_buf[BUF_SIZE];
132
133 pid = fork();
134 if( pid == 0) //server
135 {
136 // close(clnt_sock);
137 read(fds2[0], recv_buf, BUF_SIZE); //umm...
138 write(clnt_sock, recv_buf, BUF_SIZE);
139 }
140 else{
141 while((str_len = read(clnt_sock, send_buf, BUF_SIZE)) != 0)
142 {
143 write(fds1[0], send_buf, BUF_SIZE);
144 }
145 close(clnt_sock);
146 puts("Client disconnected!");
147 return 0;
148 }
149 }
150 else{
151 close(clnt_sock);
152 }
153 }
154 close(serv_sock);
155 return 0;
156 }
157
158 void error_handling(char* message)
159 {
160 fputs(message, stderr);
161 fputc('\n',stderr);
162 exit(1);
163 }
164 void read_childproc(int sig)
165 {
166 pid_t pid;
167 int status;
168 id = waitpid(-1,&status,WNOHANG);
169 printf("remove proc id : %d\n",pid);
170 }

세벌의 이미지

cksdnd0987의 이미지

이거 코드를 그냥 복사 붙여넣기 해놓으시면 사람들이 분석 안할겁니다....
하다못해 code 태그로 깔끔하게 정리해주시고, 주석으로 정리라도 해주셔야 사람들이 분석해줄겁니다...

라스코니의 이미지

뭐가 문제라는 언급도 없군요.

댓글 달기

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