-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatServer.c
More file actions
174 lines (132 loc) · 4.6 KB
/
chatServer.c
File metadata and controls
174 lines (132 loc) · 4.6 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <netinet/tcp.h>
#include <ctype.h>
#define MAXCHARACTER 4096
#define BACKLOG 30
void * connectionHandler(void *);
void messageParcer(char client_message[MAXCHARACTER]);
int main(int argc, char *argv[])
{
int svr_sockfd;
int svr_portno;
struct sockaddr_in svr_addr, cli_addr;
socklen_t clilen;
int cli_sockfd;
int *new_sockfd;
int too_many = 0; // too many clients connected
if (argc != 2)
{
fprintf(stderr, "usage: %s <svr_port>\n", argv[0]);
exit(EXIT_FAILURE);
}
svr_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (svr_sockfd < 0)
{
perror("socket");
exit(EXIT_FAILURE);
}
svr_portno = atoi(argv[1]);
bzero((char *) &svr_addr, sizeof(svr_addr));
svr_addr.sin_family = AF_INET;
svr_addr.sin_addr.s_addr = INADDR_ANY;
svr_addr.sin_port = htons(svr_portno);
// set sock for reuse of the same socket.
int on = 1;
setsockopt(svr_sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (bind(svr_sockfd, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) < 0)
{
perror("bind");
exit(EXIT_FAILURE);
}
listen(svr_sockfd, BACKLOG);
puts("Waiting for Incoming Connections...");
clilen = sizeof(cli_addr);
while (cli_sockfd = accept(svr_sockfd, (struct sockaddr *) &cli_addr, &clilen))
{
pthread_t client_thread;
new_sockfd = malloc(sizeof(int));
*new_sockfd = cli_sockfd;
//printf("new connection, socket fd is %d\n", cli_sockfd);
if (pthread_create(&client_thread, NULL, connectionHandler, (void *) new_sockfd) < 0)
{
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
if (cli_sockfd < 0)
{
perror("accept");
exit(EXIT_FAILURE);
}
free(new_sockfd);
return 0;
}
void * connectionHandler(void * sockfd)
{
int cli_sockfd = *(int *) sockfd;
int nread;
char * ip;
char * port;
char * tok[50];
char client_message[MAXCHARACTER];
char welcome_message[] = {"Welcome to the 3600 Chat Room \n"};
int listSize = 0;
char * euid[listSize];
bzero(client_message, MAXCHARACTER);
// welcome message to client
write(cli_sockfd, welcome_message, strlen(welcome_message));
recv(cli_sockfd, client_message, MAXCHARACTER, 0);
int i = 0;
tok[0] = strtok(client_message, " ");
while(tok[i] != NULL) {
++i;
tok[i] = strtok(NULL, " ");
}
ip = tok[i-3]; port = tok[i-2]; euid[listSize] = tok[i-1];
//printing out the information when a new connection is made will only be called if a new thread and connection is made.
printf("\n New connection, socket fd is %d, ip is %s, port is %s\n", cli_sockfd, ip, port);
printf( "User %s has logged in\n", euid[listSize]);
++listSize;
// bzero(client_message, MAXCHARACTER);
while((nread = recv(cli_sockfd, client_message, MAXCHARACTER, 0)) > 0)
{
char * tok[50];
int i = 0;
tok[0] = strtok(client_message, ":\n");
while(tok[i] != NULL) {
++i;
tok[i] = strtok(NULL, ":\n");
}
char * command1 = tok[0];
char * command2;
char * command3;
if(i > 1) {
command2 = tok[1];
command3 = tok[2];
}
if(command1 = "LIST") {
for(int x = 0; x < listSize; ++x){
write(cli_sockfd, euid[x], listSize);
}
if(command1 = "QUIT") {
nread = 0;
break;
}
bzero(client_message, MAXCHARACTER);
}
// client connection can be reset by peer, so just disconnect cleanly
if (nread <= 0)
{
printf("User %s has logged out\n", euid[listSize]);
fflush(stdout);
}
free(sockfd);
return 0;
}
}