Created
July 10, 2021 19:29
-
-
Save LinArcX/a8e5af9244bb5b8e54bf7dc864ca5be3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is the code that i wrote | |
------------------------------------------------------ | |
#include <stdio.h> | |
#include <winsock2.h> | |
#pragma warning(disable : 4996) | |
#pragma comment(lib, "Ws2_32.lib") // Winsock Library | |
int | |
net_init() | |
{ | |
WSADATA wsa; | |
SOCKET s; | |
struct sockaddr_in server; | |
char *message, server_reply[2000]; | |
int recv_size; | |
printf("1. Initialising Winsock\n"); | |
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { | |
printf("Failed. Error Code : %d", WSAGetLastError()); | |
return 1; | |
} | |
printf("2. Initialised\n"); | |
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { | |
printf("Could not create socket : %d", WSAGetLastError()); | |
} | |
printf("3. Socket created\n"); | |
// developer.mozilla.org = 13.32.219.47 | |
// www.goodreads.com = 52.94.237.126 | |
server.sin_addr.s_addr = inet_addr("52.94.237.126"); | |
server.sin_family = AF_INET; | |
server.sin_port = htons(80); | |
// Connect to remote server | |
if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0) { | |
puts("connect error"); | |
return 1; | |
} | |
puts("4. Connected\n"); | |
// Send some data | |
message = "GET / HTTP/1.1\r\n" | |
"Connection: close\r\n" | |
"Cache-Control: no-cache\r\n\r\n"; | |
if (send(s, message, strlen(message), 0) < 0) { | |
puts("Send failed"); | |
return 1; | |
} | |
puts("5. Data Send\n"); | |
// Receive a reply from the server | |
if ((recv_size = recv(s, server_reply, 2000, 0)) == SOCKET_ERROR) { | |
puts("recv failed"); | |
} | |
puts("6. Reply received\n"); | |
// Add a NULL terminating character to make it a proper string before printing | |
server_reply[recv_size] = '\0'; | |
puts(server_reply); | |
return 0; | |
} | |
int | |
main(int argc, char *argv[]) | |
{ | |
net_init(); | |
return 0; | |
} | |
------------------------- | |
And this is the result that i get: | |
1. Initialising Winsock | |
2. Initialised | |
3. Socket created | |
4. Connected | |
5. Data Send | |
6. Reply received | |
HTTP/1.1 301 Moved Permanently | |
Date: Sat, 10 Jul 2021 19:27:08 GMT | |
Server: Server | |
Location: https://www.goodreads.com/ | |
Content-Length: 234 | |
Content-Type: text/html; charset=iso-8859-1 | |
Connection: close | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment