-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathExample.cpp
More file actions
89 lines (71 loc) · 1.72 KB
/
Example.cpp
File metadata and controls
89 lines (71 loc) · 1.72 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
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <Windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32")
void connect_wsa()
{
WSADATA wsaData;
char buf[8192] = {};
WSABUF DataBuf;
DataBuf.buf = buf;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET s = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
struct addrinfo *result;
getaddrinfo("www.example.com", "http", NULL, &result);
int ret = WSAConnect(s, result->ai_addr, sizeof(SOCKADDR), NULL, NULL, NULL, NULL);
if (ret == SOCKET_ERROR) {
closesocket(s);
WSACleanup();
return;
}
strcpy(buf, "GET / HTTP/1.0\r\nHost: www.example.com\r\n\r\n");
DataBuf.len = strlen(buf);
WSASend(s, &DataBuf, 1, NULL, 0, NULL, NULL);
Sleep(1000);
DWORD Flags = 0;
DataBuf.len = 8192;
WSARecv(s, &DataBuf, 1, NULL, &Flags, NULL, NULL);
puts(DataBuf.buf);
closesocket(s);
WSACleanup();
}
void connect_posix()
{
WSADATA wsaData;
char buf[8192] = {};
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET s = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
SOCKADDR_IN name;
name.sin_family = AF_INET;
name.sin_addr.s_addr = *(u_long *)gethostbyname("www.example.com")->h_addr_list[0];
name.sin_port = htons(80);
int ret = connect(s, (SOCKADDR *)&name, sizeof(name));
if (ret == SOCKET_ERROR) {
closesocket(s);
WSACleanup();
return;
}
strcpy(buf, "GET / HTTP/1.0\r\nHost: www.example.com\r\n\r\n");
send(s, buf, strlen(buf), 0);
Sleep(1000);
recv(s, buf, 8192, 0);
puts(buf);
closesocket(s);
WSACleanup();
}
void do_inet_addr()
{
int addr = inet_addr("192.168.0.1");
printf("%x\n", addr);
}
int main()
{
connect_wsa();
connect_posix();
do_inet_addr();
return 0;
}