-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.cpp
50 lines (41 loc) · 983 Bytes
/
client.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
#include "headerFiles/client.h"
// singeltone
Client Client::instance;
Client& Client::getinstance()
{
return instance;
}
int Client::client(string ip, string port)
{
struct sockaddr_in serv_addr;
// converting the string 'port' to a int PORT
int PORT = stoi(port);
//converting the string 'ip' to a char* IP
char *IP = &ip[0];
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary
if (inet_pton(AF_INET, IP, &serv_addr.sin_addr) <= 0)
{
printf(
"\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
return 1;
}
void Client::send(const char* message)
{
::send(sock, message, strlen(message), 0);
valread = read(sock, buffer, 1024);
}