-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_io.c
More file actions
82 lines (70 loc) · 1.61 KB
/
web_io.c
File metadata and controls
82 lines (70 loc) · 1.61 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
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#define RECV_LEN 256
#define RECV_BYTES 8
int main(int argc, char **argv)
{
struct addrinfo hints;
struct addrinfo *result;
char *host = "farnsworth.ecst.csuchico.edu";
char *port = "80";
int farn_socket;
int s;
char *msg = "GET /lab_docs/reset_instructions.pdf HTTP/1.0\n\n";
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
if((s = getaddrinfo(host, port, &hints, &result)) != 0)
{
perror("getaddressinfo");
return -1;
}
farn_socket = -1;
if((farn_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket");
return -1;
}
if(connect(farn_socket, result->ai_addr, result->ai_addrlen) == -1)
{
perror("Error connecting");
return -1;
}
freeaddrinfo(result);
send(farn_socket, msg, strlen(msg), 0);
char recv_msg[RECV_LEN];
int recv_flag;
int fd = open("local_file", O_WRONLY | O_APPEND | O_CREAT |
O_TRUNC, S_IRWXU);
memset(recv_msg, 0, strlen(recv_msg));
while((recv_flag = recv(farn_socket, recv_msg, RECV_LEN, 0)) > 0)
{
if(write(fd, recv_msg, recv_flag) <= 0)
{
perror("Error writing to file: ");
return 1;
}
memset(recv_msg, 0, strlen(recv_msg));
}
if(recv_flag < 0)
{
perror("Error receiving bytes");
return 1;
}
close(fd);
close(farn_socket);
return 0;
}