-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhserver.cpp
257 lines (203 loc) · 7.86 KB
/
hserver.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
/*!
* Simple chat program (server side).cpp - http://github.com/hassanyf
* Version - 2.0.1
*
* Copyright (c) 2016 Hassan M. Yousuf
*/
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main()
{
/* ---------- INITIALIZING VARIABLES ---------- */
/*
1. client/server are two file descriptors
These two variables store the values returned
by the socket system call and the accept system call.
2. portNum is for storing port number on which
the accepts connections
3. isExit is bool variable which will be used to
end the loop
4. The server reads characters from the socket
connection into a dynamic variable (buffer).
5. A sockaddr_in is a structure containing an internet
address. This structure is already defined in netinet/in.h, so
we don't need to declare it again.
DEFINITION:
struct sockaddr_in
{
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
6. serv_addr will contain the address of the server
7. socklen_t is an intr type of width of at least 32 bits
*/
int client, server;
int portNum = 1500;
bool isExit = false;
int bufsize = 1024;
char buffer[bufsize];
struct sockaddr_in server_addr;
socklen_t size;
/* ---------- ESTABLISHING SOCKET CONNECTION ----------*/
/* --------------- socket() function ------------------*/
client = socket(AF_INET, SOCK_STREAM, 0);
if (client < 0)
{
cout << "\nError establishing socket..." << endl;
exit(1);
}
/*
The socket() function creates a new socket.
It takes 3 arguments,
a. AF_INET: address domain of the socket.
b. SOCK_STREAM: Type of socket. a stream socket in
which characters are read in a continuous stream (TCP)
c. Third is a protocol argument: should always be 0. The
OS will choose the most appropiate protocol.
This will return a small integer and is used for all
references to this socket. If the socket call fails,
it returns -1.
*/
cout << "\n=> Socket server has been created..." << endl;
/*
The variable serv_addr is a structure of sockaddr_in.
sin_family contains a code for the address family.
It should always be set to AF_INET.
INADDR_ANY contains the IP address of the host. For
server code, this will always be the IP address of
the machine on which the server is running.
htons() converts the port number from host byte order
to a port number in network byte order.
*/
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
server_addr.sin_port = htons(portNum);
/* ---------- BINDING THE SOCKET ---------- */
/* ---------------- bind() ---------------- */
if ((bind(client, (struct sockaddr*)&server_addr,sizeof(server_addr))) < 0)
{
cout << "=> Error binding connection, the socket has already been established..." << endl;
return -1;
}
/*
The bind() system call binds a socket to an address,
in this case the address of the current host and port number
on which the server will run. It takes three arguments,
the socket file descriptor. The second argument is a pointer
to a structure of type sockaddr, this must be cast to
the correct type.
*/
size = sizeof(server_addr);
cout << "=> Looking for clients..." << endl;
/* ------------- LISTENING CALL ------------- */
/* ---------------- listen() ---------------- */
listen(client, 1);
/*
The listen system call allows the process to listen
on the socket for connections.
The program will be stay idle here if there are no
incomming connections.
The first argument is the socket file descriptor,
and the second is the size for the number of clients
i.e the number of connections that the server can
handle while the process is handling a particular
connection. The maximum size permitted by most
systems is 5.
*/
/* ------------- ACCEPTING CLIENTS ------------- */
/* ----------------- listen() ------------------- */
/*
The accept() system call causes the process to block
until a client connects to the server. Thus, it wakes
up the process when a connection from a client has been
successfully established. It returns a new file descriptor,
and all communication on this connection should be done
using the new file descriptor. The second argument is a
reference pointer to the address of the client on the other
end of the connection, and the third argument is the size
of this structure.
*/
int clientCount = 1;
server = accept(client,(struct sockaddr *)&server_addr,&size);
// first check if it is valid or not
if (server < 0)
cout << "=> Error on accepting..." << endl;
while (server > 0)
{
strcpy(buffer, "=> Server connected...\n");
send(server, buffer, bufsize, 0);
cout << "=> Connected with the client #" << clientCount << ", you are good to go..." << endl;
cout << "\n=> Enter # to end the connection\n" << endl;
/*
Note that we would only get to this point after a
client has successfully connected to our server.
This reads from the socket. Note that the read()
will block until there is something for it to read
in the socket, i.e. after the client has executed a
the send().
It will read either the total number of characters
in the socket or 1024
*/
cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '*';
isExit = true;
}
} while (*buffer != '*');
do {
cout << "\nServer: ";
do {
cin >> buffer;
send(server, buffer, bufsize, 0);
if (*buffer == '#') {
send(server, buffer, bufsize, 0);
*buffer = '*';
isExit = true;
}
} while (*buffer != '*');
cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer == '*';
isExit = true;
}
} while (*buffer != '*');
} while (!isExit);
/*
Once a connection has been established, both ends
can both read and write to the connection. Naturally,
everything written by the client will be read by the
server, and everything written by the server will be
read by the client.
*/
/* ---------------- CLOSE CALL ------------- */
/* ----------------- close() --------------- */
/*
Once the server presses # to end the connection,
the loop will break and it will close the server
socket connection and the client connection.
*/
// inet_ntoa converts packet data to IP, which was taken from client
cout << "\n\n=> Connection terminated with IP " << inet_ntoa(server_addr.sin_addr);
close(server);
cout << "\nGoodbye..." << endl;
isExit = false;
exit(1);
}
close(client);
return 0;
}