forked from acmiitr/socket-programming-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
90 lines (70 loc) · 1.92 KB
/
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
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[])
{
if (argc != 3)
{
cerr << "Missing hostname and port number\n";
exit(0);
}
char *serverName = argv[1];
int port = atoi(argv[2]);
char message[1500];
struct hostent *host = gethostbyname(serverName);
sockaddr_in clientSocket;
clientSocket.sin_family = AF_INET;
clientSocket.sin_port = htons(port);
clientSocket.sin_addr = **(struct in_addr **)host->h_addr_list;
int clientSocketID = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocketID < 0)
{
cout << "Unsuccessful socket()\n";
exit(0);
}
int status = connect(clientSocketID, (struct sockaddr *)&clientSocket, sizeof(clientSocket));
if (status < 0)
{
cout << "Unsuccessful connect()\n";
exit(0);
}
cout << "Connection Successful\n";
recv(clientSocketID, (char *)&message, sizeof(message), 0);
if (!strcmp(message, "exit"))
{
cout<<"Session terminated"<<"\n";
return 0;
}
cout<<message<<"\n";
while (true)
{
string data;
cout << "Client: ";
//cin>>data;
getline(cin, data);
strcpy(message, data.c_str());
int y = send(clientSocketID, (char *)&message, sizeof(message), 0);
if (data=="exit" || y==-1)
{
cout<<"Session terminated"<<"\n";
break;
}
cout << "Server: ";
int x = recv(clientSocketID, (char *)&message, sizeof(message), 0);
if (!strcmp(message, "exit") || x==-1)
{
cout<<"Session terminated"<<"\n";
break;
}
cout<<message<<"\n";
}
close(clientSocketID);
return 0;
}