-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhclient.cpp
More file actions
185 lines (140 loc) · 5.24 KB
/
hclient.cpp
File metadata and controls
185 lines (140 loc) · 5.24 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
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
/*!
* Simple chat program (client 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>
#include <netdb.h>
using namespace std;
int main()
{
/* ---------- INITIALIZING VARIABLES ---------- */
/*
1. client is a file descriptor to 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 client 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
*/
int client;
int portNum = 1500; // NOTE that the port number is same for both client and server
bool isExit = false;
int bufsize = 1024;
char buffer[bufsize];
char* ip = "127.0.0.1";
struct sockaddr_in server_addr;
client = socket(AF_INET, SOCK_STREAM, 0);
/* ---------- ESTABLISHING SOCKET CONNECTION ----------*/
/* --------------- socket() function ------------------*/
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 client 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.
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_port = htons(portNum);
// this function returns returns 1 if the IP is valid
// and 0 if invalid
// inet_pton converts IP to packets
// inet_ntoa converts back packets to IP
//inet_pton(AF_INET, ip, &server_addr.sin_addr);
/*if (connect(client,(struct sockaddr *)&server_addr, sizeof(server_addr)) == 0)
cout << "=> Connection to the server " << inet_ntoa(server_addr.sin_addr) << " with port number: " << portNum << endl;*/
/* ---------- CONNECTING THE SOCKET ---------- */
/* ---------------- connect() ---------------- */
if (connect(client,(struct sockaddr *)&server_addr, sizeof(server_addr)) == 0)
cout << "=> Connection to the server port number: " << portNum << endl;
/*
The connect function is called by the client to
establish a connection to the server. It takes
three arguments, the socket file descriptor, the
address of the host to which it wants to connect
(including the port number), and the size of this
address.
This function returns 0 on success and -1
if it fails.
Note that the client needs to know the port number of
the server but not its own port number.
*/
cout << "=> Awaiting confirmation from the server..." << endl; //line 40
recv(client, buffer, bufsize, 0);
cout << "=> Connection confirmed, you are good to go...";
cout << "\n\n=> Enter # to end the connection\n" << endl;
// Once it reaches here, the client can send a message first.
do {
cout << "Client: ";
do {
cin >> buffer;
send(client, buffer, bufsize, 0);
if (*buffer == '#') {
send(client, buffer, bufsize, 0);
*buffer = '*';
isExit = true;
}
} while (*buffer != 42);
cout << "Server: ";
do {
recv(client, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '*';
isExit = true;
}
} while (*buffer != 42);
cout << endl;
} while (!isExit);
/* ---------------- 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.
*/
cout << "\n=> Connection terminated.\nGoodbye...\n";
close(client);
return 0;
}