-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverbej.cpp
302 lines (285 loc) · 8.05 KB
/
serverbej.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//serverbej.cpp
#include "mysockbej.h"
using namespace std;
class Server
{
private:
int status,tcpFlag,yes,sockfd,newfd;;
char s[INET6_ADDRSTRLEN],newMsg[MAXDATASIZE];
const char *local,*port;
struct sigaction sa;
struct addrinfo *p,*serverInfo, hints;
struct sockaddr_storage their_addr;
socklen_t sin_size,addr_len;
//Data Types for multiple socket listening
fd_set master; // master file descriptor list
fd_set read_fds; // temp file descriptor list for select()
int fdmax; // maximum file descriptor number
public:
void sigchld_handler(int s)
{
// waitpid() might overwrite errno, so we save and restore it:
int saved_errno = errno;
while(waitpid(-1, NULL, WNOHANG) > 0);
errno = saved_errno;
}
void display(int tcpFlag)
{
//bool display(struct addrinfo *pointer)
if(tcpFlag == 1)
{
cout << "\n addrinfo strucutre has the following details TCP " ;
cout << "\n ai_family" << serverInfo->ai_family;
cout << "\n ai_socktype" << serverInfo->ai_socktype;
cout << "\n ai_addr : " << (struct sockaddr *) (serverInfo->ai_addr)->sa_data;
cout << "\n ai_addrlen: " << serverInfo->ai_addrlen;
}//end tcpFlag == 1
else if(tcpFlag == 2)
{
cout << "\n addrinfo strucutre has the following details UDP " ;
cout << "\n ai_family" << serverInfo->ai_family;
cout << "\n ai_socktype" << serverInfo->ai_socktype;
cout << "\n ai_addr : " << (struct sockaddr *) (serverInfo->ai_addr)->sa_data;
cout << "\n ai_addrlen:" << serverInfo->ai_addrlen << endl;
}// end tcpFlag == 2
}// end display
Server(char *myport,int tcpFlag)
{
//Default for both TCP and UDP ports
memset(&hints, 0, sizeof (hints));
port=myport;
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_UNSPEC;
local="127.0.0.1";
yes=1;
if(tcpFlag == 1)
{
cout << " Input myportTCP \n" << myport;
cout << " Assigned portTCP \n" << port;
hints.ai_socktype = SOCK_STREAM;
}//end tcpFlag
else if(tcpFlag == 2)
{
cout << " Input myportUDP \n" << myport;
cout << " Assigned portUDP \n" << port;
hints.ai_socktype = SOCK_DGRAM;
}
}//end Constructor Server
void testDNS(int tcpFlag)
{
if(tcpFlag == 1)
{
if ((status = getaddrinfo(local, port, &hints, &serverInfo)) != 0) {
fprintf(stderr, "TCP DNS getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
display(1);
}//end tcpFlag==1
else if(tcpFlag == 2)
{
if ((status = getaddrinfo(local, port, &hints, &serverInfo)) != 0) {
fprintf(stderr, "UDP DNS getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
display(2);
}//end tcpFlag==2
}//end DNS
int bindSocket(int tcpFlag)
{
if(tcpFlag == 1)
{
cout << "bindTCP()" << endl;
for(p = serverInfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("TCP server: socket");
continue;
}
cout << "bindTCP() got correct first structure" << endl;
//If the port is already in use, then re-use it
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes)) == -1) {
perror("TCP setsockopt");
exit(1);
}
cout << "bindTCP() got reused address" << endl;
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("TCP server: bind");
continue;
}
cout << "bindTCP() got binded" << endl;
//break;
freeaddrinfo(serverInfo); // all done with this structure
if (p == NULL) {
fprintf(stderr, "TCP server: failed to bind\n");
exit(1);
}
}//end for
return 1;
}//end if tcpflag == 1
else if(tcpFlag == 2)
{
cout << "bindUDP()" << endl;
for(p = serverInfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("UDP server: socket");
continue;
}
cout << "bindUDP() got correct first structure" << endl;
//If the port is already in use, then re-use it
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes)) == -1) {
perror("UDP setsockopt");
exit(1);
}
cout << "bindUDP() got reused address" << endl;
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("UDP server: bind");
continue;
}
cout << "bindUDP() got binded" << endl;
//break;
freeaddrinfo(serverInfo); // all done with this structure
if (p == NULL) {
fprintf(stderr, "UDP server: failed to bind\n");
exit(1);
}
receiveUDP(sockfd);
}// end for
return 2;
}// end else if tcpFlag == 2
} // end bind
int listenSocket(int tcpFlag)
{
if(tcpFlag == 1)
{
cout << "listenTCPServer listening" << endl;
if (listen(sockfd, BACKLOG) == -1) {
perror("TCP listen");
exit(1);
}
cout << "TCP server: waiting for connections...\n" ;
while(1) {
// main accept() loop
sin_size = sizeof their_addr;
newfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (newfd == -1) {
perror("TCP accept");
continue;
}
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);
printf("TCP server: got connection from %s\n", s);
if (!fork())// this is the child process
{
close(sockfd); // child doesn't need the listener
receiveTCP(newfd);
}
}//end While
return 1;
}//end tcpFlag == 1
/*
Listen is not defined for UDP, just connect is sufficient. Connect tells OS that there 'may' be future connections coming
on this UDP port. Remember UDP is unreliable and used for broadcast.
*/
else if(tcpFlag == 2)
{
cout << "UDP server: waiting for connections...\n" ;
//cout << "listenUDPServer listening" << endl;
/*if (listen(sockfd, BACKLOG) == -1) {
perror("UDP listen");
exit(1);
}
cout << "UDP server: waiting for connections...\n" ;
receiveUDP(sockfd);
while(1) {
// main accept() loop
sin_size = sizeof their_addr;
newfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (newfd == -1) {
perror("UDP accept");
continue;
}
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);
printf("UDP server: got connection from %s\n", s);
return 1;
close(newfd); // parent doesn't need this
}//end While*/
}
}//end listenSocket
void receiveTCP(int newfd)
{
int numbytes;
char buffer[MAXDATASIZE];
if ((numbytes = recv(newfd,buffer,MAXDATASIZE-1, 0)) == -1) {
perror("TCP recv");
exit(1);
}// endif
buffer[numbytes] = '\0';
printf("server: received '%s'\n",buffer);
close(newfd);
exit(0);
}//end receiveTCP
void receiveUDP(int newfd)
{
int numbytes;
char buffer[MAXDATASIZE];
addr_len = sizeof their_addr;
if ((numbytes = recvfrom(newfd,buffer,MAXDATASIZE-1, 0,(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("UDP recv");
exit(1);
}// endif
buffer[numbytes] = '\0';
printf("UDP server: received '%s'\n",buffer);
close(newfd);
//exit(0);
}//end receiveUDP
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
//Select Functions
};// end Server
int main(int c, char *argv[])
{
char tcp[MAXDATASIZE],udp[MAXDATASIZE];
int choice;
cout << " Enter 1 for TCP Server and Enter 2 for UDP server" << endl;
cin >> choice;
switch(choice)
{
case 1: {
cout << "\nEnter TCP port number\n";
cin >> tcp;
Server tcpServer(tcp,1);
tcpServer.testDNS(1);
if(tcpServer.bindSocket(1) < 0)
cout << "\nTCP Server bind not successful\n";
tcpServer.listenSocket(1);
}//end 1 switch
break;
case 2:{
cout << "Enter UDP port number";
cin >> udp;
Server udpServer(udp,2);
udpServer.testDNS(2);
if(udpServer.bindSocket(2) < 0)
cout << " UDP Server bind not successful" << endl;
//udpServer.listenSocket(2);
// udpServer.receiveUDP();
//udpServer.display(2);
break;
}//end 2 switch
default : cout << "\n Please Enter the correct choice\n";
}
}