-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-tcp.cpp
180 lines (115 loc) · 3.31 KB
/
client-tcp.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
/************************* INCLUDES **************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
using namespace std;
/************************* MACROS ****************************/
#define MAXBUF 1000000
#define FALSE 0
#define TRUE 1
/************************* VARIABLES ****************************/
struct sockaddr_in sockme;
int s;
int type;
int portnum;
char *hostnum;
char bufout[MAXBUF+1];
char bufin[MAXBUF+1];
char c, *cp;
char filename[100];
int result;
int n;
int main(int argc, char *argv[])
{
// Get the port number and dotted decimal address of server
// from the command-line arguments
if (argc == 3)
{
hostnum = argv[1];
portnum = atoi(argv[2]);
}
else
{
cout << "usage: client-tcp <hostnum> <portnum>" << endl;
exit(-1);
}
// PASTE CODE FROM STEP #1 HERE
sockme.sin_family = AF_INET;
if ((sockme.sin_addr.s_addr = inet_addr(hostnum)) == INADDR_NONE)
{
cout << "Invalid dotted decimal address" << endl;
exit(-1);
}
// PASTE CODE FROM STEP #2 HERE
sockme.sin_port = htons(portnum);
// Get message from user, send to server
char message[100] = "";
// Run indefinitely, until the user enters the /QUIT command
while(true)
{
// PASTE CODE FROM STEP #3 HERE
s = socket(PF_INET, SOCK_STREAM, 0);
if ((result = connect (s, (struct sockaddr *) &sockme, sizeof(sockme))))
{
perror("Connect failed\n");
close (s);
exit(-1);
};
cout << "--------------------------------------" << endl;
cout << "Connected to server at IP address = " << hostnum;
cout << " and Port = " << portnum << endl;
cout << "Input your request (99 characters max). Enter /QUIT when finished:" << endl;
// Read a message from the standard input
string msg;
getline (cin, msg);
if (msg == "/QUIT")
{
// Close the socket
close(s);
break;
}
// Send the message
int total_characters = msg.length();
cout << "Message: " << msg << endl;
cout << "Message size: " << total_characters << endl;
// PASTE CODE FROM STEP #4 HERE
result = send (s, msg.c_str(), total_characters, 0);
if (result == -1)
{
perror("Send failed");
exit(-1);
}
// Now half close the connection so the server knows
// the request is complete
result = shutdown(s, 1);
// Read response from the server, send to standard output
char *bp = bufin;
int cnt = MAXBUF;
// PASTE CODE FROM STEP #5 HERE
while ( (n = recv (s, bp, cnt, 0)) > 0 )
{
bp += n;
cnt = MAXBUF - (bp - bufin);
}
if (n != -1)
{
*bp = '\0';
cout << "Response was: " << bufin << endl;
}
else
{
perror("recv failed");
exit(-1);
}
// Close the socket
close(s);
}
return 0;
}