Code:
/*
** server.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/msg.h>
#define PORT "3490" // the port users will be connecting to
#define BACKLOG 10 // how many pending connections queue will hold
#define SHM_SIZE 1024 // make it a 1K shared memory segment
#define QUEKEY 101
void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
// signal(SIGCHLD, SIG_IGN); // So i dont have to use wait();
// get sockaddr, IPv4 or IPv6:
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);
}
struct node {
int client;
int nodeId;
struct node *next;
struct node *prev;
};
struct node *first, *last;
//Struct to be used with message
struct myMsg {
long mtype;
char mtext[200];
};
//Function Prototypes
void myabort(char *);
void append_node(struct node *lnode);
void insert_node(struct node *lnode, struct node *after);
void remove_node(struct node *lnode);
void sendAll(char *);
int main(void)
{
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
struct sigaction sa;
int yes=1;
char s[INET6_ADDRSTRLEN];
int rv,n;
char tmp[100];
int childcount = 0;
struct node *lnode; //This is the doubly linked list that i will use to store the sockets in
first=NULL;
int msqid;
struct myMsg buf;
//Creating message
/* make the key: */
if ((key = ftok("text", 'R')) == -1) { //Creats a "random" key
perror("ftok");
exit(1);
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP
if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("server: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("server: bind");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "server: failed to bind\n");
return 2;
}
freeaddrinfo(servinfo); // all done with this structure
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
if (fork()==0){ //This is the child that will be use to receive message from all the clients
printf ("Mottagar processen snurrar\n");
if ((msqid = msgget(QUEKEY, 0666 | IPC_CREAT)) == -1) { //Creats the message que
perror("msgget");
exit(1);
}
while(1){
if (msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0) == -1) {
perror("msgrcv");
exit(1);
}
printf("Detta är skrivet från Parrent %s\n",buf.mtext);
}
}
printf("server: waiting for connections...\n");
while(1) { // main accept() loop
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
printf("new_fd är %d\n",new_fd);
if (new_fd == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);
printf("server: got connection from %s\n", s);
if (fork()==0) { // this is the child process
if ((msqid = msgget(QUEKEY, 0666 | IPC_CREAT)) == -1) { //Creats the message que
perror("msgget");
exit(1);
}
close(sockfd); // child doesn't need the listener
if (send(new_fd, "Hello, world!", 13, 0) == -1)
perror("send");
buf.mtype=1; //It took a lot of troubleshooting before i realized that this had to be set
while (1){ //read string from client
bzero(&tmp, sizeof(tmp)); // empty tmp
n = read(new_fd,(char *) &tmp, 100); // Reads a line from the client
if (n == -1)
perror("read()");
tmp[n] = '\0';
printf("client processen har tagit emot %s\n",tmp);
strcpy(buf.mtext,tmp);
int len = strlen(buf.mtext);
printf("client processen försöker sända %s med länden %d till kön\n",buf.mtext,len);
if (msgsnd(msqid, &buf, len+1, 0) == -1)
perror("msgsnd");
}
}
//Parrent process
lnode = (struct node *)malloc(sizeof(struct node)); //Make room for a node
lnode->client = new_fd; //populaate the node with some data
lnode->nodeId=childcount;
append_node(lnode); //append node to list
for(lnode = first; lnode != NULL; lnode = lnode->next) {
if (write(lnode->client, "Nu anslöt en klient \n", 20) == -1)
perror("send");
}
childcount++;
for(lnode = first; lnode != NULL; lnode = lnode->next) {
printf("hepp\n");
if (write(lnode->client, &buf, 20) == -1)
perror("send");
}
}
return 0;
}
void append_node(struct node *lnode) {
printf ("Nu lägger vi till en node i listan den har lnode->client %d\n",lnode->client);
if(first == NULL) {
first = lnode;
if (first==NULL)
printf("........ in append\n");
lnode->prev = NULL;
} else {
last->next = lnode;
lnode->prev = last;
}
last = lnode;
lnode->next = NULL;
}
void insert_node(struct node *lnode, struct node *after) {
lnode->next = after->next;
lnode->prev = after;
if(after->next != NULL)
after->next->prev = lnode;
else
last = lnode;
after->next = lnode;
}
void remove_node(struct node *lnode) {
if(lnode->prev == NULL)
first = lnode->next;
else
lnode->prev->next = lnode->next;
if(lnode->next == NULL)
last = lnode->prev;
else
lnode->next->prev = lnode->prev;
}