-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_client.cpp
81 lines (73 loc) · 1.89 KB
/
tcp_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
#include <iostream>
#include <WinSock2.h>
#include <WS2tcpip.h>
int main()
{
/**
* Initialize Winsock.
*/
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
std::cerr << "WSAStartup failed." << std::endl;
return 1;
}
/**
* Create a socket.
*
* Address family is "AF_INET" => IPv4 address
* Socket type is "SOCK_STREAM" => TCP
* Protocol is 0 => Default protocol
*/
int network_socket = socket(AF_INET, SOCK_STREAM, 0);
if (network_socket == INVALID_SOCKET)
{
std::cerr << "Socket creation failed: " << WSAGetLastError() << std::endl;
WSACleanup();
return 1;
}
std::cout << "Socket created successfully!" << std::endl
<< std::endl;
/**
* Specify an address for the socket.
*/
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
int connection_status = connect(network_socket, (struct sockaddr *)&server_address, sizeof(server_address));
/**
* Check for error with the connection.
*/
if (connection_status == SOCKET_ERROR)
{
std::cerr << "Connection failed: " << WSAGetLastError() << std::endl;
closesocket(network_socket);
WSACleanup();
return 1;
}
/**
* Recieve data from the server.
*/
char server_response[256];
int bytes_received = recv(network_socket, server_response, sizeof(server_response) - 1, 0);
if (bytes_received > 0)
{
server_response[bytes_received] = '\0';
std::cout << "The server sent the data: " << server_response << std::endl;
}
else if (bytes_received == 0)
{
std::cout << "Connection closed by server." << std::endl;
}
else
{
std::cerr << "Receive failed: " << WSAGetLastError() << std::endl;
}
/**
* Close the socket
*/
closesocket(network_socket);
WSACleanup();
return 0;
}