NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

// #include <stdio.h>
// #include <sys/socket.h>
// #include <arpa/inet.h> //inet_addr

#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

#define eradsf "afd"
#define response_404 "HTTP/1.1 404 Not Foundn"
#define response_200 "HTTP/1.1 200 OKnn"
int bufsize = 1024;

void reuse_socket(int socket_fd)
{
int so_reuseaddr = 1;

setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR,
&so_reuseaddr,
sizeof so_reuseaddr);
}

int create_socket()
{
int socket_;
if ((socket_ = socket(AF_INET, SOCK_STREAM, 0)) > 0)
{
printf("The socket was createdn");
}
return socket_;
}

struct sockaddr_in create_address(int port)
{
struct sockaddr_in address;

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
return address;
}

void bind_and_show_errors(int listen_socket, struct sockaddr_in address)
{
int bind_result = bind(listen_socket, (struct sockaddr *)&address, sizeof(address));
if (bind_result == 0)
{
printf("Bound Socketn");
}
else
{
printf("Bad bind: %dn", bind_result);
}
printf("Bind finishedn");
}

void respond(char *buffer, char *response)
{
printf("responding to: %sn", buffer);

char *token[2];
token[0] = strtok(buffer, " tn");
printf("token: %sn", token[0]);
token[1] = strtok(NULL, " t");
printf("token: %sn", token[1]);

if (strncmp("/exec", token[1], 5) != 0)
{
strcpy(response, response_404);
}
else
{
strcpy(response, response_200);
strcat(response, "We got thisnn");
}
printf("response is:n %s", response);
}

int server(int port)
{
int listen_socket;
listen_socket = create_socket();
reuse_socket(listen_socket);

struct sockaddr_in address = create_address(port);
bind_and_show_errors(listen_socket, address);
if (listen(listen_socket, 10) < 0)
{
perror("server: listen");
exit(1);
};

socklen_t addrlen;
int new_socket;
char *buffer = malloc(bufsize);
char *response = malloc(bufsize * 10);
while (1)
{
if ((new_socket = accept(listen_socket, (struct sockaddr *)&address, &addrlen)) < 0)
{
perror("server: accept");
exit(1);
}

if (new_socket > 0)
{
printf("The Client is connected...n");
}

recv(new_socket, buffer, bufsize, 0);
respond(buffer, response);
write(new_socket, response, strlen(response));
shutdown(new_socket, SHUT_RDWR);
close(new_socket);
// close(listen_socket);
}
return 0;
}

int main(int argc, char *argv[])
{
int port = atoi(argv[1]);
printf("Listenning on port %dn", port);
return server(8888);
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.