I'm trying to make copy of netcat for webserver 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 "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's wrong with my code? When I hit localhost:1600 (customnc) it just throws an error Unable to connect.
The problem is with sending data from bash. If I send response in customnc.c using write
, browser will display the message, but I have to send it from bash.
Edit: The real problem is that I want to parse http request in bash and send file using bash to c. So it goes like this: 1. Bash starts c server 2. User opens localhost in browser 3. C will get request and sent it back to bash 4. Bash will parse request and send response (text) to c 5. C will receive response and send it through socket to browser 6. C will close socket
I'm trying to implement this but I'm not able to make this bidirectional communication. Can I do it with named pipes (2)?