-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_server.c~
123 lines (106 loc) · 2.01 KB
/
tcp_server.c~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
/*
#include <ctype.h>
#include <stdlib.h>
*/
const int port = 8887;
const int MAX = 10; //biggest number of connected clients
const char IP[] = "127.0.0.1";
/*
char *strupr(char *str)
{
if (str == NULL)
return NULL;
char *ptr = str;
while (*ptr != '\0') {
if (islower(*ptr))
*ptr = toupper(*ptr);
ptr++;
}
return str;
}
*/
int main()
{
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0)
{
perror("scoket: ");
return -1;
}
puts("socket success");
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY); //IP
server.sin_port = htons(port);
if (bind(server_fd, (struct sockaddr *)&server, sizeof(server)) < 0)
{
perror("bind: ");
return -1;
}
puts("bind success");
if (listen(server_fd, MAX) < 0)
{
perror("listen: ");
return -1;
}
puts("listen success");
struct sockaddr_in client;
int client_len = sizeof(client);
int conn;
if ((conn = accept(server_fd, (struct sockaddr *)&client , &client_len) < 0))
{
perror("accept: ");
return -1;
}
puts("accept success");
/*
else
{
printf("One client connected %s:%4d\n", (char *)client.sin_addr.s_addr, (short)client.sin_port);
}
*/
char buff[105];
while (1)
{
//接收
memset(buff, 0, sizeof(buff));
if (recv(conn, buff, sizeof(buff), 0) < 0)
{
perror("read: ");
return -1;
}
if (strcmp(buff, "end\n") == 0)
{
puts("the server process end.");
break;
}
printf("recv message from client: %s", buff);
//发送
memset(buff, 0, sizeof(buff));
if (read(STDIN_FILENO, buff, sizeof(buff)) < 0)
{
perror("read2: ");
return -1;
}
/*
strupr(buff);
*/
if (send(conn, buff, strlen(buff), 0) < 0)
{
perror("send2: ");
return -1;
}
}
//关闭
close(conn);
close(server_fd);
return 0;
}