forked from rqycpp/socket
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.c
78 lines (67 loc) · 1.64 KB
/
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
//服务端程序
//接收客户端字符串,将其大小写变换后返回。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#define PORT 1234
#define BACKLOG 5
#define MAXDATASIZE 256
int main()
{
int listenfd, connectfd;
struct sockaddr_in server;
struct sockaddr_in client;
socklen_t addrlen;
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket() error.\n");
exit(1);
}
int opt = SO_REUSEADDR;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1)
{
perror("Bind() error.\n");
exit(1);
}
if(listen(listenfd, BACKLOG) == -1)
{
perror("listen() error.\n");
exit(1);
}
addrlen = sizeof(client);
if((connectfd = accept(listenfd, (struct sockaddr *)&client, &addrlen)) == -1)
{
perror("accept() error\n");
exit(1);
}
printf("You got a connection from client's ip is %s, port is %d.\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
char buf[MAXDATASIZE];
while(1){
int i, num;
if((num = read( connectfd, buf, MAXDATASIZE)) == -1){
perror("read() error.\n");
exit(1);
}
for(i = 0; i < num; ++i){
if(buf[i] >= 'a' && buf[i] <= 'z')
buf[i] = toupper(buf[i]);
else
buf[i] = tolower(buf[i]);
}
write( connectfd, buf, num);
}
close(connectfd);
close(listenfd);
return 0;
}