-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.cpp
168 lines (133 loc) · 3.1 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
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
// Client.c
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <signal.h>
#include <thread>
#include <iostream>
#define MAXIN 20
#define MAXSE 36
#define MAXOUT 1024
#define FLUSH “\e[1;1H\e[2J”
#define RED "\x1B[31m"
#define RESET "\x1B[0m"
#define SPADES "\xE2\x99\xA0"
#define CLUBS "\xE2\x99\xA3"
#define HEARTS "\xE2\x99\xA5"
#define DIAMONDS "\xE2\x99\xA6"
using namespace std;
int sockfd;
thread rThread, wThread;
void parseAndDisplay(string s){
if(s[0]=='^'){
cout.flush();
char suite=s[1], num=s[2];
if(suite=='h')
cout << ( RED HEARTS ) << num << RESET << " ";
else if(suite=='d')
cout << ( RED DIAMONDS ) << num << RESET << " ";
else if(suite=='s')
cout << ( SPADES ) << num << RESET << " ";
else
cout << ( CLUBS ) << num << RESET << " ";
cout.flush();
// printf("\n");
return;
}
else
// cout << s << "\n";
printf("%s\n", s.c_str());
}
void displayMessage(int consockfd)
{
int n;
char rcvbuf[MAXOUT];
while (1)
{
memset(rcvbuf, 0, MAXOUT); /* clear */
n = read(sockfd, rcvbuf, MAXOUT - 1);
if (n <= 0)
return;
cout << string(rcvbuf) << endl;
// parseAndDisplay(string(rcvbuf));
}
return;
}
void sendMessage(int fd, string s)
{
if (s == "")
return;
char* arr = new char[s.size() + 1];
strcpy(arr, s.c_str());
write(fd, arr, strlen(arr));
delete[] arr;
}
void readAndSendData(int consockfd)
{
int n;
char payload[MAXIN];
char tempbuf[MAXIN];
while (1)
{
string s;
cin >> s;
sendMessage(consockfd, s);
if (n <= 0)
return;
}
}
void initialConnect(string username)
{
// int n = write(sockfd, username, strlen(username));
sendMessage(sockfd, username);
rThread = thread(displayMessage, sockfd);
readAndSendData(sockfd);
rThread.join();
// wThread.join();
}
void closeHandler(int sig)
{
string s = "CLOSE";
sendMessage(sockfd, s);
cout << "\nPlease wait for 2 Seconds\n";
sleep(2);
close(sockfd);
exit(0);
}
// Server address
struct hostent *buildServerAddr(struct sockaddr_in *serv_addr, char *serverIP, int portno)
{
/* Construct an address for remote server */
memset((char *) serv_addr, 0, sizeof(struct sockaddr_in));
serv_addr->sin_family = AF_INET;
inet_aton(serverIP, &(serv_addr->sin_addr));
serv_addr->sin_port = htons(portno);
}
int main()
{
signal(SIGINT, closeHandler);
//Client protocol
// char serverIP[] = "192.168.29.51";
char serverIP[] = "127.0.0.1";
int portno = 9000;
struct sockaddr_in serv_addr;
buildServerAddr(&serv_addr, serverIP, portno);
/* Create a TCP socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* Connect to server on port */
connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
// printf("Connected to %s:%d\n",serverIP, portno);
char userID[12];
printf("%s: ", "Enter Name");
scanf("%s", userID);
/* Carry out Client-Server protocol */
initialConnect(userID);
/* Clean up on termination */
// close(sockfd);
}