-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
executable file
·130 lines (113 loc) · 3.23 KB
/
main.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
#include <stdio.h>
#include <winsock.h>
#define PORT 80
#define USERAGENT "ApOgEE MinGW Socket Client 1.0"
char *build_get_query(char *, char *);
int main(int argc, char *argv[])
{
int client_socket;
char *host;
struct hostent *hent;
int iplen = 15; //XXX.XXX.XXX.XXX
struct sockaddr_in *remote;
int tmpres;
char *get;
char buf[BUFSIZ+1];
printf("Socket client example\n");
// create tcp socket
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
client_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (client_socket < 0) {
printf("client_socket = %d\n",client_socket);
perror("Can't create TCP socket\n");
exit(1);
}
// get host ip
host = "www.ultratone.com.my";
printf("Host: %s\n",host);
char *ip = (char *)malloc(iplen+1);
memset(ip, 0, iplen+1);
if((hent = gethostbyname(host)) == NULL)
{
perror("Can't get IP");
exit(1);
}
ip = inet_ntoa(*(struct in_addr *)*hent->h_addr_list);
printf("The IP: %s\n",ip);
// setup remote socket
remote = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *));
remote->sin_family = AF_INET;
printf("s_addr:%d\n",remote->sin_addr.s_addr);
remote->sin_addr.s_addr = inet_addr(ip);
remote->sin_port = htons(PORT);
printf("s_addr:%d\n",remote->sin_addr.s_addr);
// have to read
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms737625(v=vs.85).aspx
// connect socket
if(connect(client_socket, (struct sockaddr *)remote, sizeof(struct sockaddr)) == SOCKET_ERROR){
closesocket(client_socket);
perror("Could not connect");
WSACleanup();
exit(1);
}
// prepare query
get = build_get_query(host,"/");
printf("query: \n%s\n",get);
//Send the query to the server
int sent = 0;
while(sent < strlen(get)) {
tmpres = send(client_socket, get+sent, strlen(get)-sent, 0);
if(tmpres == -1) {
perror("Can't send query");
exit(1);
}
sent += tmpres;
}
//now it is time to receive the page
memset(buf, 0, sizeof(buf));
int htmlstart = 0;
char * htmlcontent;
while((tmpres = recv(client_socket, buf, BUFSIZ, 0)) > 0) {
if(htmlstart == 0) {
/* Under certain conditions this will not work.
* If the \r\n\r\n part is splitted into two messages
* it will fail to detect the beginning of HTML content
*/
htmlcontent = strstr(buf, "\r\n\r\n");
if(htmlcontent != NULL){
htmlstart = 1;
htmlcontent += 4;
}
} else {
htmlcontent = buf;
}
if(htmlstart) {
fprintf(stdout, htmlcontent);
}
memset(buf, 0, tmpres);
}
if(tmpres < 0) {
perror("Error receiving data");
}
free(get);
free(remote);
closesocket(client_socket);
WSACleanup();
printf("Program end");
return 0;
}
char *build_get_query(char *host, char *page)
{
char *query;
char *getpage = page;
char *tpl = "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n";
if(getpage[0] == '/'){
getpage = getpage + 1;
fprintf(stderr,"Removing leading \"/\", converting %s to %s\n", page, getpage);
}
// -5 is to consider the %s %s %s in tpl and the ending \0
query = (char *)malloc(strlen(host)+strlen(getpage)+strlen(USERAGENT)+strlen(tpl)-5);
sprintf(query, tpl, getpage, host, USERAGENT);
return query;
}