aboutsummaryrefslogtreecommitdiff
path: root/server.c
diff options
context:
space:
mode:
authorFranklin Wei <frankhwei536@gmail.com>2014-07-14 16:37:17 -0400
committerFranklin Wei <frankhwei536@gmail.com>2014-07-14 16:37:17 -0400
commit573db59c564e28da0f21c4e9f43b5aaa86fb337e (patch)
treee42f723e93ebd44cdccb4d242ed9bad69061bef4 /server.c
parent79bd2194539aa559078de9afb1500a2e754ee501 (diff)
downloadwargames-server-573db59c564e28da0f21c4e9f43b5aaa86fb337e.zip
wargames-server-573db59c564e28da0f21c4e9f43b5aaa86fb337e.tar.gz
wargames-server-573db59c564e28da0f21c4e9f43b5aaa86fb337e.tar.bz2
wargames-server-573db59c564e28da0f21c4e9f43b5aaa86fb337e.tar.xz
Working code ;D
Diffstat (limited to 'server.c')
-rw-r--r--server.c38
1 files changed, 33 insertions, 5 deletions
diff --git a/server.c b/server.c
index fe8b6f8..0ec96e4 100644
--- a/server.c
+++ b/server.c
@@ -1,3 +1,23 @@
+/*
+ * WarGames - a WOPR emulator written in C
+ * Copyright (C) 2014 Franklin Wei
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Contact the author at contact@fwei.tk
+ */
+
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
@@ -9,8 +29,8 @@
#include <stdio.h>
#include <signal.h>
#include "joshua.h"
-#define PORT 1029
int server_socket;
+uint16_t port;
int pipes[FD_SETSIZE][2];
int make_server_socket(uint16_t port)
{
@@ -50,20 +70,29 @@ int process_data(int fd)
}
else
{
+ printf("Client sends: %s\n", buf);
write(pipes[fd][1], buf, strlen(buf));
+ return 0;
}
}
void serv_cleanup()
{
printf("preparing to exit...\n");
fflush(stdout);
+ shutdown(server_socket, SHUT_RDWR);
close(server_socket);
}
int main(int argc, char* argv[])
{
+ if(argc!=2)
+ {
+ printf("Usage: %s PORT\n", argv[0]);
+ return 2;
+ }
printf("starting server...\n");
+ port=atoi(argv[1]);
signal(SIGINT, &serv_cleanup);
- int sock=make_server_socket(PORT);
+ int sock=make_server_socket(port);
server_socket=sock;
fd_set active_fd_set, read_fd_set;
struct sockaddr_in client;
@@ -74,7 +103,7 @@ int main(int argc, char* argv[])
}
FD_ZERO(&active_fd_set);
FD_SET(sock, &active_fd_set);
- printf("listening on port %d\n", PORT);
+ printf("listening on port %d\n", port);
while(1)
{
read_fd_set=active_fd_set;
@@ -101,7 +130,7 @@ int main(int argc, char* argv[])
}
printf("new connection.\n");
FD_SET(new, &active_fd_set);
- int ret=pipe(pipes[i]);
+ int ret=pipe(pipes[new]);
if(ret<0)
{
printf("pipe error.\n");
@@ -109,7 +138,6 @@ int main(int argc, char* argv[])
pid_t pid=fork();
if(pid==0) /* child */
{
- printf("child.\n");
be_joshua(new);
close(new);
FD_CLR(new, &active_fd_set);