Permalink
Browse files

basic server

  • Loading branch information...
1 parent 7a1e8a4 commit a36fd84db24c0f924619107829d0e32ed59d78a6 @txus committed Oct 1, 2011
Showing with 92 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +3 −0 Makefile
  3. +3 −1 src/micetrap.c
  4. +78 −0 src/server.c
  5. +7 −0 src/server.h
View
@@ -1 +1,2 @@
src/micetrap
+src/*.o
View
@@ -2,5 +2,8 @@ CFLAGS=-Wall
all: src/micetrap
+src/micetrap: src/server.o
+
clean:
rm -f src/micetrap
+ rm -f src/server.o
View
@@ -1,7 +1,9 @@
#include <stdio.h>
+#include "server.h"
int main(int argc, char *argv[])
{
- printf("Hello world.\n");
+ printf("Starting server...\n");
+ Server_start(9999);
return 0;
}
View
@@ -0,0 +1,78 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include "server.h"
+
+void die(const char *message)
+{
+ if (errno) {
+ perror(message);
+ } else {
+ printf("ERROR: %s\n", message);
+ }
+
+ exit(1);
+}
+
+void Server_start(int port)
+{
+ // Declare file descriptors
+ int listen_fd, accept_fd;
+
+ // Declare server and client addresses
+ struct sockaddr_in server, client;
+
+ unsigned int sin_size;
+
+ // Try to open the socket
+ listen_fd = socket(AF_INET, SOCK_STREAM, 0);
+ if (listen_fd == -1) die("Could not open socket.");
+
+ server.sin_family = AF_INET;
+ server.sin_port = htons(port);
+
+ // Set our IP address automatically
+ server.sin_addr.s_addr = INADDR_ANY;
+
+ // Fil the rest of the struct with zeros
+ bzero(&(server.sin_zero), 8);
+
+ if (bind(listen_fd, (struct sockaddr* )&server, sizeof(server)) == -1) {
+ die("Could not bind the socket.");
+ }
+
+ printf("Server ready and listening on port %d...", port);
+ listen(listen_fd, 5);
+
+ while(1) {
+ sin_size = sizeof(struct sockaddr_in);
+
+ // Accept incoming connection
+ if ((accept_fd = accept(listen_fd,
+ (struct sockaddr *)&client,
+ &sin_size)) == -1) {
+ die("Could not accept incoming connection.");
+ }
+
+ printf("Incoming connection from %s\n", inet_ntoa(client.sin_addr));
+
+ char buf[255];
+ int n;
+ bzero(buf, 256);
+ n = recv(accept_fd, buf, 255, 0);
+ printf("Message received: %s", buf);
+
+ send(accept_fd, "Welcome to my server.\n", 22, 0);
+
+ // Close file descriptor
+ close(accept_fd);
+ }
+}
View
@@ -0,0 +1,7 @@
+#ifndef __server_h__
+#define __server_h__
+
+void die(const char *message);
+void Server_start(int port);
+
+#endif

0 comments on commit a36fd84

Please sign in to comment.