Code:
/**************************************************************************\
* Type * Bits*byte* Possible Values *
**************************************************************************
* short * 16 *2 * -32768 to 65536 *
* unsigned short* 16 *2 * 0 to 65536 *
* * * * *
* int * 32 *4 * -2147483648 to 4294967295 *
* long * 32 *4 * -2147483648 to 4294967295 *
* unsigned int * 32 *4 * 0 to 4294967295 *
* long long * 64 *8 * -9223372036854778838 to 18446744073709551616*
**************************************************************************
* char long float volatile *
* short signed double void *
* int unsigned const *
* *
* char, signed char, unsigned char *
* int, signed int, unsigned int *
* short int, signed short int, unsigned short int *
* long int, signed long int, unsigned long int *
* float, double, long double *
\**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include "server.h"
#include "constants.h"
#include "math.h"
struct descr *descr_list = NULL;
struct descr
{
int newfd; /* Send and receive from newfd */
short int state; /* State of player in the game */
struct descr *next; /* For generating a list */
struct input_queue *queue;/* Character input queue */
};
int initialize_descr(int newfd, struct descr *p)
{
struct descr *d;
d = descr_list;
//malloc(sizeof(struct descr))
if( d != 0)
{
while(d->next != 0)
d = d->next;
if(!(d->next = malloc(sizeof(struct descr))))
nonfatal("malloc failure initializing descr");
d = d->next;
}
else
if(!(d = malloc(sizeof(struct descr))))
nonfatal("malloc failure initializing descr");
d->newfd = newfd;
d->state = 1;p = d;
return sizeof(p);
}
struct input_queue
{
char ch_input[7024]; /* Character input in queue */
struct input_queue *next; /* Queued input or null */
};
int readjust_descr(struct descr *d)
{
struct descr *l;
struct descr *r;
for(l = descr_list; l; l = l->next)
{
r = l;
if (l->state == 0)
{
r->next = l->next;
free(l);
}
}
return sizeof(d);
}
char character_login(struct descr *d)
{
int rbytes = 0;
const char er[] = "Character name is to large.\r\n\r\n";
const char ln[] = "\r\nCharacter name or (n)ew: ";
if ((rbytes = recv(d->newfd, d->queue->ch_input, 12, 0)) <= 0)
{
if (rbytes == 0)
printf("Socket %d hung up\n", d->newfd);
else
nonfatal("Receiving a file descriptor");
d->state = 0;
}
if (rbytes > 11)
{
send(d->newfd, er, sizeof er, 0);
send(d->newfd, ln, sizeof ln, 0);
}
else
d->state = 2;
}
int main()
{
struct sockaddr_in serv;
struct sockaddr_in inco;
struct timeval seconds;
socklen_t inclen;
int simpleSocket;
int clientSocket;
int yes = 1;
int fdmax; // maximum file descriptor number
int newfd;
fd_set master; // master file descriptor list
fd_set read_fds; // will be watched to see if characters become available for reading
fd_set write_fds; // will be watched to see if a write will not block
fd_set except_fds; // will be watched for exceptions.
seconds.tv_sec = 0; /* seconds for select to wait for connections */
seconds.tv_usec = 1000; /* microseconds for select to wait for connections
* there are 1,000 microseconds in a millisecond
* there are 1,000 milliseconds in a second
* thus there are 1,000,000 Microseconds in one
* second, and there are 60 seconds in one minute */
printf("Process id #%i\n", getpid());
FD_ZERO(&master); // clear the master and temp sets
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
FD_ZERO(&except_fds);
if ((simpleSocket=socket(AF_INET, SOCK_STREAM,0)) == -1)
{
fatal("Creating simpleSocket, AF_INET SOCK_STREAM failed");
}
fcntl(simpleSocket, F_SETFL, O_NONBLOCK);
setsockopt(simpleSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
/*struct sockaddr_in
{
short sin_family; /* must be AF_INET
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8]; /* Not used, must be zero
};
struct in_addr { int s_addr; }; */
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = INADDR_ANY;
serv.sin_port = htons(2000);
if (bind(simpleSocket,(struct sockaddr *)&serv,sizeof(serv)) == -1)
{
close(simpleSocket);
fatal("Binding simpleSocket, AF_INET SOCK_STREAM failed");
}
if (listen(simpleSocket, 1000) == -1)
{
close(simpleSocket);
fatal("Listening to simpleSocket, AF_INET SOCK_STREAM failed");
}
// add the listener to the master set
FD_SET(simpleSocket, &master);
// keep track of the biggest file descriptor
fdmax = simpleSocket; // so far, it's this one
for(;;)
{
struct descr *d;
int i;
read_fds = master;
write_fds = master;
except_fds = master;
if (select(fdmax+1, &read_fds, &write_fds, &except_fds, &seconds) == -1)
nonfatal("Select failed");
if (FD_ISSET(simpleSocket, &read_fds))
{
/* handle new connections */
newfd = accept(simpleSocket, (struct sockaddr *)&inco, &inclen);
if(newfd == -1)
nonfatal("Accepting a file descriptor");
else
{
FD_SET(newfd, &master); /* add to master set */
if (newfd > fdmax) /* keep track of the max */
fdmax = newfd;
int i = GetRand(0, 4);
send(newfd, castle_well[i], sizeof (castle_well[i]), 0);
const char name_prompt[] = "Character name or (n)ew: ";
send(newfd, name_prompt, sizeof name_prompt, 0);
initialize_descr(newfd, d);
printf("New connection: on socket %d,\r\n", newfd);
printf(" sin_addr: %s\r\n", inet_ntoa(inco.sin_addr));
/*
printf(" sin_family: %s\n", inco.sin_family);
printf(" sin_port: %s\n", inco.sin_port);
printf(" s_addr: %s\n", inco.sin_addr.s_addr);
printf(" sin_zero: %s\n", inco.sin_zero[8]);*/
}
} /* END FD_ISSET(simpleSocket, &read_fds) */
for (d = descr_list; d; d = d->next)
{
if(FD_ISSET(d->newfd, &read_fds) && d->state == 1)
character_login(d);
}
for(i = 0; i<1000; i++)
if(!(i%1000))
{
int j;
const char tick[] = "tick.";
for(j = 0; j <= fdmax; j++)
{
// send to everyone!
if (FD_ISSET(j, &master))
{
send(j, tick, sizeof tick, 0);
}
}
}
}/* END for(;;).*/
close(simpleSocket);
exit(EXIT_SUCCESS);
}