Добавил:
Upload
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабы по 2 RKG с нета / ПО сетей ЭВМ / Лаб_1 / time
.cpp#include <winsock2.h>
#include <iostream.h>
#include <time.h>
#include <stdio.h>
#include <conio.h>
int Time(char * strHost)
{
int s, res;
sockaddr_in clnt_sin;
time_t dwTime;
struct tm *newtime;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(1,1), &wsaData))
{
cout << "WSAStartup failed";
return 1;
}
if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
cout << "Socket failed" << endl;
return 2;
}
memset((char *)&clnt_sin, NULL, sizeof(clnt_sin));
clnt_sin.sin_family = AF_INET;
clnt_sin.sin_addr.s_addr = inet_addr(strHost);
clnt_sin.sin_port = htons(37);
res = connect(s,(sockaddr *)&clnt_sin, sizeof(clnt_sin));
if (res == SOCKET_ERROR)
{
cout << "Connect failed" << endl;
return 4;
}
else
{
cout << "Connected!" << endl;
}
if (recv(s, (char*)&dwTime, sizeof(dwTime), 0) == SOCKET_ERROR)
{
cout << "Recv failed" << endl;
return 6;
}
dwTime = ntohl(dwTime);
dwTime -= 2208988800;
newtime = localtime(&dwTime);
cout << asctime(newtime) << endl;
getch();
closesocket(s);
WSACleanup();
return 0;
}