Simple C Sockets HTTP Program Can't Find Resource: 404 - c

I have spring-boot server that I am running in AWS. The server works fine. I can access it using chrome, postman, and curl with no issues. However, I have an embedded device that is running C and I am using sockets to try to connect to my server. The embedded device is running Linux so I can use curl to talk to the server with no issues. However, my C sockets code cannot seem to find the resource on the server. I keep getting 404's.
Here is my embedded client code,
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <errno.h>
int main() {
// Define some parameters
int sockfd, bytes_read;
struct sockaddr_in dest;
char buffer[4000];
char hdr[1000];
// Create Server Client Strings
bzero(hdr, sizeof(hdr));
strcpy(hdr, "GET /hello HTTP/1.1\r\n");
strcat(hdr, "Host: 52.200.39.81\r\n\r\n");
// Clean things up a bit before sarting
printf("\n\n");
// Create Socket
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
printf("Socket not created\n");
return 0;
}
// Initialize server address/port struct
bzero(&dest, sizeof(dest));
// *** Added this line to fix the code ***
dest.sin_addr.s_addr = inet_addr("52.200.39.81", &dest.sin_addr.s_addr);
dest.sin_family = AF_INET;
dest.sin_port = htons(8080);
if ( inet_addr("52.200.39.81", &dest.sin_addr.s_addr) == 0 ) {
printf("Incorrect Address Expression\n");
return 0;
}
// Connect Socket
if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 ) {
printf("Socket Connection Failed\n");
close(sockfd);
return 0;
}
// Send data
if (send(sockfd, hdr, strlen(hdr), 0) < 0) {
printf("Send Data Failed\n");
return 0;
}
printf("Socket successfully sent\n");
printf("\nSend Message - TxBufferSize = %d\n\n",strlen(hdr));
printf("%s", hdr);
bzero(buffer, sizeof(buffer));
bytes_read = recv(sockfd, buffer, sizeof(buffer), 0);
if (bytes_read < 0) {
printf("Read Data Failed\n");
}
if (bytes_read > 0) {
// Print out receive buffer
printf("\n\nReceived Message -- RxSize = %d \n\n", strlen(buffer));
printf("%s", buffer);
}
if (bytes_read == 0) {
printf("No Read Bytes Received\n");
}
/*---Clean up---*/
close(sockfd);
return 0;
}
Here is what I get back,
debian#beaglebone:~$ ./helloworld
Socket successfully sent
***** Send Message -- TxBufferSize = 80 *****
GET http://52.200.39.81:8080/hello HTTP/1.1
Host: 52.200.39.81
accept: */*
***** Received Message -- RxBufferSize = 467 *****
HTTP/1.1 404 Not Found
Date: Sat, 02 Apr 2016 17:36:37 GMT
Server: Apache/2.2.22 (Debian)
Vary: Accept-Encoding
Content-Length: 283
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /hello was not found on this server.</p>
<hr>
<address>Apache/2.2.22 (Debian) Server at 52.200.39.81 Port 8080</address>
</body></html>
debian#beaglebone:~$
If I use the curl command on the embedded device,
debian#beaglebone:~$ curl 52.200.39.81:8080/hello
Greetings WebServiceTest1 -- ServiceCount = 11
debian#beaglebone:~$
I get the correct response from the server. So I am confident that the embedded device is talking to my server. Just can't seem to get the sockets code to work. Any help would be appreciated.

Ok, I found out what the problem was. I did not set the socket address. A very simple omission but that what the problem was. I have updated the code and it is now working. I added gethostbyname to make the code more standardized. The new lines of code are in the comment section "// Initialize server address/port struct". I would like to thank Lilas for providing me the reference code that led me to the omission of the dest.sin_addr.s_addr line. Below is the corrected code.
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
int main() {
// Define some parameters
int sockfd, bytes_read;
struct sockaddr_in dest;
char *hostname = "skmdev1.net";
struct hostent *hostent;
char buffer[4000];
char hdr[1000];
// Create Server Client Strings
bzero(hdr, sizeof(hdr));
strcpy(hdr, "GET /hello HTTP/1.1\r\n");
strcat(hdr, "Host: skmdev1.net\r\n\r\n");
// Clean things up a bit before sarting
printf("\n\n");
// Build the address
hostent = gethostbyname(hostname);
if (hostent == NULL) {
printf("error: gethostbyname(\"%s\")\n", hostname);
return 0;
}
// Create Socket
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
printf("Socket not created\n");
return 0;
}
// Initialize server address/port struct
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(8080);
dest.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr*)hostent->h_addr));
if ( dest.sin_addr.s_addr == INADDR_NONE ) {
printf("Incorrect Address Expression\n");
return 0;
}
// Connect Socket
if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 ) {
printf("Socket Connection Failed\n");
close(sockfd);
return 0;
}
// Send data
if (send(sockfd, hdr, strlen(hdr), 0) < 0) {
printf("Send Data Failed\n");
return 0;
}
printf("Socket successfully sent\n");
printf("\nSend Message - TxBufferSize = %d\n\n",strlen(hdr));
printf("%s", hdr);
bzero(buffer, sizeof(buffer));
bytes_read = recv(sockfd, buffer, sizeof(buffer), 0);
if (bytes_read < 0) {
printf("Read Data Failed\n");
}
if (bytes_read > 0) {
// Print out receive buffer
printf("\n\nReceived Message -- RxSize = %d \n\n", strlen(buffer));
printf("%s\n\n", buffer);
}
if (bytes_read == 0) {
printf("No Read Bytes Received\n");
}
/*---Clean up---*/
close(sockfd);
return 0;
}

