-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringServer.cpp
More file actions
147 lines (129 loc) · 3.91 KB
/
Copy pathstringServer.cpp
File metadata and controls
147 lines (129 loc) · 3.91 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdlib.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sstream>
#include <limits.h>
#include <string.h>
using namespace std;
#define MAX_LEN 100
#define MAX_BUF USHRT_MAX
#define PORT 0
#define MAX_CLIENT 5
string process(string s) {
stringstream ss;
ss << s;
int len = s.length();
bool capital = 1;
for(int i=0; i<len; i++) {
char space = s[i];
if(space == 32) {
capital = 1;
continue;
}
char c;
ss >> c;
int ic = (int) c;
if(capital) {
if(ic >= 97 && ic <= 122) {
s[i] = c - 32;
capital = 0;
} else {
capital = 0;
continue;
}
} else {
if(ic >= 65 && ic <= 90) {
s[i] = c + 32;
} else {
continue;
}
}
}
return s;
}
int main() {
char hostname[MAX_LEN];
char remoteIP[INET6_ADDRSTRLEN];
int listenfd, connfd, n, bytes_sent, bytes_recv, i, j, fdmax, newfd, result;
socklen_t clilen;
fd_set master, read_fds;
socklen_t addrlen;
struct sockaddr_in server_addr;
struct sockaddr_storage remoteaddr;
////////////////////////////////////////////////////////////////////////////////////
// Code of making connection is reused from <Beej's Guide to Network Programming> //
////////////////////////////////////////////////////////////////////////////////////
listenfd = socket(AF_INET, SOCK_STREAM, 0); //socket
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORT);
bind(listenfd, (struct sockaddr *) &server_addr, sizeof(server_addr)); //bind
listen(listenfd, MAX_CLIENT); //listen
//print SERVER_ADDRESS
gethostname(hostname,MAX_LEN);
cout << "SERVER_ADDRESS " << hostname << endl;
//print SERVER_PORT
addrlen = sizeof(server_addr);
if (getsockname(listenfd, (struct sockaddr *)&server_addr, &addrlen) == -1) perror("getsockname");
cout << "SERVER_PORT " << ntohs(server_addr.sin_port) << endl;
///////////////////////////////////////////////////////////////////////////
// Code of select() is reused from <Beej's Guide to Network Programming> //
///////////////////////////////////////////////////////////////////////////
// add the listener to the master set
FD_SET(listenfd, &master);
// keep track of the biggest file descriptor
fdmax = listenfd; // so far, it's this one
while(1) {
read_fds = master; // copy it
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
perror("select");
exit(4);
}
for(i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) { // we got one!!
if (i == listenfd) {
// handle new connections
addrlen = sizeof remoteaddr;
newfd = accept(listenfd, (struct sockaddr *)&remoteaddr, &addrlen); //accept
if (newfd == -1) {
perror("accept");
} else {
FD_SET(newfd, &master); // add to master set
if (fdmax < newfd) {
fdmax = newfd;
}
}
} else {
char buffer[MAX_BUF];
char len[4];
read(i, len, 4); //read 4-byte length
if ((bytes_recv = recv(i, buffer, MAX_BUF, 0)) <= 0) { //receive
if (bytes_recv != 0) {
perror("recv");
}
close(i);
FD_CLR(i, &master);
} else {
//process data
string hex_len(reinterpret_cast<char*>(len), 4);
string reply(hex_len);
reply.append(process(string(buffer)));
reply[reply.length()] = '\0';
if((bytes_sent = send(i, reply.c_str(), reply.length(), 0)) == -1) { //send
perror("sent");
exit(1);
}
memset(buffer, 0, sizeof(buffer));
}
}
}
}
}
}