-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathclient.cpp
139 lines (123 loc) · 2.83 KB
/
client.cpp
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
#include <bits/stdc++.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <thread>
#include <signal.h>
#include <mutex>
#define MAX_LEN 200
#define NUM_COLORS 6
using namespace std;
bool exit_flag=false;
thread t_send, t_recv;
int client_socket;
string def_col="\033[0m";
string colors[]={"\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m"};
void catch_ctrl_c(int signal);
string color(int code);
int eraseText(int cnt);
void send_message(int client_socket);
void recv_message(int client_socket);
int main()
{
if((client_socket=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket: ");
exit(-1);
}
struct sockaddr_in client;
client.sin_family=AF_INET;
client.sin_port=htons(10000); // Port no. of server
client.sin_addr.s_addr=INADDR_ANY;
//client.sin_addr.s_addr=inet_addr("127.0.0.1"); // Provide IP address of server
bzero(&client.sin_zero,0);
if((connect(client_socket,(struct sockaddr *)&client,sizeof(struct sockaddr_in)))==-1)
{
perror("connect: ");
exit(-1);
}
signal(SIGINT, catch_ctrl_c);
char name[MAX_LEN];
cout<<"Enter your name : ";
cin.getline(name,MAX_LEN);
send(client_socket,name,sizeof(name),0);
cout<<colors[NUM_COLORS-1]<<"\n\t ====== Welcome to the chat-room ====== "<<endl<<def_col;
thread t1(send_message, client_socket);
thread t2(recv_message, client_socket);
t_send=move(t1);
t_recv=move(t2);
if(t_send.joinable())
t_send.join();
if(t_recv.joinable())
t_recv.join();
return 0;
}
// Handler for "Ctrl + C"
void catch_ctrl_c(int signal)
{
char str[MAX_LEN]="#exit";
send(client_socket,str,sizeof(str),0);
exit_flag=true;
t_send.detach();
t_recv.detach();
close(client_socket);
exit(signal);
}
string color(int code)
{
return colors[code%NUM_COLORS];
}
// Erase text from terminal
int eraseText(int cnt)
{
char back_space=8;
for(int i=0; i<cnt; i++)
{
cout<<back_space;
}
}
// Send message to everyone
void send_message(int client_socket)
{
while(1)
{
cout<<colors[1]<<"You : "<<def_col;
char str[MAX_LEN];
cin.getline(str,MAX_LEN);
send(client_socket,str,sizeof(str),0);
if(strcmp(str,"#exit")==0)
{
exit_flag=true;
t_recv.detach();
close(client_socket);
return;
}
}
}
// Receive message
void recv_message(int client_socket)
{
while(1)
{
if(exit_flag)
return;
char name[MAX_LEN], str[MAX_LEN];
int color_code;
int bytes_received=recv(client_socket,name,sizeof(name),0);
if(bytes_received<=0)
continue;
recv(client_socket,&color_code,sizeof(color_code),0);
recv(client_socket,str,sizeof(str),0);
eraseText(6);
if(strcmp(name,"#NULL")!=0)
cout<<color(color_code)<<name<<" : "<<def_col<<str<<endl;
else
cout<<color(color_code)<<str<<endl;
cout<<colors[1]<<"You : "<<def_col;
fflush(stdout);
}
}