Skip to content

Commit

Permalink
Handling socket binding errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanpc committed Nov 27, 2012
1 parent cc611c0 commit 2eea979
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions TODO
@@ -1,5 +1,6 @@
The TODO list for the awesome bonsai server:

- Colorize the console output
- Implement the Lua scripting for the config files (yes, all config files are going to be written in Lua)
- Write the standards for the config files.
- Implement the Virtual Host stuff in Lua.
Expand Down
13 changes: 10 additions & 3 deletions bonsai.c
Expand Up @@ -28,6 +28,7 @@ int main(int argc, char *argv[]) {
int socket_descriptor = 0;
int connection = 0;
struct sockaddr_in address;
int binded;

socket_descriptor = socket(AF_INET, SOCK_STREAM, 0);

Expand All @@ -36,10 +37,16 @@ int main(int argc, char *argv[]) {
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(PORT);

bind(socket_descriptor, (struct sockaddr*)&address, sizeof(address));
binded = bind(socket_descriptor, (struct sockaddr*)&address, sizeof(address));
listen(socket_descriptor, MAX_CONNECTIONS);

printf("Server listening on port %d\n", PORT);

// TODO: Handle the ECONNREFUSED error <http://www.gnu.org/software/libc/manual/html_node/Accepting-Connections.html>
if (binded == 0) {
printf("Server listening on port %d\n", PORT);
} else {
perror("ERROR");
return 1;
}

while (true) {
connection = accept(socket_descriptor, (struct sockaddr*)NULL, NULL);
Expand Down
8 changes: 4 additions & 4 deletions process.c
Expand Up @@ -180,13 +180,13 @@ void print_request_headers(char headers[MAX_HEADERS][HEADER_SIZE]) {
* @param request Request file description.
*/
void process_request(int connection, FILE *request) {
char request_type[8];
char file_requested[501];
char request_headers[MAX_HEADERS][HEADER_SIZE] = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };

memset(output, 0, sizeof(output));

char request_headers[MAX_HEADERS][HEADER_SIZE] = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
parse_request_headers(request_headers, request);

char request_type[8];
char file_requested[501];
request_type_and_file(request_type, file_requested, request_headers[0]);
printf("%s %s\n", request_type, file_requested);
print_request_headers(request_headers);
Expand Down

0 comments on commit 2eea979

Please sign in to comment.