-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelayClient.c
158 lines (138 loc) · 3.87 KB
/
relayClient.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <unistd.h>
struct clientInfo {
int id;
char name[1024];
struct sockaddr_in cliAdd;
};
void * connection (void * arg);
void * request (void* arg);
void * acceptCon (void* arg);
char name[1024] = {'\0'};
int main(int argc, char* argv[]) {
int servSock, cliSock;
struct sockaddr_in servAdd;
int servPort, i;
char *servIP;
char message[1024], buffer[1024] = {'\0'};
if (argc != 3) {
printf("Usage: %s <SERVER_IP> <SERVER_PORT>\n", argv[0]);
exit(0);
}
servIP = argv[1];
servPort = atoi(argv[2]);
if ((cliSock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("Error");
exit(EXIT_FAILURE);
}
servAdd.sin_family = AF_INET;
servAdd.sin_addr.s_addr = inet_addr(servIP);
servAdd.sin_port = htons(servPort);
if ((connect(cliSock, (struct sockaddr*) &servAdd, sizeof(servAdd))) < 0) {
perror("Error");
exit(EXIT_FAILURE);
}
else {
printf("Connected.\n");
}
printf("Please complete your registration:\nType a name: ");
scanf("%s", name);
int msgLen = send(cliSock, (void*)name, 1024, 0);
printf("Type something: ");
scanf("%s", message);
msgLen = send(cliSock, (void*)message, strlen(message), 0);
struct clientInfo session[1024];
msgLen = recv(cliSock, &session, sizeof(session), 0);
if (strcmp(message, "list") == 0) {
for (i = 0; session[i].id != 0; i++) {
printf("%s\n", session[i].name);
}
}
int j = 0;
for(j = 0; strcmp(name, session[j].name)!=0; j++) { }
pthread_t myThread;
pthread_create(&myThread, NULL, &connection, (void*)&session[j]);
perror("myThread");
pthread_t requestThread;
char username[1024];int found = 0;
printf("Type a username to connect : ");
scanf("%s", username);
for(i = 0; strcmp(username, session[i].name)!=0; i++) {
if (i >= 5) {
printf("User not found.\n");
break;
}
}
if (strcmp(username, session[i].name)==0) {
found = 1;
pthread_create(&requestThread, NULL, &request, (void*)&session[i]);
perror("requestThread\n");
}
}
void * request (void * arg) {
struct clientInfo user = *((struct clientInfo*) arg);
struct sockaddr_in userAdd = user.cliAdd;
int reqSock;
if((reqSock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("Error");
exit(EXIT_FAILURE);
}
if (connect(reqSock,(struct sockaddr*)&userAdd,sizeof(userAdd)) < 0) {
perror("Error");
exit(EXIT_FAILURE);
}
printf("Connected to : %s\n", user.name);
send(reqSock, name, 1024, 0);
printf("Type Message: ");
char message[1024] = {'\0'};
scanf("%s", message);
send(reqSock, message, 1024, 0);
}
void * connection (void * arg) {
struct clientInfo session = *((struct clientInfo*)arg);
struct sockaddr_in myself, requestAdd;
int requestSize;
int j = 0;
pthread_t acceptThread;
char userName[1024] = {'\0'};
int acceptSock = socket(PF_INET, SOCK_STREAM, 0);
perror("");
myself = session.cliAdd;
bind(acceptSock, (struct sockaddr*) &myself, sizeof(myself));
listen(acceptSock, 5);
perror("Listening");
while(1) {
requestSize = sizeof(requestAdd);
int reqSock = accept (acceptSock, (struct sockaddr*) &requestAdd, &requestSize);
struct clientInfo userSess;
userSess.id = reqSock;
recv(reqSock, userName, 1024, 0);
strcpy(userSess.name, userName);
userSess.cliAdd = requestAdd;
pthread_create(&acceptThread, NULL, &acceptCon, (void*)&userSess);
}
}
void * acceptCon (void * arg) {
struct clientInfo user = *((struct clientInfo*)arg);
int userSock = user.id;
char buffer[1024] = {'\0'};
int n = recv(userSock, buffer, 1024, 0);
printf("%s : %s\n", user.name, buffer);
}
/* for (i = 0; session[i].id != 0; i++) {
// printf("%d ", try[i]);
printf("%d : ", session[i].id);
printf("%s\n", session[i].name);
char *IP = inet_ntoa(session[i].cliAdd.sin_addr);
int port = ntohs(session[i].cliAdd.sin_port);
//printf("%s : ", IP);
//printf("%d\n", port);
}
printf("\n");
*/