-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
108 lines (68 loc) · 1.98 KB
/
client.cpp
File metadata and controls
108 lines (68 loc) · 1.98 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
101
102
103
104
105
106
107
108
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <cstring>
#include <string>
#include <thread>
#define PORT 8080
void receiveMessages(SOCKET serverSocket)
{
char buffer[1024];
int bytesReceived;
while (true)
{
bytesReceived = recv(serverSocket, buffer, sizeof(buffer), 0);
buffer[bytesReceived] = '\0';
std::cout << "Message from Server: " << buffer << std::endl;
if(bytesReceived<=0)
{
break;
}
}
}
void sendMessages(SOCKET clientSocket)
{
while(true){
std::string message;
std::cout<<"the message you want to send to the server: "<< message<<std::endl;
std::getline(std::cin ,message);
send(clientSocket, message.c_str(), message.length(),0);
if(message=="exit")
{
break;
}
}
}
int main(){
WSADATA wsadata;
int result = WSAStartup(MAKEWORD(2,2),&wsadata);
if (result!=0){
std::cerr << "WSAStartup basarisiz oldu: " << result << std::endl;
return 1;
}
SOCKET clientSocket = INVALID_SOCKET;
clientSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(clientSocket==INVALID_SOCKET){
std::cerr<<"Socket creation bu hata ile fail oldu: " << WSAGetLastError() << std::endl;
return 1 ;
}
sockaddr_in serverAddr={};
serverAddr.sin_family=AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = INADDR_ANY;
inet_pton(AF_INET, "127.0.0.1", &serverAddr.sin_addr);
if (connect(clientSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
std::cerr << "Connect failed with error: " << WSAGetLastError() << std::endl;
closesocket(clientSocket);
WSACleanup();
return 1;
}
std::thread receiveIslem(receiveMessages,clientSocket);
std::thread sendIslem(sendMessages,clientSocket);
receiveIslem.join();
sendIslem.join();
std::cout << "Sunucu kapatiliyor." << std::endl;
closesocket(clientSocket);
WSACleanup();
return 0 ;
}