Related

Problem displaying png file from web server to client web browser in C

I am implementing a web server in C and trying to display different types of files to a client browser such as Chrome. I got it to work for files such as txt and html, however I can't seem to get it to work for png files.
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#define PORT 10000
int main() {
FILE* file;
char* file_name = "img.png";
int file_size;
file = fopen(file_name, "r");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fseek(file, 0, SEEK_END);
file_size = ftell(file);
rewind(file);
// need http_header for proper HTTP response to client or there will be errors
char http_header[2048] = {0};
sprintf(http_header, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nConnection: close\r\nContent-Type: image/png\r\n\r\n", file_size);
printf("%s", http_header);
// buffer for client HTTP request
char buffer[1024] = {0};
int server_fd;
// defining socket's port and IP address
struct sockaddr_in address;
int addrlen = sizeof(address);
// htons host-to-net short (16-bit) translation
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// options for the socket file descriptor
int opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
// bind socket to the port and IP address
if (bind(server_fd, (struct sockaddr *) &address,
sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// server starts listening and can have up to 3 max clients queued for backlog
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
// client socket
int new_socket;
int valread;
// while loop cause server will keep listening for requests
while(1) {
// address and addrlen are result arguments that will have the client's info
// we redefined address and addrlen and we can probably use this later
if ((new_socket = accept(server_fd, (struct sockaddr *) &address,
(socklen_t * ) & addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// we read in the client's HTTP request just for console output purposes
valread = read(new_socket, buffer, 1024);
printf("%s\n", buffer);
// we send client socket the HTTP response
send(new_socket, http_header, sizeof(http_header), 0);
//send(new_socket, ht, sizeof(response), 0);
char dbuffer[4096];
memset(dbuffer, 0, 4096);
long bytes_read;
if (file > 0) {
while ((bytes_read = fread(dbuffer, 1, 4096, file)) > 0) {
if (send(new_socket, dbuffer, sizeof(dbuffer), 0) < 0) {
perror("Error\n");
return 1;
}
}
}
close(new_socket);
}
fclose(file);
//printf("Hello message sent\n");
return 0;
}
When I connect to http://localhost:10000/, I just get a black screen. Sometimes, I get an occasional small white square in the center. When I am checking the HTTP response sent to my client, I see this:
HTTP/1.1 200 OK
Content-Length: 177589
Connection: close
Content-Type: image/png
Along with that, I see that I am getting a ERR_CONTENT_LENGTH_MISMATCH error.
send(new_socket, http_header, sizeof(http_header), 0); should be strlen(http_header) instead of sizeof.
if (file > 0) { doesn't do anything, you already check if the fil exist after you open it, and as it's a pointer you only want to check if it's NULL or not.
fread() returns a size_t not a long (they may be the same type of course).
You have use bytes_read instead of sizeof(dbuffer) in send() to a short read results in a short write (but see refinement for partial writes which are normal).
(not fixed) Browsers may request other files. Firefox, for instance, asks for a /favicon.ico so you want to reject that with a 403. With the above changes curl returns file as is.
As you run that send in a while(1) you probably want to move the rewind(file) into that while-loop and you may need to clearerr(file) too.
size_t bytes_read;
do {
bytes_read = fread(dbuffer, 1, BUFFER_LEN, file);
ssize_t bytes_sent;
for(size_t n = 0; n < bytes_read; n += bytes_sent) {
bytes_sent = send(new_socket, dbuffer + n, bytes_read - n, 0);
if(bytes_sent == -1) {
perror("send failed");
goto err;
}
}
// if feof(file) then we just finished reading the file,
// otherwise we may want to perror() something.
} while(bytes_read == BUFFER_LEN);
err:
close(new_socket);
clearerr(file);
rewind(file);

TCP socket hangs when reading response from custom HTTP request - C

I have the following code that I have written which is suppose to send a simple http request over a TCP socket, I get a response but as soon as I try to read in the loop it hangs, in the 2nd read operation (tried it manually)
if anyone has an idea of why this might fail I will appreciate it a lot
attached below is the entire code
I am running the program like this: ./http_client yahoo.com
I get this response text at first:
HTTP/1.1 301 Moved Permanently
Date: Sat, 06 Aug 2022 08:07:11 GMT
Connection: keep-alive
Server: ATS
Cache-Control: no-store, no-cache
Content-Type: text/html
Content-Language: en
X-Frame-Options: SAMEORIGIN
Location: https://www.yahoo.com/
Content-Length: 8
redirect
and then it hangs and closes the socket, it shouldn't hang at all, it should run and exit without a delay or anything
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
int main(int argc, char *argv[]) {
int sockfd;
struct sockaddr_in cli_name;
struct sockaddr_in *saddr;
char *hostname;
struct addrinfo *res;
int port = 80;
if (argc != 2) {
perror("Usage: establish tcp connection to: <hostname>\n");
exit(1);
}
hostname = argv[1];
printf("Client is alive and establishing socket connection %s.\n", hostname);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Error opening channel");
close(sockfd);
exit(1);
}
if (0 != getaddrinfo(hostname, NULL, NULL, &res)) {
fprintf(stderr, "Error in resolving hostname %s\n", hostname);
exit(1);
}
bzero(&cli_name, sizeof(cli_name));
cli_name.sin_family = AF_INET;
saddr = (struct sockaddr_in *) res->ai_addr;
cli_name.sin_addr.s_addr = inet_addr(inet_ntoa(saddr->sin_addr));
cli_name.sin_port = htons(port);
fflush(stdout);
if (connect(sockfd, (struct sockaddr *) &cli_name, sizeof(cli_name)) < 0) {
perror("Error establishing communications");
close(sockfd);
exit(1);
}
char header[100];
int cx;
char buf[2056];
size_t byte_count = 0;
size_t sent_byte_count = 0;
cx = snprintf(header, 100, "GET / HTTP/1.1\r\nHost: %s:%d\r\n\r\n", hostname, port);
size_t total = strlen(header);
size_t sent = 0;
do {
sent_byte_count = write(sockfd, header + sent, total - sent);
if (sent_byte_count < 0)
printf("ERROR writing message to socket");
if (sent_byte_count == 0)
break;
sent += sent_byte_count;
} while (sent < total);
memset(buf,0,sizeof(buf));
while ((byte_count = read(sockfd, buf, 2054)) > 0) {
buf[byte_count] = '\0';
printf("%s", buf); // <-- give printf() the actual data size
fflush(stdout);
}
printf("Exiting now.\n");
close(sockfd);
exit(0);
}

Client code is unable to read data from server [sockets]

My objective is to implement a simple data transfer from server to client. The problem is that the client is unable to read data from the server most of the time (it only works sometimes) even though the server says that the transfer was successful.
server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8080
void main() {
int sock = socket(AF_INET, SOCK_STREAM, 0), option = 1;
struct sockaddr_in address;
socklen_t addrlen = sizeof(address);
char buffer[1024] = {0};
char source_file[] = "<path>/something.txt";
char fname[] = "something.txt";
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
if(bind(sock, (struct sockaddr*) &address, sizeof(address)) == -1){
perror("Could not bind to address");
return;
}
if(listen(sock, 5) == -1) {
perror("Error while listening to connections");
return;
}
int new_socket = accept(sock, (struct sockaddr*) &address, &addrlen);
if(new_socket == -1){
perror("Error connecting to client");
exit(EXIT_FAILURE);
}
else printf("Connected to client\n");
// first send the filename
if(send(new_socket, fname, sizeof(fname), 0) == -1){
perror("Error while sending file");
return;
}
printf("Sending file %s\n\n", source_file);
FILE* f = fopen(source_file, "r");
// send the contents of the file
while(fgets(buffer, sizeof(buffer), f)){
printf("Sending %s", buffer);
if(send(new_socket, buffer, strlen(buffer), 0) == -1)
perror("Error while sending\n");
else printf("Successfully sent\n\n");
memset(buffer, 0, sizeof(buffer));
}
printf("File transfer complete\n");
fclose(f);
}
client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8080
void main() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
long val;
struct sockaddr_in address;
char buffer[1024] = {0};
address.sin_family = AF_INET;
address.sin_port = htons(PORT);
if(inet_pton(AF_INET, "127.0.0.1", &address.sin_addr) == -1){
perror("Invalid IP value");
exit(EXIT_FAILURE);
}
if(connect(sock,(struct sockaddr*) &address, sizeof(address)) == -1){
perror("Connection Error");
exit(EXIT_FAILURE);
}
else printf("Connected to server\n");
// Get file name from server
if(read(sock, buffer, sizeof(buffer)) == -1){
perror("Could not read filename from server");
exit(EXIT_FAILURE);
}
printf("Receiving file %s\n", buffer);
FILE *f = fopen(buffer, "w");
// printf("%ld", read(sock, buffer, sizeof(buffer)));
while((val = read(sock, buffer, sizeof(buffer)))){
if (val == 0) break;
if (val == -1) {
perror("Error reading the content");
break;
}
printf("Read returned %ld\n", val);
printf("Received %s", buffer);
fputs(buffer, f);
memset(buffer, 0, sizeof(buffer));
}
printf("File transfer complete\n");
fclose(f);
}
Sample text file for data transfer
Hello there!
Nice to meet you.
This is a text file.
Bye Bye :)
Have a good day.
Execution from server side
$ cc ./server.c -o server
$ ./server
Connected to client
Sending file /home/username/Documents/something.txt
Sending Hello there!
Successfully sent
Sending Nice to meet you.
Successfully sent
Sending This is a text file.
Successfully sent
Sending Bye Bye :)
Successfully sent
Sending Have a good day.
Successfully sent
File transfer complete
This was the output I got from the server-side. This is weird because on client-side I didn't receive any message
$ cc ./client.c -o client
$ ./client
Connected to server
Receiving file something.txt
File transfer complete
The while loop in client side did not execute at all (because the data within buffer wasn't printed) and the read function did not return -1 otherwise a proper message would have been displayed.
This exact client code only works sometimes and other times it doesn't. What's the problem here?

C browser displays server socket response on webpage, but webpage keeps loading

I am new to learning C sockets, and I was able to successfully send an html <h1>hello world!</h1> as text/html content from a C socket server to the browser(client). However, even though the h1 tag displays correctly, I'm not sure why the page is stuck with a loading indicator. I tried adding a Content-Length property to indicate the length of my response, which works, but I was told that this shouldn't be necessary.
I think I am reading and writing properly to the socket, so I'm not sure what's hanging. Code:
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void servConn(int port)
{
int sd, new_sd;
struct sockaddr_in name, cli_name;
int sock_opt_val = 1;
int cli_len;
char data[256]; /* Our receive data buffer. */
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("(servConn): socket() error");
exit(-1);
}
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&sock_opt_val, sizeof(sock_opt_val)) < 0)
{
perror("(servConn): Failed to set SO_REUSEADDR on INET socket");
exit(-1);
}
name.sin_family = AF_INET;
name.sin_port = htons(port);
name.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sd, (struct sockaddr *)&name, sizeof(name)) < 0)
{
perror("(servConn): bind() error");
exit(-1);
}
listen(sd, 5);
for (;;)
{
cli_len = sizeof(cli_name);
new_sd = accept(sd, (struct sockaddr *)&cli_name, &cli_len);
printf("Assigning new socket descriptor: %d\n", new_sd);
if (new_sd < 0)
{
perror("(servConn): accept() error");
exit(-1);
}
if (fork() == 0)
{ /* Child process. */
close(sd);
char reply[] = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n";
char *requestType;
char *filename;
int status = 200;
char *strPter;
int index = 0;
char c;
while (1)
{
read(new_sd, &c, 1);
if (index > 254)
{
data[index] = '\0';
break;
}
if (c == '\n')
{
data[index] = '\0';
break;
}
else
{
data[index++] = c;
}
}
printf("read: %d bytes: %s\n", index, data);
requestType = strtok_r(data, " ", &strPter);
if (strcmp(requestType, "GET") != 0)
status = 501;
else
printf("request was GET\n");
filename = strtok_r(NULL, " ", &strPter);
printf("filename: %s\n", filename);
strtok_r(NULL, " ", &strPter);
char *response = "<h1>hello world!</h1>";
strcat(reply, response);
printf("\nresponse is (%d): \n%s\n\n", strlen(reply), reply);
send(new_sd, reply, strlen(reply), 0);
close(new_sd);
printf("closed connection!\n");
exit(0);
}
}
}
int main()
{
servConn(5050); /* Server port. */
return 0;
}
Here is my output, which seems to be in the correct HTTP format:
Here is the browser output, which is stuck in loading even though the content is displayed:
How do I correctly close the socket and stop the page from loading after sending hello world?
Before calling
close(new_sd);
you need
shutdown(new_sd, SHUT_RDWR);
It is this call that sends proper connection termination sequence. close doesn't, it just destroys the socket.

