-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.c
More file actions
319 lines (292 loc) · 7.45 KB
/
Copy pathclient.c
File metadata and controls
319 lines (292 loc) · 7.45 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*---------------------------------------------------------------------------------------
-- SOURCE FILE: client.c - A simple TCP client program for the chat application.
--
-- PROGRAM: client.exe
--
-- FUNCTIONS: Berkeley Socket API
--
-- DATE: April 5, 2018
--
-- FUNCTIONS:
-- int main (int argc, char **argv)
-- void* readThreadFunc()
-- void* sendThreadFunc()
-- void printFunc()
-- void signal_catcher(int signo)
--
--
-- DESIGNERS: Vafa Dehghan Saei
-- Luke Lee
--
-- PROGRAMMERS: Vafa Dehghan Saei
-- Luke Lee
--
--
-- NOTES:
-- The program will establish a TCP connection to a user specifed server.
-- The server can be specified using a fully qualified domain name or and
-- IP address. After the connection has been established the user can type
-- messages to be sent to other connected clients. The client will also
-- display any incoming message sent from other clients through the server.
---------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#define SERVER_TCP_PORT 7000 // Default port
#define BUFLEN 512 // Buffer length
void* readThreadFunc();
void* sendThreadFunc();
void signal_catcher(int signo);
void printFunc();
int n, bytes_to_read, sd;
char sbuf[BUFLEN], rbuf[BUFLEN], *bp;
char* printBuffer[2048];
int fileIndex = 0;
/*----------------------------------------------------------------------
-- FUNCTION: main
--
-- DATE: April 5, 2018
--
-- DESIGNER: Vafa Dehghan Saei
-- Luke Lee
--
-- PROGRAMMER: Vafa Dehghan Saei
-- Luke Lee
--
-- INTERFACE: int main(int argc, char **argv)
--
-- ARGUMENT: int argc - an int that counts the number of
-- arguments from cmd line
-- char **argv - an array of char* that holds the
-- values of each argument
--
-- RETURNS: int - returns 0 if no error occurs
--
-- NOTES:
-- This is the entry point of the Linux chat console application for
-- client. All the variables are initialized here, and connection to
-- server is set up.
----------------------------------------------------------------------*/
int main(int argc, char **argv)
{
signal(SIGINT, signal_catcher);
int port;
struct hostent *hp;
struct sockaddr_in server;
char *host, **pptr;
char str[16];
pthread_t readThread, sendThread;
switch(argc)
{
case 2:
host = argv[1]; // Host name
port = SERVER_TCP_PORT;
break;
case 3:
host = argv[1];
port = atoi(argv[2]); // User specified port
break;
default:
fprintf(stderr, "Usage: %s host [port]\n", argv[0]);
exit(1);
}
// Create the socket
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Cannot create socket");
exit(1);
}
bzero((char *)&server, sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(port);
if ((hp = gethostbyname(host)) == NULL)
{
fprintf(stderr, "Unknown server address\n");
exit(1);
}
bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length);
// Connecting to the server
if (connect (sd, (struct sockaddr *)&server, sizeof(server)) == -1)
{
fprintf(stderr, "Can't connect to server\n");
perror("connect");
exit(1);
}
// printing starting message
pptr = hp->h_addr_list;
fprintf(stdout, "/====================================\n");
fprintf(stdout, "| Welcome to the chat room!\n");
fprintf(stdout, "| Connected to Server: %s\n", hp->h_name);
fprintf(stdout, "| Server Address: %s\n", inet_ntop(hp->h_addrtype, *pptr, str, sizeof(str)));
fprintf(stdout, "\\====================================\n");
pthread_create (&readThread, NULL, readThreadFunc, NULL);
pthread_create (&sendThread, NULL, sendThreadFunc, NULL);
pthread_join(readThread, NULL);
pthread_join(sendThread, NULL);
close (sd);
return (0);
}
/*----------------------------------------------------------------------
-- FUNCTION: readThreadFunc
--
-- DATE: April 5, 2018
--
-- DESIGNER: Vafa Dehghan Saei
-- Luke Lee
--
-- PROGRAMMER: Vafa Dehghan Saei
-- Luke Lee
--
-- INTERFACE: void* readThreadFunc()
--
-- ARGUMENT: void
--
-- RETURNS: void* - returns a function pointer to the
-- pthread_create() argument
--
-- NOTES:
-- This function gets called after client connects to the server
-- successfully, a new thread is created to receive any incoming message
-- from server.
----------------------------------------------------------------------*/
void* readThreadFunc()
{
int k = 0;
char* temp[1024];
bp = rbuf;
bytes_to_read = BUFLEN;
while (1)
{
n = 0;
while ((n = recv (sd, bp, bytes_to_read, 0)) < BUFLEN)
{
bp += n;
bytes_to_read -= n;
}
temp[k]= (char *) malloc(strlen(rbuf) + 1);
memcpy(temp[k], rbuf, strlen(rbuf));
printBuffer[fileIndex++] = temp[k++];
printf ("\n%s", rbuf);
printf ("(Me): "); // printing for send thread
fflush(stdout);
}
}
/*----------------------------------------------------------------------
-- FUNCTION: sendThreadFunc
--
-- DATE: April 5, 2018
--
-- DESIGNER: Vafa Dehghan Saei
-- Luke Lee
--
-- PROGRAMMER: Vafa Dehghan Saei
-- Luke Lee
--
-- INTERFACE: void* sendThreadFunc()
--
-- ARGUMENT: void
--
-- RETURNS: void* - returns a function pointer to the
-- pthread_create() argument
--
-- NOTES:
-- This function gets called after client connects to the server
-- successfully, a new thread is created to listen for user input from
-- stdin and put it in a send buffer.
----------------------------------------------------------------------*/
void* sendThreadFunc()
{
int k = 0;
char* temp[1024];
while (1)
{
fprintf(stdout, "(Me): ");
fgets (sbuf, BUFLEN, stdin);
if(!strcmp(sbuf, "-p\n"))
{
printf("[Chat saved to output.txt]\n");
fflush(stdin);
printFunc();
continue;
}
//
// char meString[6] = "Me: ";
// strcat(meString, sbuf);
temp[k]= (char *) malloc(strlen(sbuf) + 1);
memcpy(temp[k], sbuf, strlen(sbuf));
printBuffer[fileIndex++] = temp[k++];
// Transmit data through the socket
send (sd, sbuf, BUFLEN, 0);
}
return NULL;
}
/*----------------------------------------------------------------------
-- FUNCTION: printFunc
--
-- DATE: April 5, 2018
--
-- DESIGNER: Vafa Dehghan Saei
--
-- PROGRAMMER: Vafa Dehghan Saei
--
-- INTERFACE: void printFunc()
--
-- ARGUMENT: void
--
-- RETURNS: void
--
-- NOTES:
-- This function is called when user turns on the save to file mode.
-- It saves the received messages to a text file.
----------------------------------------------------------------------*/
void printFunc()
{
FILE* fp;
int tempIndex = 0;
remove("output.txt");
fp = fopen("output.txt", "w");
while (printBuffer[tempIndex] != NULL)
{
fputs(printBuffer[tempIndex++], fp);
}
fclose(fp);
}
/*----------------------------------------------------------------------
-- FUNCTION: signal_catcher
--
-- DATE: April 5, 2018
--
-- DESIGNER: Vafa Dehghan Saei
--
-- PROGRAMMER: Vafa Dehghan Saei
--
-- INTERFACE: void signal_catcher(int signo)
--
-- ARGUMENT: int signo - an int representing the signal number
--
-- RETURNS: void
--
-- NOTES:
-- This function sets the Ctrl-C signal to send an EOF character to the
-- connected socket.
----------------------------------------------------------------------*/
void signal_catcher(int signo)
{
if(signo == SIGINT)
{
char eof[2];
eof[0] = EOF;
send(sd, eof, BUFLEN, 0);
kill(getpid(), SIGTERM);
}
}