-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangclient.c
58 lines (46 loc) · 1.56 KB
/
hangclient.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
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
# define LINESIZE 80
# define HANGMAN_TCP_PORT 1066
int main (int argc, char * argv [])
{
struct sockaddr_in server; // Server's address assembled here
struct hostent * host_info;
int sock, count;
char i_line[LINESIZE];
char o_line[LINESIZE];
char * server_name;
/* Get server name from the command line. If none, use 'localhost' */
server_name = (argc = 1)? argv [1]: "localhost";
/* Create the socket */
sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock <0) {
perror ("Creating stream socket");
exit (1);
}
host_info = gethostbyname(server_name);
if (host_info == NULL) {
fprintf (stderr, "%s: unknown host:%s \ n", argv [0], server_name);
exit (2);
}
/* Set up the server's socket address, then connect */
server.sin_family = host_info->h_addrtype;
memcpy ((char *) & server.sin_addr, host_info->h_addr, host_info->h_length);
server.sin_port = htons (HANGMAN_TCP_PORT);
if (connect (sock, (struct sockaddr *) & server, sizeof server) <0) {
perror ("connecting to server");
exit (3);
}
/*OK connected to the server.
Take a line from the server and show it, take a line and send the user input to the server.
Repeat until the server terminates the connection. */
printf ("Connected to server% s \ n", server_name);
while ((count = read (sock, i_line, LINESIZE)) > 0) {
write (1, i_line, count);
count = read (0, o_line, LINESIZE);//0 = STDIN
write (sock, o_line, count);
}
}