forked from matricali/simple-client-server-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
151 lines (123 loc) · 3.69 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
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <thread>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#define DEFAULT_BUFLEN 256
/* Declarations */
struct client_type
{
int id;
int sockfd;
char received_message[DEFAULT_BUFLEN];
};
const int INVALID_SOCKET = -1;
const int SOCKET_ERROR = -1;
/* Function prototypes */
int process_client(client_type &new_client);
int process_client(client_type &new_client)
{
while (true) {
memset(new_client.received_message, 0, DEFAULT_BUFLEN);
if (new_client.sockfd != 0) {
int iResult = recv(
new_client.sockfd,
new_client.received_message,
DEFAULT_BUFLEN, 0
);
if (iResult != SOCKET_ERROR) {
printf("%s\n", new_client.received_message);
} else {
fprintf(stderr, "recv() failed\n");
break;
}
}
}
// if (WSAGetLastError() == WSAECONNRESET)
fprintf(stderr, "The server has disconnected\n");
return 0;
}
int main(int argc, char** argv)
{
int port_no;
struct sockaddr_in server_addr;
struct hostent *server;
client_type client = { INVALID_SOCKET, -1, "" };
std::string message;
std::string sent_message = "";
int ret = 0;
/* Banner */
fprintf(stdout, "simple-chat-client v0.0.1-alpha\n");
/* Arguments */
if (argc < 3) {
fprintf(stderr, "Usage: %s hostname port\n", argv[0]);
return -1;
}
port_no = atoi(argv[2]);
if (port_no <= 0) {
fprintf(stderr, "Invalid port -.-\n");
return -1;
}
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
return -2;
}
fprintf(stdout, "Starting client...\n");
/* Opening socket */
client.sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (client.sockfd < 0) {
fprintf(stderr, "ERROR opening socket\n");
return -2;
}
bzero((char *) &server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
bcopy(
(char *)server->h_addr,
(char *)&server_addr.sin_addr.s_addr,
server->h_length
);
server_addr.sin_port = htons(port_no);
/* Connecting */
printf("Trying connect to %s:%d...\n", (char *)server->h_addr, port_no);
if (connect(client.sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
close(client.sockfd);
fprintf(stderr, "Unable to connect to server!\n");
return -3;
}
fprintf(stdout, "Connected!\n");
/* Receiving handshake */
recv(client.sockfd, client.received_message, DEFAULT_BUFLEN, 0);
message = client.received_message;
if (message != "Server is full") {
client.id = atoi(client.received_message);
std::thread my_thread(process_client, std::ref(client));
while (true) {
fprintf(stdout, "You> ");
getline(std::cin, sent_message);
ret = send(client.sockfd, sent_message.c_str(), strlen(sent_message.c_str()), 0);
if (ret <= 0) {
fprintf(stderr, "send() failed\n");
break;
}
}
/* Shutdown the connection since no more data will be sent */
my_thread.detach();
} else {
printf("%s\n", client.received_message);
}
/* Closing socket */
fprintf(stdout, "Closing socket...\n");
ret = shutdown(client.sockfd, SHUT_WR);
if (ret == SOCKET_ERROR) {
fprintf(stderr, "shutdown() failed with error.\n");
close(client.sockfd);
return -4;
}
close(client.sockfd);
return 0;
}