-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangserver_iter.c
112 lines (93 loc) · 2.83 KB
/
hangserver_iter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Network server for hangman game */
/* File: hangserver.c */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <syslog.h>
#include <signal.h>
#include <errno.h>
extern time_t time ();
int maxlives = 12;
char *word [] = {
# include "words"
};
# define NUM_OF_WORDS (sizeof (word) / sizeof (word [0]))
# define MAXLEN 80 /* Maximum size in the world of Any string */
# define HANGMAN_TCP_PORT 1066
main ()
{
int sock, fd, client_len;
struct sockaddr_in server, client;
srand ((int) time ((long *) 0)); /* randomize the seed */
sock = socket (AF_INET, SOCK_STREAM, 0);//0 or IPPROTO_TCP
if (sock <0) { //This error checking is the code Stevens wraps in his Socket Function etc
perror ("creating stream socket");
exit (1);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(HANGMAN_TCP_PORT);
if (bind(sock, (struct sockaddr *) & server, sizeof(server)) <0) {
perror ("binding socket");
exit (2);
}
listen (sock, 5);
while (1) {
client_len = sizeof (client);
if ((fd = accept (sock, (struct sockaddr *) &client, &client_len)) <0) {
perror ("accepting connection");
exit (3);
}
play_hangman (fd, fd);
close (fd);
}
}
/* ---------------- Play_hangman () ---------------------*/
play_hangman (int in, int out)
{
char * whole_word, part_word [MAXLEN],
guess[MAXLEN], outbuf [MAXLEN];
int lives = maxlives;
int game_state = 'I';//I = Incomplete
int i, good_guess, word_length;
char hostname[MAXLEN];
gethostname (hostname, MAXLEN);
sprintf(outbuf, "Playing hangman on host% s: \n \n", hostname);
write(out, outbuf, strlen (outbuf));
/* Pick a word at random from the list */
whole_word = word[rand() % NUM_OF_WORDS];
word_length = strlen(whole_word);
syslog (LOG_USER | LOG_INFO, "server chose hangman word %s", whole_word);
/* No letters are guessed Initially */
for (i = 0; i <word_length; i++)
part_word[i]='-';
part_word[i] = '\0';
sprintf (outbuf, "%s %d \n", part_word, lives);
write (out, outbuf, strlen(outbuf));
while (game_state == 'I')
/* Get a letter from player guess */
{
while (read (in, guess, MAXLEN) <0) {
if (errno != EINTR)
exit (4);
printf ("re-read the startin \n");
} /* Re-start read () if interrupted by signal */
good_guess = 0;
for (i = 0; i <word_length; i++) {
if (guess [0] == whole_word [i]) {
good_guess = 1;
part_word [i] = whole_word [i];
}
}
if (! good_guess) lives--;
if (strcmp (whole_word, part_word) == 0)
game_state = 'W'; /* W ==> User Won */
else if (lives == 0) {
game_state = 'L'; /* L ==> User Lost */
strcpy (part_word, whole_word); /* User Show the word */
}
sprintf (outbuf, "%s %d \n", part_word, lives);
write (out, outbuf, strlen (outbuf));
}
}