C language, get HTML source

I'm trying to get the HTML of this page http://pastebin.com/raw/7y7MWssc using C. So far I'm trying to connect to pastebin using sockets & port 80, and then use a HTTP request to get the HTML on that pastebin page.
I know what I have so far is probably WAY off, but here it is:
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int main()
{
/*Define socket variables */
char host[1024] = "pastebin.com";
char url[1024] = "/raw/7y7MWssc";
char request[2000];
struct hostent *server;
struct sockaddr_in serverAddr;
int portno = 80;
printf("Trying to get source of pastebin.com/raw/7y7MWssc ...\n");
/* Create socket */
int tcpSocket = socket(AF_INET, SOCK_STREAM, 0);
if(tcpSocket < 0) {
printf("ERROR opening socket\n");
} else {
printf("Socket opened successfully.\n");
}
server = gethostbyname(host);
serverAddr.sin_port = htons(portno);
if(connect(tcpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0) {
printf("Can't connect\n");
} else {
printf("Connected successfully\n");
}
bzero(request, 2000);
sprintf(request, "Get %s HTTP/1.1\r\n Host: %s\r\n \r\n \r\n", url, host);
printf("\n%s", request);
if(send(tcpSocket, request, strlen(request), 0) < 0) {
printf("Error with send()");
} else {
printf("Successfully sent html fetch request");
}
printf("test\n");
}
The code above made sense to a certain point, and now I'm confused. How would I make this get the web source from http://pastebin.com/raw/7y7MWssc ?
Fixed, i needed to set add
serverAddr.sin_family = AF_INET;
and bzero serverAddr, and also my HTTP request was wrong, it had an extra /r/n and spaces, like #immibis said.
Corrected:
sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", url, host);
You are getting the pointer returned by gethostbyname() but you weren't doing anything with it.
You need to populate the sockaddr_in with the address, domain and port.
This works...but now you need to worry about obtaining the response...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int main()
{
/*Define socket variables */
char host[1024] = "pastebin.com";
char url[1024] = "/raw/7y7MWssc";
char request[2000];
struct hostent *server;
struct sockaddr_in serverAddr;
short portno = 80;
printf("Trying to get source of pastebin.com/raw/7y7MWssc ...\n");
/* Create socket */
int tcpSocket = socket(AF_INET, SOCK_STREAM, 0);
if(tcpSocket < 0) {
printf("ERROR opening socket\n");
exit(-1);
} else {
printf("Socket opened successfully.\n");
}
if ((server = gethostbyname(host)) == NULL) {
fprintf(stderr, "gethostbybname(): error");
exit(-1);
}
memcpy(&serverAddr.sin_addr, server -> h_addr_list[0], server -> h_length);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(portno);
if(connect(tcpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0) {
printf("Can't connect\n");
exit(-1);
} else {
printf("Connected successfully\n");
}
bzero(request, 2000);
sprintf(request, "Get %s HTTP/1.1\r\n Host: %s\r\n \r\n \r\n", url, host);
printf("\n%s", request);
if(send(tcpSocket, request, strlen(request), 0) < 0) {
printf("Error with send()");
} else {
printf("Successfully sent html fetch request");
}
printf("test\n");
}

Resources