-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient2.cpp
More file actions
100 lines (85 loc) · 2.17 KB
/
client2.cpp
File metadata and controls
100 lines (85 loc) · 2.17 KB
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
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <thread>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
bool fn_dll() {
WSAData data;
return WSAStartup(MAKEWORD(2, 2), &data) == 0;
}
void sendmessage(SOCKET s){
string name;
string message;
cout << "Enter your chat name: ";
getline(cin, name);
while(1){
getline(cin, message);
string msg = name + " : " + message;
if(message=="exit"){
break;
}
int bytesend = send(s, msg.c_str(), msg.length(), 0);
if(bytesend==SOCKET_ERROR){
cout << "Error sending message\n";
break;
}
}
closesocket(s);
WSACleanup();
}
void recvmessage(SOCKET s){
char buffer[4096];
string msg = "";
while(1){
int byte_recv = recv(s, buffer, sizeof(buffer), 0);
if (byte_recv<=0) {
cout << "client disconnected" << endl;
break;
}
else{
msg = string(buffer, byte_recv);
cout << msg << endl;
}
}
closesocket(s);
WSACleanup();
}
int main() {
cout << "client side code" << endl;
if (!fn_dll()) {
cout << "Failed to initialize lib" << endl;
return 1;
}
else {
cout << "Successfully initialized lib" << endl;
}
SOCKET s;
s = socket(AF_INET, SOCK_STREAM, 0);
if (s == INVALID_SOCKET) {
cout << "Failed to create socket" << endl;
}
else {
cout << "Successfully created socket" << endl;
}
string server_addr = "127.0.0.1";
int port = 1234;
sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port);
inet_pton(AF_INET, server_addr.c_str(), &server_address.sin_addr);
if (connect(s, (struct sockaddr*)&server_address, sizeof(server_address)) == SOCKET_ERROR) {
cout << "Failed to connect to server" << endl;
closesocket(s);
WSACleanup();
return 1;
}
else {
cout << "Successfully connected to server" << endl;
}
thread sender_thread(sendmessage, s);
thread recvmessage_thread(recvmessage, s);
sender_thread.join();
recvmessage_thread.join();
WSACleanup();
}