#include< stdio.h >
#include< string.h >
#include< winsock.h >
#include< windows.h >
#include<iostream>
#pragma comment( lib, "ws2_32.lib" )
#define PORT 2046
#define BACKLOG 10
#define TRUE 1
#define MAXDATASIZE 1000
using namespace std;
//VOID CALLBACK my_timer();
void main( void )
{
int iServerSock;
int m=0;
//
全局变量接受服务器发送过来的充值时间
// int iClientSock;
int addr_len;
int numbytes;
char buf[ MAXDATASIZE ];
//
定义接受缓存
struct sockaddr_in ServerAddr;
//
定义IP的结构
struct sockaddr_in ClientAddr;
WSADATA WSAData;
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )
//
启动套接字
{
printf( "initializationing error!\n" );
WSACleanup( );
//
关闭套接字
exit( 0 );
}
iServerSock = socket( AF_INET, SOCK_DGRAM, 0 );
//
创建套接字
if( iServerSock == INVALID_SOCKET )
{
printf( "
创建套接字失败!\n" );
WSACleanup( );
//
关闭套接字
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );
//
监视的端口号
ServerAddr.sin_addr.s_addr = INADDR_ANY;
//
本地IP
memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 )
//
绑定套接字
{
printf( "bind
调用失败!\n" );
WSACleanup( );
exit( 0 );
}
addr_len = sizeof( struct sockaddr );
numbytes = recvfrom( iServerSock, buf, MAXDATASIZE, 0, ( struct sockaddr * ) & ClientAddr, &addr_len );
//
接受数据
if( numbytes == -1 )
{
printf( "recvfrom
调用失败!\n" );
WSACleanup( );
exit( 0 );
}
buf[ numbytes ] = '\0';
printf( "packet contains %s\n", buf );
m=int(buf);
//
将字符类型转换为整型
::SetTimer(NULL,0,1000,0);
//
调用系统时间开始计时
MSG msg;
while(GetMessage(&msg,0,0,0))
//
查找的是本线程消息队列及属于本线程的所有窗口的消息
{
printf("
你当前的余额为%d\n",m);
m=m--;
if(m==10)
printf("
系统在10秒钟后关机\n");
if(m==0)
{
printf("
系统马上关机\n");
system("shutdown -s ");
}
}
closesocket( iServerSock );
WSACleanup( );
}
本文出自 “爱你一万年” 博客,转载请与作者联系!