Hi,
I am a beginner when it comes to Socket Programming. I am currently trying to develop a web server that can service basic GET and POST http requests from a browser. I am using my laptop as the server and the client both. So the idea is that I should be able to type http://127.0.0.1:PORT/ in Firefox and my web server should be able to connect to that port and start talking.
So far the code I have written does not seem to work. I'm using ConText and Cygwin to be able to run and execute the program in Windows. I've removed firewalls from the port I'm using as well. However, the webserver does not seem to recognise the connection.
Any ideas why? I'm attaching the code I have written so far. Any help would be very greatly appreciated. At the moment the program doesnt go past "Listening on port..". I have isolated the fault and it seems that its stuck on the statement that I have highlighted.
/* Webserver Code */
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h> // for the use of inet_ntop()
#include <netinet/in.h>
#define PORT "1500" // the port we will connect to
#define BACKLOG 10 // how many pending connections the queue will hold
void *get_in_addr(struct sockaddr *sa);
int main (void) {
int sockfd, new_fd=0;
int status;
struct addrinfo hints;
struct addrinfo *servinfo;
struct sockaddr_storage their_addr;
char s[INET6_ADDRSTRLEN];
socklen_t addr_size;
FILE *errors;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
errors = fopen("Errors.txt", "w");
fprintf(errors,"testing...\n");
if(errors == NULL){
fprintf(stderr, "Cant open output file\n");
}
if ((status = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
fprintf(errors, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
// make a socket
// socket() function returns a socket file descriptor called sockfd
sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, 0);
// bind to the port we passed in to getaddrinfo()
if((bind(sockfd, servinfo->ai_addr, servinfo->ai_addrlen))== -1) {
fprintf(errors, "Binding Failed\n");
exit(1);
}
// listen on the port
if((listen(sockfd, BACKLOG))== -1) {
fprintf(errors, "Listening failed\n");
exit(1);
} else {
printf("Listening on port...\n");
}
//now accept an incoming connection
addr_size = sizeof(their_addr);
**new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size)**;
printf( "new_fd = %d\n", new_fd);
if(new_fd == -1) {
fprintf(errors, "Accepting failed\n");
exit(1);
} else {
printf("The Accepting worked\n");
}
//Prints out the address of the connector
inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof(s));
printf("server: got connection from %s\n", s);
fclose(errors);
return 0;
}
// To obtain the client's information
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}