-
Notifications
You must be signed in to change notification settings - Fork 0
/
audclient.c
161 lines (141 loc) · 3.43 KB
/
audclient.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "def.h"
#define MAXHOSTNAME 80
#define AUDIENCE 0
#define ACTOR 1
#define BUFSIZE 1024
void exit (int);
char buf[BUFSIZE];
char rbuf[BUFSIZE];
void GetUserInput ();
void sig_leaving();
int rc, cc;
int servsock, clisock;
char *username;
void ignoreQUIT (int sig);
void ignoreCTRD (int sig);
main (argc, argv)
int argc;
char *argv[];
{
int childpid;
struct sockaddr_in remoteServer;
struct sockaddr_in localServer;
struct sockaddr_in client;
struct hostent *hp, *gethostbyname ();
struct servent *sp;
struct sockaddr_in from;
struct sockaddr_in addr;
int fromlen;
int length;
int MODE;
int ServerPort;
int Port;
if (argc == 1) {
printf (" Usage: audclient <host> <port> \n");
exit (0);
}
//ignore quit signal
signal (SIGQUIT, SIG_IGN);
Port = atoi(argv[2]);
Port++;
printf ("CLIENT is an audience\n");
printf ("Server Port is: %d\n", Port);
if ((hp = gethostbyname (argv[1])) == NULL) {
addr.sin_addr.s_addr = inet_addr (argv[1]);
if ((hp = gethostbyaddr ((char *) &addr.sin_addr.s_addr,
sizeof (addr.sin_addr.s_addr), AF_INET)) == NULL) {
fprintf (stderr, "Can't find host %s\n", argv[1]);
exit (-1);
}
}
bcopy (hp->h_addr, &(remoteServer.sin_addr), hp->h_length);
remoteServer.sin_family = AF_INET;
remoteServer.sin_port = htons(Port);
clisock = socket (AF_INET, SOCK_STREAM, 0);
if (connect (clisock, (SA *) & remoteServer, sizeof (remoteServer)) < 0) {
close (clisock);
perror ("connecting stream socket");
exit (0);
}
if ((username = getlogin()) == NULL) perror("Who are you?\n");
if ( send(clisock, username, strlen(username), 0) < 0)
perror("send username message");
childpid = fork ();
if (childpid == 0) {
signal (SIGQUIT, ignoreQUIT);
signal (SIGINT,sig_leaving );
GetUserInput ();
}
for (;;) {
signal (SIGQUIT, SIG_IGN);
signal (SIGINT,SIG_IGN);
cleanup (rbuf);
if ((rc = recv (clisock, rbuf, sizeof (buf), 0)) < 0) {
perror ("receiving stream message");
exit (-1);
}
if (rc > 0) {
rbuf[rc] = '\0';
if (strcmp(rbuf," \b") != 0)
printf ("%s\n", rbuf);
fflush (stdout);
}
else {
printf ("Disconnected..\n");
close (clisock);
exit (0);
}
}
}
cleanup (buf)
char *buf;
{
int i;
for (i = 0; i < BUFSIZE; i++) buf[i] = '\0';
}
void sig_leaving()
{
printf ("\n\nI am leaving ... \n");
close (clisock);
kill (getppid (), 9);
exit (0);
}
void ignoreCTRD (int sig)
{
signal(SIGINT, ignoreCTRD);
printf("\nUse Ctr+C to exit ...\n");
}
void ignoreQUIT (int sig)
{
signal(SIGQUIT, ignoreQUIT);
printf("\nUse Ctr+C to exit ...\n");
}
void GetUserInput ()
{
for (;;) {
//printf ("Received: ");
fflush (stdout);
cleanup (buf);
rc = read (0, buf, sizeof (buf));
if (rc == 0)
break;
if (send (clisock, buf, rc, 0) < 0)
perror ("sending stream message");
}
int pid1 = fork ();
if (pid1==0)
{
signal (SIGQUIT, SIG_IGN);
signal (SIGINT,SIG_IGN );
for (;;)
{
signal(SIGQUIT,ignoreCTRD);
}
exit(0);
}
wait(NULL);
printf ("EOF... exit\n");
close (clisock);
kill (getppid (), 9);
exit (0);
}