Warning: Please treat me like the rookie I am. This is my first "real" C program. So if I don't understand some things, that's why.
I'm trying to make a chat server following the example from Beej's Guide to Network Programming. It came recommended, so there you go.
I want to have a function accept a pointer to a structure, modify properties in that pointer, and set up a listener on the passed port. In the below code, I am getting a Segmentation Fault, and I honestly can't figure out why. Blame me being green. (Developing in Netbeans 6.8 on Ubuntu, if needed):
#define PORT "4400"
typedef struct {
int port;
fd_set *connections;
int connections_count;
int listener;
struct addrinfo address;
struct addrinfo socket_hints;
} Server;
typedef struct {
struct sockaddr_storage address; // User's address
int fs_id; // ID to the socket they belong to
char *name; // Pointer to the user's name
struct User *nextUser; // Next user in the list
} User;
void initialize_server(Server *passed_server, char *port) {
struct addrinfo *temp;
int result;
// Set up the server hints
memset(&passed_server->socket_hints, 0, sizeof(struct addrinfo));
passed_server->socket_hints.ai_family = AF_UNSPEC;
passed_server->socket_hints.ai_socktype = SOCK_STREAM;
passed_server->socket_hints.ai_flags = AI_PASSIVE;
result = getaddrinfo(NULL, port, &passed_server->socket_hints, &temp);
printf("Result: %d\n", result);
}
int main(int argc, char** argv) {
// Set up socket stuff
Server *server; // Set up the server
memset(server, 0, sizeof(Server));
fd_set read_sockets; // Master socket holder and sockets to read
int new_connection; // Holds the socket ID of the new connection
socklen_t address_length; // Used to hold the length of the address from the user
struct addrinfo;
// Useful sets
char buffer[1024];
int bytes_recieved;
int yes = 1; // For SETOPT
// Set up server info on defined port
initialize_server(server, PORT);
FD_ZERO(&read_sockets);
return (EXIT_SUCCESS);
}
If you need the full code (I think I posted everything essential), you can find a link below. Thanks in advance for any help or attempt at it!