92 lines
2.4 KiB
C
92 lines
2.4 KiB
C
/*
|
|
Final Project - Part 1
|
|
Nicholas Pease
|
|
COS440
|
|
*/
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <arpa/inet.h>
|
|
#include <unistd.h>
|
|
#include <netdb.h>
|
|
#include <errno.h>
|
|
#include <signal.h>
|
|
#include <string.h>
|
|
|
|
// Constants
|
|
#define PORT 5000
|
|
#define DEBUG 0
|
|
|
|
// Globals
|
|
|
|
// Structs
|
|
struct http_start_line {
|
|
char method[8];
|
|
char path[1024];
|
|
char version[16];
|
|
};
|
|
|
|
struct http_header {
|
|
char field_name[256];
|
|
char field_value[1024];
|
|
};
|
|
|
|
struct http_message {
|
|
struct http_start_line start_line;
|
|
struct http_header headers[100];
|
|
char body[8192];
|
|
};
|
|
|
|
// Functions
|
|
struct http_message create_http_message_from_string(char *message_str) {
|
|
struct http_message message;
|
|
|
|
|
|
}
|
|
|
|
// Main
|
|
int main(int argc, char *argv[]) {
|
|
|
|
// Setup TCP Socket
|
|
if (DEBUG) printf("SETUP: Setup Sockets\n");
|
|
int serv_sock, client_sock, read_size;
|
|
struct sockaddr_in server, client;
|
|
char client_message[20000];
|
|
serv_sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
client_sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (DEBUG) printf("SETUP: Socket Created\n");
|
|
|
|
// Bind Sockets
|
|
server.sin_family = AF_INET;
|
|
server.sin_port = htons(PORT);
|
|
// inet_pton(AF_INET, INADDR_ANY, &(server.sin_addr));
|
|
bind(serv_sock,(struct sockaddr *)&server, sizeof(server));
|
|
if (DEBUG) printf("SETUP: Socket Bound\n");
|
|
|
|
// Start Listening and Waiting for Connections
|
|
listen(serv_sock, 1);
|
|
if (DEBUG) printf("SETUP: Listening for Connections\n\n");
|
|
|
|
while (1) {
|
|
// Accept Connection from Client
|
|
if (DEBUG) printf("WAIT: Waiting for incoming connections...\n");
|
|
int c = sizeof(struct sockaddr_in);
|
|
client_sock = accept(serv_sock, (struct sockaddr *)&client, &c);
|
|
if (DEBUG) printf("CONNECT: Connection Accepted\n");
|
|
|
|
// Receive Message from Client
|
|
memset(client_message, 0, sizeof(client_message));
|
|
read_size = recv(client_sock, client_message, sizeof(client_message), 0);
|
|
if (DEBUG) printf("RECEIVE: Message Received\n");
|
|
|
|
// Process Message From Client
|
|
if (DEBUG) printf("PROCESS: Processing Message\n");
|
|
struct http_message message = create_http_message_from_string(client_message);
|
|
|
|
// Close Client Socket
|
|
close(client_sock);
|
|
if (DEBUG) printf("DISCONNECT: Client Disconnected\n\n");
|
|
}
|
|
|
|
return 0;
|
|
} |