Untitled
unknown
plain_text
4 years ago
3.4 kB
8
Indexable
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#include <winsock2.h>
#include <string.h>
#define TIME_PORT 27015
void showMenu(int* choise);
void main()
{
enum request
{
whatItsTheTime = 1,
howAreYou = 2,
whatIsTheWether = 3
};
int choise = -1;
showMenu(&choise);
while (choise != 4) {
showMenu(&choise);
// 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_DGRAM, IPPROTO_UDP);
if (INVALID_SOCKET == connSocket)
{
cout << "Time Client: Error at socket(): " << WSAGetLastError() << endl;
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);
// Send and receive data.
int bytesSent = 0;
int bytesRecv = 0;
char sendBuff[255];
switch (choise)
{
case whatItsTheTime:
strcpy(sendBuff, "1" );
break;
case howAreYou:
strcpy(sendBuff, "2");
break;
case whatIsTheWether:
strcpy(sendBuff, "3");
break;
}
char recvBuff[255];
// Asks the server what's the currnet time.
// The send function sends data on a connected socket.
// The buffer to be sent and its size are needed.
// The fourth argument is an idicator specifying the way in which the call is made (0 for default).
// The two last arguments hold the details of the server to communicate with.
// NOTE: the last argument should always be the actual size of the client's data-structure (i.e. sizeof(sockaddr)).
bytesSent = sendto(connSocket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&server, sizeof(server));
if (SOCKET_ERROR == bytesSent)
{
cout << "Time Client: Error at sendto(): " << WSAGetLastError() << endl;
closesocket(connSocket);
WSACleanup();
return;
}
cout << "Time Client: Sent: " << bytesSent << "/" << strlen(sendBuff) << " bytes of \"" << sendBuff << "\" message.\n";
// Gets the server's answer using simple recieve (no need to hold the server's address).
bytesRecv = recv(connSocket, recvBuff, 255, 0);
if (SOCKET_ERROR == bytesRecv)
{
cout << "Time Client: Error at recv(): " << WSAGetLastError() << endl;
closesocket(connSocket);
WSACleanup();
return;
}
recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
cout << "Time Client: Recieved: " << bytesRecv << " bytes of \"" << recvBuff << "\" message.\n";
// Closing connections and Winsock.
cout << "Time Client: Closing Connection.\n";
closesocket(connSocket);
system("pause");
}
}
void showMenu(int* choise)
{
cout << "Please Enter you choise\n";
cout << "1.What It's The Time ?\n";
cout << "2.How Are You ?\n";
cout << "3.What is the Wether ?\n";
cout << "4. Exit ?\n";
cin >> *choise;
}Editor is loading...