-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpClient.cpp
146 lines (125 loc) · 4.17 KB
/
TcpClient.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
#include <iostream>
using namespace std;
// Don't forget to include "Ws2_32.lib" in the library list.
#include <winsock2.h>
#include <string.h>
#include <iomanip>
const int TIME_PORT = 27015;
void main()
{
// Initialize Winsock (Windows Sockets).
WSAData wsaData;
if (NO_ERROR != WSAStartup(MAKEWORD(2,2), &wsaData))
{
cout << "Time Client: Error at WSAStartup()\n";
return;
}
// Client side:
// Create a socket and connect to an internet address.
SOCKET connSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == connSocket)
{
cout << "Time Client: Error at socket(): " << WSAGetLastError() << endl;
WSACleanup();
return;
}
//
// Binding the created socket to the IP address and port representing "us".
//
//Note: for clients, if the socket is left unbound, the first call for connect() will
//bind the socket with arbitrary properties (IP and Port)
//If specific values are required, you can use the following code:
//struct sockaddr_in me;
//me.sin_family = AF_INET;
//me.sin_addr.s_addr = INADDR_ANY;
//me.sin_port = 0;
//
// Binding the created socket to the IP address and port representing "us".
//
//if (bind(connSocket, (struct sockaddr*)&me, sizeof(me)) == SOCKET_ERROR)
//{
// cout<<"Time Server: Error at bind(): "<<WSAGetLastError()<<endl;
// closesocket(connSocket);
// WSACleanup();
// return;
//}
// For a client to communicate on a network, it must connect to a server.
// Need to assemble the required data for connection in sockaddr structure.
// Create a sockaddr_in object called server.
sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(TIME_PORT);
// Connect to server.
// The connect function establishes a connection to a specified network
// address. The function uses the socket handler, the sockaddr structure
// (which defines properties of the desired connection) and the length of
// the sockaddr structure (in bytes).
if (SOCKET_ERROR == connect(connSocket, (SOCKADDR *) &server, sizeof(server)))
{
cout << "Time Client: Error at connect(): " << WSAGetLastError() << endl;
closesocket(connSocket);
WSACleanup();
return;
}
// Send and receive data.
int bytesSent = 0;
int bytesRecv = 0;
char sendBuff[255];
char recvBuff[255];
int option = 0;
while(option != 3)
{
cout<<"\nPlease choose an option:\n";
cout<<"1 - Get current time.\n";
cout<<"2 - Get current time in seconds.\n";
cout<<"3 - Exit.\n";
cout<<">> ";
cin>>option;
if(option == 1)
strcpy(sendBuff, "TimeString");
else if(option == 2)
strcpy(sendBuff, "SecondsSince1970");
else if(option == 3)
strcpy(sendBuff, "Exit");
// The send function sends data on a connected socket.
// The buffer to be sent and its size are needed.
// The last argument is an idicator specifying the way
// in which the call is made (0 for default).
bytesSent = send(connSocket, sendBuff, (int)strlen(sendBuff), 0);
if (SOCKET_ERROR == bytesSent)
{
cout << "Time Client: Error at send(): " << WSAGetLastError() << endl;
closesocket(connSocket);
WSACleanup();
return;
}
cout<<"Time Client: Sent: "<<bytesSent<<"/"<<strlen(sendBuff)<<" bytes of \""<<sendBuff<<"\" message.\n";
// Gets the server's answer for options 1 and 2.
if(option == 1 || option == 2)
{
bytesRecv = recv(connSocket, recvBuff, 255, 0);
if (SOCKET_ERROR == bytesRecv)
{
cout << "Time Client: Error at recv(): " << WSAGetLastError() << endl;
closesocket(connSocket);
WSACleanup();
return;
}
if (bytesRecv == 0)
{
cout<<"Server closed the connection\n";
return;
}
recvBuff[bytesRecv]='\0'; //add the null-terminating to make it a string
cout<<"Time Client: Recieved: "<<bytesRecv<<" bytes of \""<<recvBuff<<"\" message.\n";
}
else if(option == 3)
{
// Closing connections and Winsock.
cout << "Time Client: Closing Connection.\n";
closesocket(connSocket);
WSACleanup();
}
}
}