// time.cpp : Defines the entry point for the console application.
//
// client.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include "winsock.h"
#include "stdlib.h"void usage(char *name)
{
fprintf(stderr, "usage: %s [ip [port]]\n", name);
}int main(int argc,char *argv[])
{
SOCKET sock;
struct sockaddr_in sa;
int err;
int servport = 5555; /* 默认去连接的端口 */
char buff[256]; /* 存放服务端返回的信息 */
WSADATA wsd;
unsigned long uladdr; /* 转换后的服务器地址 */
printf("************************************************************\n");
printf("*time client sample,written by suyu,chenyu\n");
printf("*mail:[email protected]                    \n");
printf("************************************************************\n");
if (argc > 3 || argc < 2) {
usage(argv[0]);
return -1;
} if (argc > 1) {
uladdr = inet_addr(argv[1]);
if (uladdr == INADDR_NONE) {
usage(argv[0]);
return -1;
}
} if (argc == 3) {
servport = atoi(argv[2]);
if (servport > 65535 || servport < 1) { /* 如不是合法端口,则退出*/
usage(argv[0]);
return -2;
}
} /* 初始化Winsock1.1 */
if (WSAStartup(MAKEWORD(1,1), &wsd) != 0) {
printf("WSAStartup() failed !\n");
return -3;
} /* 创建连接套接字 */
printf("create socket ...\n");
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
fprintf(stderr, "socket() failed: %d\n", WSAGetLastError());
return -4;
}
printf("[OK]\n"); /* 填充要连接的服务器地址结构 */
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(servport);
sa.sin_addr.S_un.S_addr = uladdr; /* 连接服务器 */
printf("connect ...\n");
err = connect(sock, (const sockaddr *)&sa,sizeof(sa));
if (err != 0) {
fprintf(stderr, "connect() failed: %d\n", WSAGetLastError());
return -5;
}
printf("[OK]\n"); /* 接收返回的时间/日期信息 */
printf("recv data ...\n");
memset(buff, 0, sizeof(buff));
if (recv(sock, buff, sizeof(buff), 0) == SOCKET_ERROR) {
fprintf(stderr, " recv error !\n");
return -1;
}
printf("[OK]\n");/* 输出返回的信息 */
printf("Server Date/time : \n%s\n", buff);

closesocket(sock); /* 关闭连接套接字 */
WSACleanup(); /* 释放资源 */
return 0;
}--------------------Configuration: time - Win32 Debug--------------------
Compiling...
Command line warning D4028 : minimal rebuild failure, reverting to normal build
time.cpp
e:\vc_game\chapter3\client\time.cpp(8) : fatal error C1083: Cannot open precompiled header file: 'Debug/client.pch': No such file or directory
Error executing cl.exe.time.obj - 1 error(s), 1 warning(s)如何修改??