-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpClient.c
More file actions
187 lines (146 loc) · 3.93 KB
/
httpClient.c
File metadata and controls
187 lines (146 loc) · 3.93 KB
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
185
186
187
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
/* consts and definitions */
#define MAX_LENGTH 256
#define MAX_MESSAGE_LENGTH 4096
/* prototypes */
void requestFile();
unsigned char isConnected();
int main(int argc, char **argv)
{
/* strings for user input
* httpAddress = address of HTTP server to connect
* fileRequest = name of file to request from server
*/
char* httpAddress = (char*)calloc(MAX_LENGTH, sizeof(char));
char* fileRequest = (char*)calloc(MAX_LENGTH, sizeof(char));
/* server port */
unsigned short* httpPort = (unsigned short*)calloc(1, sizeof(short));
/* boolean to check if we're still connected to the server */
char isConnected = 0;
/* socket declarations */
int sockfd;
struct sockaddr_in serverAddr;
struct hostent *server;
/* get HTTP address from user */
printf("Hello! I'm a talking string!\n");
printf("Enter address of HTTP server: ");
scanf("%256s", httpAddress);
printf("\nYou entered the address [%s]\n", httpAddress);
printf("String length is %d\n", (int)strlen((const char*)httpAddress));
printf("Enter port on HTTP server: ");
scanf("%hu", httpPort);
printf("\nYou entered the port %d\n", *httpPort);
server = gethostbyname((char*)httpAddress);
/* check if the address is valid */
if(server == NULL)
{
printf("\nError! No such host at \"%s\"\n", httpAddress);
exit(1);
}
/* create the client socket */
memset(&serverAddr, 0, sizeof(serverAddr));
bcopy((char*)server->h_addr,(char*)&serverAddr.sin_addr.s_addr,
server->h_length);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(*httpPort);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* check the socket */
if(sockfd < 0)
{
printf("\nError! Could not open socket\n");
exit(1);
}
/* test connection */
if(connect(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
{
printf("\nError! Connection to server failed!\n");
return 0;
}
else
{
printf("\nHey! We're connected!!!\n");
}
int req;
char *buffer = (char*)calloc(MAX_MESSAGE_LENGTH, sizeof(char));
printf("\nWhat file do you want? ");
scanf("%256s", fileRequest);
bzero(buffer, MAX_MESSAGE_LENGTH);
sprintf(buffer, "GET %s HTTP/1.1\r\n", fileRequest);
req = write(sockfd, buffer, strlen(buffer));
if(req < 0)
{
printf("\nError! Could not write to socket!\n");
exit(1);
}
else
{
while(write(sockfd, buffer, sizeof(buffer)) > 0);
}
bzero(buffer, MAX_MESSAGE_LENGTH);
req = read(sockfd, buffer, MAX_MESSAGE_LENGTH);
if(req < 0)
{
printf("\nError! Could not read from socket!\n");
exit(1);
}
else
{
while(read(sockfd, buffer, sizeof(buffer)) > 0);
}
printf("\n\n\n\n\n");
printf("Buffer: [%s]\n", buffer);
bzero(buffer, MAX_MESSAGE_LENGTH);
bzero(fileRequest, MAX_LENGTH);
printf("Buffer is now [%s]\n", buffer);
printf("Req is now [%s]\n", fileRequest);
printf("\nWhat file do you want? ");
scanf("%256s", fileRequest);
bzero(buffer, MAX_MESSAGE_LENGTH);
sprintf(buffer, "GET %s HTTP/1.1\r\n", fileRequest);
req = write(sockfd, buffer, strlen(buffer));
if(req < 0)
{
printf("\nError! Could not write to socket!\n");
exit(1);
}
else
{
while(write(sockfd, buffer, sizeof(buffer)) > 0);
}
bzero(buffer, MAX_MESSAGE_LENGTH);
req = read(sockfd, buffer, MAX_MESSAGE_LENGTH);
if(req < 0)
{
printf("\nError! Could not read from socket!\n");
exit(1);
}
else
{
while(read(sockfd, buffer, sizeof(buffer)) > 0);
}
printf("Buffer: [%s]\n", buffer);
/* while we're connected, allow the user to keep requesting for additional
files */
/*
while(isConnected())
{
requestFile();
}
return 0;
*/
return 0;
}
void requestFile()
{
}
unsigned char isConnected();