-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.c
48 lines (41 loc) · 1.14 KB
/
socket.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
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
int main(int ac, char *av[])
{
int sock_desc;
struct sockaddr_in server;
char *request;
char server_reply[2000];
sock_desc = socket(AF_INET, SOCK_STREAM, 0);
if ( sock_desc == -1 ) {
perror("can't creat socket:");
} else {
printf("socket created successful:%d\n", sock_desc);
}
server.sin_addr.s_addr = inet_addr("74.125.235.20");
server.sin_family = AF_INET;
server.sin_port = htons(80);
if (connect(sock_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("can't connect to server");
return 1;
} else {
printf("connected to server successfully\n");
}
request = "GET / HTTP/1.0\r\n\r\n";
if (send(sock_desc, request, strlen(request), 0) < 0) {
perror("can't send request\n");
return 2;
} else {
printf("request send ok\n");
}
if (recv(sock_desc, server_reply, 2000, 0) < 0){
perror("can't recv");
return 3;
} else {
printf("recv ok:\n%s", server_reply);
}
close(sock_desc);
return 0;
}