-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcpclient.c
184 lines (157 loc) · 4.82 KB
/
tcpclient.c
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
/* TCPClient.c */
#include <unistd.h>
#include <strings.h>
#include <sys/types.h> /* for open */
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h> /* for gethostbyname */
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <sys/time.h> /* for gettimeofday */
#include <stdlib.h> /* for atoi */
#define TRUE 1
/* This is the client program. It opens a socket,
and initiates a TCP connection with the server.
The form of the command line is:
'tcpclient serverName serverPort fileName gatewayName gatewayPort' */
int main(int argc, char *argv[]){
int sock;
struct sockaddr_in server, client;
struct hostent *hp, *gethostbyname();
static int buflen = 1024;
char buf[1024];
int rval;
int seqno, length;
struct timeval sndtime;
struct timezone zone;
/* Get command line arguments */
if (argc != 6){
printf("Error: incorrect number of arguments\n");
printf("Usage: tcpclient serverName serverPort fileName gatewayName gatewayPort\n");
exit(1);
}
/* create socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0){
perror("opening stream socket");
exit(1);
}
/* connect socket using name specified at command line */
server.sin_family = AF_INET;
hp = gethostbyname(argv[4]);
if (hp == 0){
printf("%s: unknown host\n", argv[1]);
exit(1);
}
bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
server.sin_port = htons(atoi(argv[5]));
/* find out assigned port number and print out */
if (getsockname(sock, (struct sockaddr *)&client, (socklen_t*)&length)){
perror("getting sock name");
exit(1);
}
if (gettimeofday(&sndtime, &zone) < 0){
printf("Error: Client getting time\n");
}
printf("Client started on port #%d at time: %ld %ld\n", ntohs(client.sin_port),
sndtime.tv_sec, sndtime.tv_usec);
system("date");
if(connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0){
perror("Error: Client connecting stream socket");
exit(1);
}
if (gettimeofday(&sndtime, &zone) < 0){
printf("Error: Client getting time\n");
}
printf("Client start time: %ld %ld\n", ntohs(client.sin_port),
sndtime.tv_sec, sndtime.tv_usec);
bzero(buf, sizeof(buf));
system("date");
// write the hostname and port to the gateway
char serverName[255];
bzero(serverName, sizeof(serverName));
char serverPort[255];
static int length255 = 255;
bzero(serverPort, sizeof(serverPort));
bcopy(argv[1], serverName, length255);
bcopy(argv[2], serverPort, length255);
int rrval = 0;
if ((rrval = write(sock, serverPort, length255)) < 0){
perror("error writing serverName to gateway");
}
if ((rval = write(sock, serverPort, length255)) < 0){
perror("error writing portnumber to gayway");
}
// request the file from the gateway
char file[255];
bzero(file, sizeof(file));
bcopy(argv[3], file, length255);
if ((rrval = write(sock, file, length255)) < 0){
perror("error writing file name to gateway");
}
// Open the output file for writing
FILE *pFile = fopen("./outputfile", "w");
printf("sent the gateway the file :%s, host: %s, port: %s\n", pFile, serverName,
serverPort);
int receivedTotal = 0;
int receivedPackets = 0;
static int fileBufLen = 498;
/* Now read from socket and write to stdout */
do {
if ((rrval = read(sock, buf, buflen)) < 0){
perror("Client error reading message stream: ");
}else if (rval < 0){
perror("some other error:");
}else if (rval == 0){
printf("Client connection ending \n");
system("date");
}else{
sscanf(buf, "%d", &seqno);
receivedPackets++;
printf("Client received packet, size = %%d, seqno = %d\n", rval, seqno);
if((rval = fwrite((char*) &buf[2], 1, fileBufLen, pFile)) < 0){
perror("Client error writing packets to file\n");
}
}
system("date");
if (receivedPackets == 10){
// disconnect from gateway, reconnect, reset counter
printf("Client dropping connection\n");
system("date");
close(sock);
sleep(1);
/* create socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0){
perror("opening socket stream");
exit(1);
}
if (getsockname(sock, (struct sockaddr *)&client, (socklen_t*)&length)){
perror("getting socket name");
exit(1);
}
printf("Client reinitiating connection, sequence number = %d\n", seqno);
// sit in a loop and reconnect
while( connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0){
printf("Still connecting... \n");
}
// write to gateway
bzero(buf, sizeof(buf));
sprintf(buf, "%d", seqno);
if(( rval = write(sock, buf, buflen)) < 0){
perror("error writing to gateway: \n");
}
receivedPackets = 0;
}
} while(rval > 0);
if (gettimeofday(&sndtime, &zone) < 0){
perror("getting time");
}
printf("Client end time: %ld %ld\n", sndtime.tv_sec, sndtime.tv_usec);
printf("Received %d packets \n", seqno);
fclose(pFile);
close(sock);
exit(0);
}