forked from aznashwan/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
124 lines (102 loc) · 3.02 KB
/
client.c
File metadata and controls
124 lines (102 loc) · 3.02 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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "debug.h"
#include "socklib.h"
#include "termlib.h"
// the mutex used to guard console writes:
pthread_mutex_t console_mutex;
void* conn_thread(void* sockfd) {
int* fd = (int *) sockfd;
char* msg = (char *) malloc (STDTRANS_SIZE);
for (;;) {
if (read_str_from_sock(*fd, msg, STDTRANS_SIZE - 1) <= 0) {
printf("%sError reading message from socket.\n", CLEARLINE);
goto cleanup;
}
pthread_mutex_lock(&console_mutex);
printm(msg);
pthread_mutex_unlock(&console_mutex);
}
cleanup:
free(msg);
free(sockfd);
exit(-1);
return NULL;
}
int main(int argc, char* argv[]) {
int nerror = 0;
if (argc != 5) {
printf("USAGE: %s SERVER_ADDRESS SERVER_PORT USERNAME PASSWORD\n", argv[0]);
return -1;
}
// open the socket to the server:
int* sockfd = (int *) malloc(sizeof(int));
*(sockfd) = connect_to_server(argv[1], atoi(argv[2]));
if (sockfd < 0) {
printf("Error connecting to server.\n");
return -2;
}
// attempt auth:
debug("writing username to server");
if (write_str_to_sock(*sockfd, argv[3]) < 0) {
printf("Error writing username on socket.\n");
close(*sockfd);
return -3;
}
debug("writing password to server");
if (write_str_to_sock(*sockfd, argv[4]) < 0) {
printf("Error writing password on socket.\n");
close(*sockfd);
return -4;
}
// the message read from the console:
char* cmsg = (char *) malloc(STDTRANS_SIZE); *(cmsg) = '\0';
// placeholder message used for constant comparison of update:
char* plcmsg = (char *) malloc (STDTRANS_SIZE); strcpy(plcmsg, cmsg);
// create the message reader thread:
pthread_t read_thread;
if (pthread_create(&read_thread, NULL, &conn_thread, sockfd) != 0) {
debug("Error starting reading thread.");
goto cleanup;
}
// loop and keep sending out messages:
for (;;) {
getl(cmsg);
// skip if return key not hit yet:
if (strcmp(cmsg, plcmsg) == 0) {
continue;
}
// skip if no input provided:
if (strcmp(cmsg, "") == 0) {
printf(RETURN);
printf(UP);
printf(CLEARLINE);
printf(PROMPT);
continue;
}
// clear the input line:
pthread_mutex_lock(&console_mutex);
printf(RETURN);
printf(UP);
printf(CLEARLINE);
printf(PROMPT);
pthread_mutex_unlock(&console_mutex);
// remember last input:
strcpy(plcmsg, cmsg);
// send the message to the server:
if (write_str_to_sock(*sockfd, cmsg) < 0) {
printf("%sError writing message to socket.\n", CLEARLINE);
nerror = -6;
goto cleanup;
}
}
cleanup:
free(cmsg);
free(plcmsg);
close(*sockfd);
free(sockfd);
return nerror;
}