aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2015-07-31 19:25:24 +0100
committerFreeArtMan <dos21h@gmail.com>2015-07-31 19:25:24 +0100
commit892dad5e3018ca1bd998c71ee32a72d725e92047 (patch)
tree773a2d8a8f0c5117d1070e5992da871d338bb1be
parent932d320f4074563bba6544e6fa707dc9524d6c06 (diff)
downloadcode-snippets-892dad5e3018ca1bd998c71ee32a72d725e92047.tar.gz
code-snippets-892dad5e3018ca1bd998c71ee32a72d725e92047.zip
http_show showes http requests that comes on port 8081
-rw-r--r--http_show/Makefile2
-rw-r--r--http_show/http_show.c85
2 files changed, 87 insertions, 0 deletions
diff --git a/http_show/Makefile b/http_show/Makefile
new file mode 100644
index 0000000..f78dcc6
--- /dev/null
+++ b/http_show/Makefile
@@ -0,0 +1,2 @@
+make:
+ gcc http_show.c -o http_show \ No newline at end of file
diff --git a/http_show/http_show.c b/http_show/http_show.c
new file mode 100644
index 0000000..cc36d7d
--- /dev/null
+++ b/http_show/http_show.c
@@ -0,0 +1,85 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#define MAX_CLIENTS 2
+#define MAX_RESPONSE_SIZE 1024
+#define DEFAULT_PORT 8081
+
+int main()
+{
+
+ struct sockaddr_in server;
+ struct sockaddr_in client;
+
+ int sock;
+ int new_accepted;
+ int sockaddr_sizeof = sizeof( struct sockaddr_in );
+ ssize_t data_len;
+ char response[MAX_RESPONSE_SIZE];
+
+ if( (sock = socket(AF_INET, SOCK_STREAM,0)) == -1 )
+ {
+ perror("couldnot create socket");
+ exit(1);
+ }
+
+ server.sin_family = AF_INET;
+ server.sin_port = htons( DEFAULT_PORT );
+ server.sin_addr.s_addr = INADDR_ANY;
+ bzero(&server.sin_zero, 8);
+
+ if ( bind( sock, (struct sockaddr *)&server, sockaddr_sizeof ) == -1 )
+ {
+ perror("Cannot bind");
+ exit(1);
+ }
+
+ if ( listen( sock, MAX_CLIENTS ) == -1 )
+ {
+ perror("Error while listening");
+ exit(1);
+ }
+
+ while ( 1 != 2 )
+ {
+ if ((new_accepted = accept(sock, (struct sockaddr *)&client, &sockaddr_sizeof )) == -1)
+ {
+ perror("could not accept new connection");
+ exit(1);
+ }
+ printf("New Client connected from port no %d and IP %s\n", ntohs(client.sin_port), inet_ntoa(client.sin_addr));
+
+ data_len = 1;
+ while( data_len )
+ {
+ data_len = recv( new_accepted, response, MAX_RESPONSE_SIZE-1, 0 );
+ response[data_len-1]=0;
+ printf("Http-request size: %d byte\n", data_len);
+ printf("%s", response);
+ if ( data_len )
+ {
+ //char *p = (char *)memchr(&response[4], ' ', strlen( &response[4] ) );
+ //*p = '\0';
+ //send( new_accepted, &response[4], p-&response[4], 0 );
+ send( new_accepted, "\r\n", 2, 0 );
+ //send( new_accepted, response, data_len, 0 );
+ //send( new_accepted, "Hello", 5, 0 );
+ send( new_accepted, response, data_len-1, 0 );
+ data_len = 0;
+ }
+ }
+ printf("Client disconnected\n");
+ close( new_accepted );
+ }
+
+ close( sock );
+
+ return 0;
+} \ No newline at end of file