-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecho-client-simple.cc
89 lines (74 loc) · 2.06 KB
/
echo-client-simple.cc
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
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
using namespace std;
int
main(int argc, char **argv)
{
// setup default arguments
int option;
int port = 3000;
string host = "localhost";
// process command line options using getopt()
// see "man 3 getopt"
while ((option = getopt(argc,argv,"h:p:")) != -1) {
switch (option) {
case 'p':
port = atoi(optarg);
break;
case 'h':
host = optarg;
break;
default:
cout << "client [-s IP address] [-p port]" << endl;
exit(EXIT_FAILURE);
}
}
// use DNS to get IP address
struct hostent *hostEntry;
hostEntry = gethostbyname(host.c_str());
if (!hostEntry) {
cout << "No such host name: " << host << endl;
exit(-1);
}
// setup socket address structure
struct sockaddr_in server_addr;
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
memcpy(&server_addr.sin_addr, hostEntry->h_addr_list[0], hostEntry->h_length);
// create socket
int server = socket(PF_INET,SOCK_STREAM,0);
if (server < 0) {
perror("socket");
exit(-1);
}
// connect to server
if (connect(server,(const struct sockaddr *)&server_addr,sizeof(server_addr)) < 0) {
perror("connect");
exit(-1);
}
// allocate buffer
int buflen = 1024;
char* buf = new char[buflen+1];
// read a line from standard input
string line;
while (getline(cin,line)) {
// write the data to the server
send(server, line.c_str(), line.length(), 0);
// read the response
memset(buf,0,buflen);
recv(server,buf,buflen,0);
// print the response
cout << buf << endl;
}
// Close socket
close(server);
}