-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatClient5.c
More file actions
242 lines (212 loc) · 7.14 KB
/
Copy pathchatClient5.c
File metadata and controls
242 lines (212 loc) · 7.14 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
#include <stdio.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "inet.h"
#include "common.h"
void sighandler(int);
struct getSock {
int socket;
};
struct getSock thesocket;
struct servers { // servers to choose from
char name[50];
char addr[20];
int socket;
LIST_ENTRY(servers) connections;
};
LIST_HEAD(servhead, servers); // server list head
void get_name(int sockfd, int *name_made) { // Sets username and sends to server
char name[32];
printf("Enter your nickname (max 24 characters, no spaces): \n");
if (scanf(" %24s", name) != 1) {
printf("Invalid input, try again: \n");
while (scanf(" %24s", name) != 1) { // If invalid, try again until they get it right (not hard)
printf("Invalid input, try again: \n");
}
}
char send[MAX];
snprintf(send, MAX, "N%s", name); // Compose name into server recognizeable message
write(sockfd, send, MAX);
*name_made = 1;
}
int main()
{
signal(SIGINT, sighandler); // Handles disconnect
struct servhead list_head; // To hold incoming server list data
LIST_INIT(&list_head);
char s[MAX] = {'\0'};
int sockfd;
struct sockaddr_in chat_serv_addr, dir_serv_addr;
fd_set readset;
/* Set up the address of the directory server. */
memset((char *) &dir_serv_addr, 0, sizeof(dir_serv_addr));
dir_serv_addr.sin_family = AF_INET;
dir_serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR); /* hard-coded in inet.h */
dir_serv_addr.sin_port = htons(SERV_TCP_PORT); /* hard-coded in inet.h */
/* Create a socket (an endpoint for communication). */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("client: can't open stream socket");
return EXIT_FAILURE;
} else {
thesocket.socket = sockfd;
}
/* Connect to the server. */
if (connect(sockfd, (struct sockaddr *) &dir_serv_addr, sizeof(dir_serv_addr)) < 0) {
perror("client: can't connect to directory server");
return EXIT_FAILURE;
}
// Prepare for query
char query[MAX] = {'\0'};
snprintf(query, MAX, "CQ");
write(sockfd, query, MAX);
// Start query
char query_num[MAX];
int query_size = 0;
ssize_t query_size_read = read(sockfd, query_num, MAX); // how many messages to read/expect
fprintf(stderr, "%s", query_num);
if (query_size_read > 0) {
if (sscanf(query_num, "%d", &query_size) != 1) {
fprintf(stderr, "Error: Failed to parse query size\n");
close(sockfd);
exit(EXIT_FAILURE);
}
fprintf(stderr, "Query Size: %d", query_size);
}
else {
fprintf(stderr, "Error: No proper connection to server. Try again later.");
close(sockfd);
exit(0);
}
printf("\nThe list of active servers is as follows: \n");
for (int i = 0; i < query_size; i++) { // read query from directory
char query[MAX] = {'\0'};
ssize_t query_read = read(sockfd, query, MAX);
if (query_read <= 0) { // if no query return
perror("Error: Invalid reading.");
close(sockfd);
exit(0);
}
struct servers *new_server = malloc(sizeof(struct servers)); // make server directory entry
if (sscanf(query, "%49[^:]:%19[^:]:%d", new_server->name, new_server->addr, &new_server->socket) == 3) {
printf(" %s\n", new_server->name); // prints name
}
LIST_INSERT_HEAD(&list_head, new_server, connections);
}
close(sockfd);
// Selecting server
printf("\nEnter the name of the server to join: \n");
char serv[50];
if (scanf(" %49[^\n]", serv) != 1) {
fprintf(stderr, "Error: Improper input");
exit(EXIT_FAILURE);
}
// checks to see if input is valid
struct servers *iter;
struct servers *to_join;
LIST_FOREACH(iter, &list_head, connections) {
if (strncmp(iter->name, serv, 50) == 0) {
to_join = iter;
break;
}
}
if (to_join == NULL) {
fprintf(stderr, "\nError: No server found with name %s\n", serv);
}
/* Set up the address of the directory server. */
memset((char *) &chat_serv_addr, 0, sizeof(chat_serv_addr));
chat_serv_addr.sin_family = AF_INET;
chat_serv_addr.sin_addr.s_addr = inet_addr(to_join->addr);
chat_serv_addr.sin_port = htons(to_join->socket);
/* Create a socket (an endpoint for communication). */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("client: can't open stream socket");
return EXIT_FAILURE;
}
/* Connect to the server. */
if (connect(sockfd, (struct sockaddr *) &chat_serv_addr, sizeof(chat_serv_addr)) < 0) {
perror("client: can't connect to server");
return EXIT_FAILURE;
}
int name_made = 0; // Lets client know internally ask user for username and to send name request to server
for(;;) {
FD_ZERO(&readset);
FD_SET(STDIN_FILENO, &readset);
FD_SET(sockfd, &readset);
/* Checks if name needs to be made, and sets name if so*/
if (name_made == 0) { // If no name set, set one
get_name(sockfd, &name_made);
}
if (select(sockfd+1, &readset, NULL, NULL, NULL) > 0)
{
/* Check whether there's user input to read */
if (FD_ISSET(STDIN_FILENO, &readset)) {
char message[64];
if (1 == scanf(" %63[^\t\n]", message)) { // Only scans in 63 characters [leaves 1 character buffer for sanity]
/* Send the user's message to the server */
//fprintf(stderr, "%s:%d Input message: %s\n", __FILE__, __LINE__, message); //DEBUG
char send[MAX]; // sets send to MAX to account for extra input
snprintf(send, MAX, "M%s", message); // transposes message into a special send. First initial denotes type of message
write(sockfd, send, MAX);
} else {
perror("Error reading or parsing user input");
//fprintf(stderr, "%s:%d Error reading or parsing user input\n", __FILE__, __LINE__); //DEBUG
}
}
/* Check whether there's a message from the server to read */
if (FD_ISSET(sockfd, &readset)) {
ssize_t nread = read(sockfd, s, MAX);
if (nread <= 0) {
printf("Error: could not read from server. Please try again later");
printf("\nGoodbye!\n");
char sINT[MAX] = {'\0'};
snprintf(sINT, MAX, "D");
write(sockfd, sINT, MAX);
close(sockfd);
exit(0);
} else {
if (s[0] == 'E') { // If error message
if (s[1] == 'I') { // Invalid input, spits out message from server then kills client for safety :)
snprintf(s, MAX, "%s", s+2);
printf("%s\n", s);
exit(0);
}
else if (s[1] == 'N') { // Invalid name, asks for a new one. Spits out why from server
name_made = 0;
snprintf(s, MAX, "%s", s+2);
printf("%s\n", s);
get_name(sockfd, &name_made);
}
}
else {
snprintf(s, MAX, "%s", s+1);
printf("%s\n", s);
}
//fprintf(stderr, "%s:%d Read %zd bytes from server: %s\n", __FILE__, __LINE__, nread, s); //DEBUG
}
}
}
}
close(sockfd);
// return or exit(0) is implied; no need to do anything because main() ends
}
// Reused and altered my code from Assignment 2's client3.c
/* Handles Ctrl+C interrupt to disconnect client */
void sighandler(int signo)
{
int sockfd = thesocket.socket;
if (signo == SIGINT) {
printf("\nDisconnecting...\n");
sleep(2); // Visual feedback, makes user feel like they're disconnecting. Placebo effect
printf("Goodbye!\n");
char sINT[MAX] = {'\0'};
snprintf(sINT, MAX, "D");
write(sockfd, sINT, MAX);
exit(0); //shut it down
}
}