资源简介
这个程序是一个简单的时间获取,用于客户端和服务器的交互,用的是TCP/IP协议,用c语言编写的,还是比较简单的。
代码片段和文件信息
// Shangzhi.cpp : Defines the entry point for the console application.
//
#include “stdafx.h“
#include “winsock2.h“
#include “stdlib.h“
void usage(_TCHAR *name)
{
fprintf(stderr “usage: %s [ip [port]]\n“ name);
}
int _tmain(int argc _TCHAR* argv[]) //此处_TCHAR*在vc6下应是char*
{
SOCKET sock;
struct sockaddr_in sa;
int err;
int servport = 5555;
char buff[256];
WSADATA wsd;
unsigned long uladdr;
//if(argc > 3 || argc < 2)
//{
// usage(argv[0]);
// return -1;
//}
// if(argc > 1)
if(1)
{
//char* pBuffer = (char *)malloc(20);
//wcstombs(pBuffer argv[1] 20 );
// uladdr = inet_addr(pBuffer);
uladdr = inet_addr(“127.0.0.1“);
if(uladdr == INADDR_NONE)
{
usage(argv[0]);
return -1;
}
}
if(argc == 3)
{
servport = _wtoi(argv[2]); //此处_wtoi在vc6下应是atoi
if(servport > 65535 || servport <1)
{
usage(argv[0]);
return -2;
}
}
if(WSAStartup(MAKEWORD(11) &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(“connecting ...\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;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
....... 32256 2008-03-13 00:00 简单的时间获取\bin\client.exe
....... 32256 2008-03-13 00:00 简单的时间获取\bin\client_ip.exe
....... 31232 2008-03-13 00:00 简单的时间获取\bin\server.exe
....... 1911 2008-03-13 00:00 简单的时间获取\client.cpp
....... 2485 2008-03-13 00:00 简单的时间获取\server.cpp
目录 0 2010-11-10 11:27 简单的时间获取\bin
目录 0 2010-11-10 11:27 简单的时间获取
----------- --------- ---------- ----- ----
100140 7
评论
共有 条评论