-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cpp
66 lines (55 loc) · 1.99 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
#include <iostream>
#include <string>
#include <Winsock2.h>
#include <Ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#pragma warning(disable:4996)
#define SERVER_IP "127.0.0.1"
#define PORT 8888
#define BUFFER_SIZE 1024
int main() {
WSADATA wsaData;
SOCKET clientSocket;
struct sockaddr_in serverAddress;
char buffer[BUFFER_SIZE];
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cerr << "Failed to initialize Winsock" << std::endl;
return -1;
}
// Create socket
clientSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (clientSocket == INVALID_SOCKET) {
std::cerr << "Failed to create socket: " << WSAGetLastError() << std::endl;
WSACleanup();
return -1;
}
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP);
serverAddress.sin_port = htons(PORT);
while (true) {
// Read input from the user
std::cout << "Enter message: ";
std::string message;
std::getline(std::cin, message);
// Send data to server
int sendResult = sendto(clientSocket, message.c_str(), message.size(), 0, (struct sockaddr*)&serverAddress, sizeof(serverAddress));
if (sendResult == SOCKET_ERROR) {
std::cerr << "Failed to send data: " << WSAGetLastError() << std::endl;
break;
}
// Receive data from server
int serverAddressSize = sizeof(serverAddress);
int recvResult = recvfrom(clientSocket, buffer, BUFFER_SIZE, 0, (struct sockaddr*)&serverAddress, &serverAddressSize);
if (recvResult == SOCKET_ERROR) {
std::cerr << "Failed to receive data: " << WSAGetLastError() << std::endl;
break;
}
// Print received data
std::cout << "Received from server: " << buffer << std::endl;
}
// Cleanup
closesocket(clientSocket);
WSACleanup();
return 0;
}