1 #include <arpa/inet.h>
2 #include <fcntl.h>
3 #include <signal.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 #define CONN_QUEUE_SIZE 20
10 #define BUFFER_SIZE 1024
11 #define SERVER_PORT 12580
12
13 int server_sockfd;
14 int conn;
15
signal_handler(int signo)16 void signal_handler(int signo)
17 {
18
19 printf("Server is exiting...\n");
20 close(conn);
21 close(server_sockfd);
22 exit(0);
23 }
24
25 static char logo[] =
26 " ____ ___ ____ \n| _ \\ _ __ __ _ __ _ ___ _ __ / _ \\ / ___| "
27 "\n| | | || '__| / _` | / _` | / _ \\ | '_ \\ | | | |\\___ \\ \n| |_| || | | (_| || (_| || (_) || | | || |_| | "
28 "___) |\n|____/ |_| \\__,_| \\__, | \\___/ |_| |_| \\___/ |____/ \n |___/ \n";
29
tcp_server()30 void tcp_server()
31 {
32 printf("TCP Server is running...\n");
33 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
34 printf("socket() ok, server_sockfd=%d\n", server_sockfd);
35 struct sockaddr_in server_sockaddr;
36 server_sockaddr.sin_family = AF_INET;
37 server_sockaddr.sin_port = htons(SERVER_PORT);
38 server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
39
40 if (bind(server_sockfd, (struct sockaddr *)&server_sockaddr, sizeof(server_sockaddr)))
41 {
42 perror("Server bind error.\n");
43 exit(1);
44 }
45
46 printf("TCP Server is listening...\n");
47 if (listen(server_sockfd, CONN_QUEUE_SIZE) == -1)
48 {
49 perror("Server listen error.\n");
50 exit(1);
51 }
52
53 printf("listen() ok\n");
54
55 char buffer[BUFFER_SIZE];
56 struct sockaddr_in client_addr;
57 socklen_t client_length = sizeof(client_addr);
58 /*
59 Await a connection on socket FD.
60 When a connection arrives, open a new socket to communicate with it,
61 set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
62 peer and *ADDR_LEN to the address's actual length, and return the
63 new socket's descriptor, or -1 for errors.
64 */
65 conn = accept(server_sockfd, (struct sockaddr *)&client_addr, &client_length);
66 printf("Connection established.\n");
67 if (conn < 0)
68 {
69 printf("Create connection failed, code=%d\n", conn);
70 exit(1);
71 }
72 send(conn, logo, sizeof(logo), 0);
73 while (1)
74 {
75 memset(buffer, 0, sizeof(buffer));
76 int len = recv(conn, buffer, sizeof(buffer), 0);
77 if (len <= 0)
78 {
79 printf("Receive data failed! len=%d\n", len);
80 break;
81 }
82 if (strcmp(buffer, "exit\n") == 0)
83 {
84 break;
85 }
86
87 printf("Received: %s\n", buffer);
88 send(conn, buffer, len, 0);
89 }
90 close(conn);
91 close(server_sockfd);
92 }
93
udp_server()94 void udp_server()
95 {
96 printf("UDP Server is running...\n");
97 server_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
98 printf("socket() ok, server_sockfd=%d\n", server_sockfd);
99 struct sockaddr_in server_sockaddr;
100 server_sockaddr.sin_family = AF_INET;
101 server_sockaddr.sin_port = htons(SERVER_PORT);
102 server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
103
104 if (bind(server_sockfd, (struct sockaddr *)&server_sockaddr, sizeof(server_sockaddr)))
105 {
106 perror("Server bind error.\n");
107 exit(1);
108 }
109
110 printf("UDP Server is listening...\n");
111
112 char buffer[BUFFER_SIZE];
113 struct sockaddr_in client_addr;
114 socklen_t client_length = sizeof(client_addr);
115
116 while (1)
117 {
118 memset(buffer, 0, sizeof(buffer));
119 int len = recvfrom(server_sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr, &client_length);
120 if (len <= 0)
121 {
122 printf("Receive data failed! len=%d", len);
123 break;
124 }
125 if (strcmp(buffer, "exit\n") == 0)
126 {
127 break;
128 }
129
130 printf("Received: %s", buffer);
131 sendto(server_sockfd, buffer, len, 0, (struct sockaddr *)&client_addr, client_length);
132 printf("Send: %s", buffer);
133 }
134 close(conn);
135 close(server_sockfd);
136 }
137
tcp_client()138 void tcp_client()
139 {
140 printf("Client is running...\n");
141 int client_sockfd = socket(AF_INET, SOCK_STREAM, 0);
142
143 struct sockaddr_in server_addr = {0};
144 server_addr.sin_family = AF_INET;
145 server_addr.sin_port = htons(12581);
146 server_addr.sin_addr.s_addr = inet_addr("192.168.199.129");
147 printf("to connect\n");
148 if (connect(client_sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
149 {
150 perror("Failed to establish connection to server\n");
151 exit(1);
152 }
153 printf("connected to server\n");
154
155 char sendbuf[BUFFER_SIZE] = {0};
156 char recvbuf[BUFFER_SIZE] = {0};
157
158 int x = recv(client_sockfd, recvbuf, sizeof(recvbuf), 0);
159
160 fputs(recvbuf, stdout);
161
162 memset(recvbuf, 0, sizeof(recvbuf));
163
164 while (1)
165 {
166 fgets(sendbuf, sizeof(sendbuf), stdin);
167 sendbuf[0] = 'a';
168
169 // printf("to send\n");
170 send(client_sockfd, sendbuf, strlen(sendbuf), 0);
171 // printf("send ok\n");
172 if (strcmp(sendbuf, "exit\n") == 0)
173 {
174 break;
175 }
176
177 int x = recv(client_sockfd, recvbuf, sizeof(recvbuf), 0);
178 if (x < 0)
179 {
180 printf("recv error, retval=%d\n", x);
181 break;
182 }
183
184 fputs(recvbuf, stdout);
185
186 memset(recvbuf, 0, sizeof(recvbuf));
187 memset(sendbuf, 0, sizeof(sendbuf));
188 }
189 close(client_sockfd);
190 }
191
udp_client()192 void udp_client()
193 {
194 struct sockaddr_in addr;
195 int sockfd, len = 0;
196 int addr_len = sizeof(struct sockaddr_in);
197 char buffer[256];
198
199 /* 建立socket,注意必须是SOCK_DGRAM */
200 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
201 {
202 perror("socket");
203 exit(1);
204 }
205
206 /* 填写sockaddr_in*/
207 bzero(&addr, sizeof(addr));
208 addr.sin_family = AF_INET;
209 addr.sin_port = htons(12581);
210 addr.sin_addr.s_addr = inet_addr("192.168.199.129");
211
212 printf("to send logo\n");
213 sendto(sockfd, logo, sizeof(logo), 0, (struct sockaddr *)&addr, addr_len);
214 printf("send logo ok\n");
215 while (1)
216 {
217 bzero(buffer, sizeof(buffer));
218
219 printf("Please enter a string to send to server: \n");
220
221 /* 从标准输入设备取得字符串*/
222 len = read(STDIN_FILENO, buffer, sizeof(buffer));
223 printf("to send: %d\n", len);
224 /* 将字符串传送给server端*/
225 sendto(sockfd, buffer, len, 0, (struct sockaddr *)&addr, addr_len);
226
227 /* 接收server端返回的字符串*/
228 len = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&addr, &addr_len);
229 printf("Receive from server: %s\n", buffer);
230 }
231
232 return 0;
233 }
main()234 void main()
235 {
236 // signal(SIGKILL, signal_handler);
237 // signal(SIGINT, signal_handler);
238 tcp_server();
239 // udp_server();
240 // tcp_client();
241 // udp_client();
242 }