ktcc: added sample http_tcp_demo - working with http using only tcp sockets

git-svn-id: svn://kolibrios.org@8448 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Rustem Gimadutdinov (rgimad) 2020-12-18 18:38:07 +00:00
parent 44d9aced87
commit 045e0dbdd2
2 changed files with 64 additions and 1 deletions

View File

@ -19,7 +19,8 @@ all:
$(CC) $(CFLAGS) clayer/dialog.c -lck -ldialog -o clayer/dialog.kex
$(CC) $(CFLAGS) net/tcpsrv_demo.c -lck -o net/tcpsrv_demo.kex
$(CC) $(CFLAGS) net/nslookup.c -lck -lnetwork -o net/nslookup.kex
$(CC) $(CFLAGS) net/nslookup.c -lck -lnetwork -o net/nslookup.kex
$(CC) $(CFLAGS) net/http_tcp_demo.c -lck -lnetwork -o net/http_tcp_demo.kex
$(CC) $(CFLAGS) tinygl/fps.c tinygl/gears.c -o tinygl/gears.kex -ltinygl -lck

View File

@ -0,0 +1,62 @@
#include <net/network.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
con_init_console_dll();
networklib_init();
con_set_title("http request demo using raw sockets");
char *host = "kolibrios.org";
int port = 80;
printf("Connecting to %s on port %d\n", host, port);
struct addrinfo *addr_info;
if (getaddrinfo(host, 0, 0, &addr_info) != 0) {
printf("Host %s not found!\n", host);
freeaddrinfo(addr_info);
exit(-1);
}
unsigned int host_sin_addr = addr_info->ai_addr->sin_addr;
printf("IP address of %s is %s\n", host, inet_ntoa(host_sin_addr));
//printf("Host port = %d\n", addr_info->ai_addr->sin_addr >> 8);
freeaddrinfo(addr_info);
char request[256];
sprintf(request, "GET /en/ HTTP/1.1\r\nHost: %s\r\n\r\n", host);
printf("request = %s\n", request);
int sock = socket(AF_INET4, SOCK_STREAM, IPPROTO_TCP);
sockaddr sock_addr;
sock_addr.sin_family = AF_INET4;
sock_addr.sin_port = 80 << 8; // dirty hack, normally need to use htons(80) but there is no such here in libraries
sock_addr.sin_addr = host_sin_addr;
sock_addr.sin_zero = 0;
puts("Connecting...\n");
if (connect(sock, &sock_addr, sizeof(sock_addr)) != 0) {
printf("Connection failed, err_code = %d\n", err_code);
exit(err_code);
}
puts("Connected successfully\n");
puts("Sending request...\n");
if (send(sock, request, strlen(request), MSG_NOFLAG) == -1) {
printf("Sending failed, err_code = %d\n", err_code);
exit(err_code);
}
puts("Request sended successfully, waiting for response...\n");
char buf[512 + 1];
if (recv(sock, buf, 512, MSG_NOFLAG) == -1) {
printf("Receive failed, err_code = %d\n", err_code);
exit(err_code);
}
printf("Response = %s\n", buf);
con_exit(0);
return 0;
}