Code:
#include <stdio.h> /* sleep(350)*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include "relik server.h"
//Syntax:
//int socket(int af, int type, int protocol);
//C source:
int main(int argc, char **argv)
{
int pos = 1, port = 2000;
while ((pos < argc) && (*(argv[pos]) == '-')) {
switch (*(argv[pos] + 1))
{
case 'f': break;
case 'o': break;
case 'd': break;
case 'm': break;
case 'c': break;
case 'q': break;
case 'r': break;
case 's': break;
case 'h': break;
default: break;
}
pos++;
}
if (pos < argc) {
if (!isdigit(*argv[pos])) {
printf("Usage: %s [-c] [-m] [-q] [-r] [-s] [-d pathname] [port #]\n", argv[0]);
exit(EXIT_FAILURE);
}
else if ((port = atoi(argv[pos])) <= 1024) {
printf("SYSERR: Illegal port number %d.\n", port);
exit(EXIT_FAILURE);
}
}
struct sockaddr_in address;
int mastersocket;
fd_set descrip;
int activity, user;
int comment;
int client_socket[700];
int maxclients = 700;
char buffer[1000];
if (mastersocket=socket(AF_INET,SOCK_STREAM,0)==-1)
fatal("Creating mastersocket, AF_INET SOCK_STREAM failed");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port); /* port is the port to use for connections */
/* Binding to mastersocket */
if(bind(mastersocket,(struct sockaddr *)&address,sizeof(address))==-1) /* bind the socket to the port specified above */
fatal("Binding mastersocket, AF_INET SOCK_STREAM failed");
/* LIstening to mastersocket */
if(listen(mastersocket, 3) == -1)
fatal("Listening to mastersocket, AF_INET SOCK_STREAM failed");
fcntl(mastersocket, F_SETFL, FNDELAY);
While(1=1) //
{ //
/****************************************************************************************************************/
FD_ZERO(&descrip); /* create a list of sockets to check for activity */
FD_SET(mastersocket, &descrip); /* specify mastersocket - ie listen for new connections */
for (user=0; user<max_clients; user++) { !!!!!
if (client_socket[user] > 0) !!!!!
FD_SET(client_socket[user], &descrip); !!!!!
}
if ((activity=select(max_conns, &descrip, NULL, NULL, NULL)<0) && (errno!=EINTR))/* wait for a connection*/
fatal("Selecting a file descriptor");
if (FD_ISSET(mastersocket, &descrip)) {
/* Open the new socket */
/* Open the new socket as 'new_socket' */
int time, new_socket, addrlen=sizeof(address);
if ((new_socket = accept(mastersocket, (struct sockaddr *)&address, &addrlen))<0)
fatal("Accept failed to return a socket descriptor");
/* inform user of socket number - used in send and receive commands */
printf("New socket is fd %d\n", new_socket);
/* transmit message to new connection */
for (int time = 0; time = 5; time++){
char *message =".";
if (send(new_socket, message, strlen(message), 0) != strlen(message)) {
/* if send failed to send all the message, display error */
nonfatal("sending message to a new socket.");
}
sleep(1);
}
puts("Welcome message sent successfully");
/* add new socket to list of sockets */
for (user=0; user<max_clients; user++) {
if (client_socket[user] == 0) {
client_socket[user] = new_socket;
printf("Adding to list of sockets as %d\n", user);
user = max_clients;
}
}
for (user=0; user<max_clients; user++) {
if (FD_ISSET(client_socket[user], &readfds)) {
if ((read(client_socket[user], buffer, 1024)) < 0) {
close(client_socket[user]);
client_socket[user] = 0;
}
else
{
/* set the terminating NULL byte on the end of the data read */
buffer[1000] = 0;
for (comment=0; comment<max_clients; comment++) {
/* note, flags for send() are normally 0, see man page for details */
send(client_socket[loop2], buffer, strlen(buffer), 0);
}
}
}
}
}
}
if (close(mastersocket))exit(EXIT_SUCCESS);
}
int menu_select(fdset simpleChildSocket)
{
char buffer[1024];
int recv_length=0;
//system("clear");
//snprintf(ch str, sizeof(str), "", ...) copies the first sizeof() bytes from "" to
// ch str, also accepting the same variables as printf for formating the string.
send(simpleChildSocket, "-------------- A 20th century 2 demensional multi ^ \r\n ", 70, 0);
send(simpleChildSocket, "\\________ \\ user dungeon game not just for the / \\ \r\n", 70, 0);
send(simpleChildSocket, " | / average role playing adventurer. A | | | \r\n ", 70, 0);
send(simpleChildSocket, " | \\ sensation of its own. Created by, \\ / / \r\n ", 70, 0);
send(simpleChildSocket, " | | \\ Eric Justin Allan / / \\ \r\n ", 70, 0);
send(simpleChildSocket, " |______|_____ / | | | \r\n ", 70, 0);
send(simpleChildSocket, " \\/ \\ / \r\n ", 70, 0);
send(simpleChildSocket, " ^ v \r\n ", 70, 0);
send(simpleChildSocket, "/ \\ \r\n", 7, 0);
send(simpleChildSocket, "| | | \r\n", 7, 0);
send(simpleChildSocket, "\\/ / \r\n", 7, 0);
send(simpleChildSocket, "/ / \\ \r\n", 7, 0);
send(simpleChildSocket, "| | | \r\n", 7, 0);
send(simpleChildSocket, "\\ / \r\n", 7, 0);
send(simpleChildSocket, " v \r\n", 7, 0);
send(simpleChildSocket, " Read Our Policy \"Rules\"\r\n", 65, 0);
send(simpleChildSocket, " 1. New game\r\n", 53, 0);
send(simpleChildSocket, " 2. Continue\r\n", 53, 0);
send(simpleChildSocket, " #", 42, 0);
recv(simpleChildSocket, &buffer, 1024, 0);
}