-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsockets.c
More file actions
55 lines (39 loc) · 868 Bytes
/
sockets.c
File metadata and controls
55 lines (39 loc) · 868 Bytes
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
/* sockets.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
/* connect() import */
#include <unistd.h>
/* inet_add imports */
#include <netinet/in.h>
#define IP_ADDRESS "172.217.170.196" /* www.google.com */
#define PORT 80 /* http */
int main()
{
int s;
struct sockaddr_in sock;
char buf[512];
char *data;
data = "HEAD / HTTP/1.0\n\n";
s = socket(AF_INET, SOCK_STREAM, 0);
if ( s < 0 ){
printf("socket() error\n");
return -1;
}
sock.sin_addr.s_addr = inet_addr(IP_ADDRESS);
sock.sin_port = htons(PORT);
sock.sin_family = AF_INET;
if (connect(s, (struct sockaddr *)&sock, sizeof(struct sockaddr_in)) != 0){
printf("connect() error \n");
close(s);
return -1;
}
write(s, data, strlen(data));
read(s, buf, 511);
close(s);
printf("\n%s", buf);
return 0;
}