comparison cbcecho/#echoserver_cbc.cbc# @ 11:1e0cd7fade8b

add regen
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Sun, 19 Jun 2011 16:36:05 +0900
parents
children
comparison
equal deleted inserted replaced
10:41634f26cd6f 11:1e0cd7fade8b
1 #include <stdio.h>
2 #include <stdlib.h> /* exit() */
3 #include <string.h> /* bzero() */
4 #include <sys/types.h>
5 #include <sys/socket.h> /* socket(), bind(), listen(), accept(), recv() */
6 #include <netinet/in.h> /* htons() */
7 #include <unistd.h>
8 #include <pthread.h>
9
10 #define PORT 8888 /* Listen’¤¹’¤ë’¥Ý’¡¼’¥È */
11 #define MAXDATA 1024 /* ’¼õ’¿®’¥Ð’¥Ã’¥Õ’¥¡’¥µ’¥¤’¥º */
12
13
14 //__code echoaccept(int listen_fd, struct sockaddr *caddr, socklen_t *len );
15 __code echoaccept(int listen_fd, void *caddr, void *len );
16 //__code echoread(void *ptr);
17 void *echoread(void *ptr);
18 __code conref();
19 __code confail();
20 __code echowrite(int conn_fd, char *buf, int rsize);
21
22
23 void main()
24 {
25 struct sockaddr_in saddr; /* ’¥µ’¡¼’¥Ð’ÍÑ’¥¢’¥É’¥ì’¥¹’³Ê’Ǽ’¹½’¤’ÂÎ */
26 struct sockaddr_in caddr; /* ’¥¯’¥é’¥¤’¥¢’¥ó’¥È’ÍÑ’¥¢’¥É’¥ì’¥¹’³Ê’Ǽ’¹½’¤’ÂÎ */
27
28 int listen_fd;
29 int conn_fd;
30
31 //int len = sizeof(struct sockaddr_in);
32 socklen_t len = sizeof(struct sockaddr_in);
33
34 int rsize;
35 char buf[MAXDATA]; /* ’¼õ’¿®’¥Ð’¥Ã’¥Õ’¥¡ */
36
37 /* ’¥½’¥±’¥Ã’¥È’¤Î’À¸’À® */
38 if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
39 perror("socket");
40 exit(EXIT_FAILURE);
41 }
42
43 /*
44 * saddr’¤Î’Ãæ’¿È’¤ò0’¤Ë’¤·’¤Æ’¤ª’¤«’¤Ê’¤¤’¤È’¡¢bind()’¤Ç’¥¨’¥é’¡¼’¤¬’µ¯’¤³’¤ë’¤³’¤È’¤¬’¤¢’¤ë
45 */
46 bzero((char *)&saddr, sizeof(saddr));
47
48 /* ’¥½’¥±’¥Ã’¥È’¤Ë’¥¢’¥É’¥ì’¥¹’¤È’¥Ý’¡¼’¥È’¤ò’·ë’¤Ó’¤Ä’¤±’¤ë */
49 saddr.sin_family = PF_INET;
50 saddr.sin_addr.s_addr = INADDR_ANY;
51 saddr.sin_port = htons(PORT);
52 if (bind(listen_fd, (struct sockaddr *)&saddr, len) < 0) {
53 perror("bind");
54 exit(EXIT_FAILURE);
55 }
56
57 /* ’¥Ý’¡¼’¥È’¤òListen’¤¹’¤ë */
58 if (listen(listen_fd, SOMAXCONN) < 0) {
59 perror("listen");
60 exit(EXIT_FAILURE);
61 }
62 printf("Start Listening Port : %d...\n", PORT);
63
64 // goto echoread((void *)&conn_fd);
65 goto echoaccept(listen_fd, (void *)&caddr, (void *)&len);
66 }
67
68
69 //__code echoaccept(int listen_fd, struct sockaddr *caddr, socklen_t *len )
70 __code echoaccept(int listen_fd, void *caddr, void *len )
71 {
72 int conn_fd;
73 if((conn_fd = accept(listen_fd, (struct sockaddr *)caddr, (socklen_t *)len)) < 0){
74 perror("accept");
75 exit(EXIT_FAILURE);
76 }else{
77 pthread_t thread;
78 pthread_create( &thread, NULL, (void *)echoread, (void *)&conn_fd);
79 goto echoaccept(listen_fd, caddr, len);
80 }
81 }
82
83
84 //__code echoread(void *ptr)
85 void *echoread(void *ptr)
86 {
87 int conn_fd = *(int*)ptr;
88 char buf[MAXDATA]; /* ’¼õ’¿®’¥Ð’¥Ã’¥Õ’¥¡ */
89 int rsize;
90
91 rsize = recv(conn_fd, buf, MAXDATA, 0);
92
93 if(rsize == 0 ){/* if connection refused */
94 goto conref();
95 }else if(rsize == -1){
96 goto confail();
97 }else{
98 goto echowrite(conn_fd, buf, rsize);
99 }
100
101 }
102
103 __code conref()
104 {
105 printf("connection refused");
106 pthread_cancel(pthread_self());
107 }
108
109 __code confail()
110 {
111 perror("recv");
112 exit(EXIT_FAILURE);
113 }
114
115 __code echowrite(int conn_fd, char *buf, int rsize)
116 {
117 write(conn_fd, buf, rsize);
118 goto echoread((void *)&conn_fd);
119 }