-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_client.cpp
193 lines (168 loc) · 6.28 KB
/
chat_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
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
#include<ncurses.h>
#include<string>
#include <cstddef>
#include <unistd.h>
#include <cstdio>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include<iostream>
#include <arpa/inet.h>
#include <signal.h>
#include<thread>
#include<mutex>
using namespace std;
class ClientWin{
WINDOW *stdscr,*inp_win;
mutex write_mtx;
int inp_win_height=3;
public:
~ClientWin(){
endwin();
}
void write(string a){
lock_guard<mutex> lck(write_mtx);
int y,x;
getyx(stdscr,y,x);
mvwprintw(stdscr,++y,0,"> ");
printw(a.c_str());
refresh();
};
string read(){
char str[200];
wmove(inp_win,1,1);
wclrtoeol(inp_win);
wrefresh(inp_win);
mvwgetstr(inp_win,1,1,str);
wmove(inp_win,1,1);
wclrtoeol(inp_win);
wrefresh(inp_win);
return string(str);
};
void run(){
stdscr=initscr();
refresh();
cbreak();
int starty=(LINES-inp_win_height);
inp_win=newwin(inp_win_height,COLS,starty,0);
box(inp_win,0,0);
wrefresh(inp_win);
}
};
class TCP_CLIENT{
int sockfd;
struct addrinfo hints,*servinfo,*addr_iter;
const char *hostname,*port;
ClientWin *out;
int MAXDATASIZE;
public:
TCP_CLIENT(string host,string port,ClientWin *out,int max_datasize=100):hostname(host.c_str()),port(port.c_str()),out(out),MAXDATASIZE(max_datasize){
memset(&hints,0,sizeof hints);
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
int addrinfo_retvalue;
if((addrinfo_retvalue=getaddrinfo(hostname,this->port,&hints,&servinfo))!=0){
fprintf(stderr,"getaddrinfo: %s\n",gai_strerror(addrinfo_retvalue));
exit(1);
}
};
~TCP_CLIENT(){
freeaddrinfo(servinfo);
close(sockfd);
}
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
void connectToServer(){
for(addr_iter=servinfo;addr_iter!=NULL;addr_iter=addr_iter->ai_next){
if((sockfd=socket(addr_iter->ai_family,addr_iter->ai_socktype,addr_iter->ai_protocol))==-1){
out->write(strerror(errno));
continue;
}
if(connect(sockfd,addr_iter->ai_addr,addr_iter->ai_addrlen)==-1){
close(sockfd);
out->write(strerror(errno));
continue;
}
break;
}
if(addr_iter==NULL){
fprintf(stderr, "client: failed to connect\n");
exit(1);
}
char s[INET6_ADDRSTRLEN];
inet_ntop(addr_iter->ai_family,get_in_addr((sockaddr*)addr_iter->ai_addr),s,sizeof s);
char connnected_msg[100];
sprintf(connnected_msg,"client: connected to %s\n", s);
out->write(connnected_msg);
}
string receive(){
char buffer[MAXDATASIZE];
int numbytes;
if((numbytes=recv(sockfd,buffer,MAXDATASIZE-1,0))==-1){
perror("recv");
exit(1);
}
buffer[numbytes]='\0';
return string(buffer);
}
void sendMsg(string msg){
int numbytes;
const char* str=msg.c_str();
if (send(sockfd,str,strlen(str), 0) == -1){
out->write(strerror(errno));
}
}
};
class ClientApp{
TCP_CLIENT *client;
ClientWin *window;
public:
ClientApp(string hostname,string port){
window=new ClientWin();
client=new TCP_CLIENT(hostname,port,window);
window->run();
client->connectToServer();
thread receiver(&ClientApp::receive_and_print,this);
thread inp_send(&ClientApp::input_and_send,this);
inp_send.join();
receiver.join();
}
void input_and_send(){
window->write("Please enter your name");
auto name=window->read();
client->sendMsg(name);
while(1){
auto msg=window->read();
// cerr<<msg<<endl;
client->sendMsg(msg);
}
}
void receive_and_print(){
while(1){
window->write(client->receive());
}
}
~ClientApp(){
free( window);
free( client);
}
};
int main(int argc,char **argv){
// string hostname="127.0.0.1";
// string port="3491";
if (argc != 3) {
printf("Usage: %s <hostname> <port>\n", argv[0]);
exit(1);
}
string hostname(argv[1]);
string port(argv[2]);
ClientApp(hostname,port);
}