C custom netcat for a simple web server

I am trying to make a copy of netcat for web server purposes using c and bash. This is my code in bash:

#!/bin/bash

rm -f out
mkfifo out
trap "rm -f out" EXIT

cat out | nc -l 1500 > >(
    while read line
    do
       echo $line
       # parse http request from line
       # some other things that doesnt matter

       printf "HTTP/1.1 200 OK\nContent-Type:text/html\n\ntest" > out
    done


)

When I visit localhost: 1500, it prints a "test". I want to replace netcat (nc) with my own.

cat out | customnc -l 1500 > >(
 #
)

This is customnc.c

#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>    

int main() {    
   int create_socket, new_socket;    
   socklen_t addrlen;    
   int bufsize = 1024;    
   char *buffer = malloc(bufsize);    
   struct sockaddr_in address;    

   if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) > 0){    
      printf("The socket was created\n");
   }

   address.sin_family = AF_INET;    
   address.sin_addr.s_addr = INADDR_ANY;    
   address.sin_port = htons(1600);    

   if (bind(create_socket, (struct sockaddr *) &address, sizeof(address)) == 0){    
      printf("Binding Socket\n");
   }


      if (listen(create_socket, 10) < 0) {    
         perror("server: listen");    
         exit(1);    
      }    

      if ((new_socket = accept(create_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);    
      printf("%s", buffer);    
      close(new_socket);    

   close(create_socket);    
   return 0;    
}

What happened to my code? When I click localhost: 1600 (customnc), it just throws an error. Unable to connect.

The problem is sending data from bash. If I send a response to customnc.c using write, the browser will display this message, but I have to send it from bash.

: , http- bash bash c. :  1. bash c-  2. localhost .  3. C bash  4. bash () c  5. C  6. C

, . (2)?

+4
1

, -. stdin . close(new_socket);:

char c;
while ((c = getchar()) != EOF) {
    write(new_socket, &c, 1);
}

, , fflush(stdout), printf, bash script, malloc SO_REUSEADDR , (. ).

#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>

#define BUFSIZE 1024
int main()
{
    int create_socket, new_socket;
    socklen_t addrlen;
    char buffer[BUFSIZE];
    struct sockaddr_in address;
    char c;
    int true = 1;

    if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("server: socket");
        exit(1);
    }

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(1600);

    setsockopt(create_socket,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int));

    if (bind(create_socket, (struct sockaddr *)&address, sizeof(address)) < 0)
    {
        perror("server: bind");
        exit(1);
    }
    if (listen(create_socket, 10) < 0)
    {
        perror("server: listen");
        exit(1);
    }
    if ((new_socket = accept(create_socket, (struct sockaddr *)&address, &addrlen)) < 0)
    {
        perror("server: accept");
        exit(1);
    }
    if (recv(new_socket, buffer, BUFSIZE, 0) < 0)
    {
        perror("server: recv");
        exit(1);
    }
    printf("%s", buffer);
    fflush(stdout);

    while ((c = getchar()) != EOF)
    {
        if (write(new_socket, &c, 1) < 0)
        {
            perror("server: write");
            exit(1);
        }
    }
    close(new_socket);
    close(create_socket);
    return 0;
}

bash script. , , GET.

#!/bin/bash
rm -f out
mkfifo out
trap "rm -f out" EXIT

cat out | ./a.out > >(
    while read line
    do
       # strip carriage return
       line=$(echo $line | tr -d '\r')
       echo $line
       if [[ $line == "" ]]; then
           # reached end of GET request
           break
       fi
    done

    printf "HTTP/1.1 200 OK\nContent-Type:text/html\n\ntest" > out
)

nc, , (-l) 1600.

+2

Source: https://habr.com/ru/post/1659979/


All Articles