VC编程(1)Socket网络编程 连接邮箱服务器+授时服务器

【转载请注明出处:http://blog.csdn.net/leytton/article/details/37727491

SocketLogin函数过程:Socket连接网易邮件服务器,接收服务器欢迎消息,向服务器发送退出指令,接收服务器Bye消息,关闭socket

SocketGetTime函数过程:Socket连接授时服务器,接收服务器时间消息,关闭socket

效果图:



源代码:


#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"

#include "winsock2.h"

#define HIGHTIME  21968699 // 21968708 // Jan 1, 1900 FILETIME.highTime
#define LOWTIME   4259332096 // 1604626432 // Jan 1, 1900 FILETIME.lowtime

BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
        HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
		HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
    }

    return FALSE;
}

BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
    return TRUE;
}


//声明
void SocketLogin(HWND hwnd);
void SocketGetTime(HWND hwnd);
void ConvertToSystemTime(DWORD dwTime,LPSYSTEMTIME st);
void ShowError(HWND hwnd);

void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
    switch(id)
    {
        case IDC_BUTTON_LOGIN:   //登录按钮ID
			{
			    SocketLogin(hwnd);
			}
        break;
		case IDC_BUTTON_GETTIME: //获取时间按钮ID
			{
				SocketGetTime(hwnd);
			}
		break;
        default:
		break;
    }
}

void Main_OnClose(HWND hwnd)  //此处询问是否退出,关闭窗口时执行,如果不执行函数EndDialog(hwnd, 0)则不退出,可以恶搞哦 ^_^
{
	int i=MessageBox(NULL,TEXT("您真的退出?"),TEXT("退出"),MB_YESNO);//是、否
	if(IDYES==i){   //如果选择"是",选择"否"默认不执行
		EndDialog(hwnd, 0);
	}
}


void SocketLogin(HWND hwnd){
		WSADATA wsaData;
		//初始化Socket库
		WSAStartup(MAKEWORD(2,0),&wsaData);
		//创建一根电线
		SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		SOCKADDR_IN sa;
		sa.sin_family=AF_INET;
		//设置电线连接服务器端的端口
		sa.sin_port = htons(IPPORT_SMTP);
		//sa.sin_port = htons(IPPORT_FTP);
		//123.125.50.135是ping smtp.163.com出来的
		sa.sin_addr.S_un.S_addr = inet_addr("220.181.12.16");

		if(connect(sock,(SOCKADDR *)&sa,sizeof(sa))==SOCKET_ERROR)   //如果发生错误,则调用ShowError(hwnd);
		{
			ShowError(hwnd);
			return;
		}

		//接收问候语 
		TCHAR buff[256];
		ZeroMemory(buff,sizeof(buff)/sizeof(TCHAR));
		recv(sock,buff,sizeof(buff)/sizeof(TCHAR),0);
		MessageBox(hwnd,buff,TEXT("返回消息"),MB_OK);

		//发送消息;注意不能忘了末尾的回车 
		TCHAR cQuit[] = "QUIT\n";
		send(sock,cQuit,lstrlen(cQuit),0); 

		//接收
		ZeroMemory(buff,sizeof(buff)/sizeof(TCHAR));
		recv(sock,buff,sizeof(buff)/sizeof(TCHAR),0);
		MessageBox(hwnd,buff,TEXT("返回消息"),MB_OK);

		closesocket(sock);
		WSACleanup();
}


void SocketGetTime(HWND hwnd){
	WSADATA wsaData;
	//初始化Socket库
	WSAStartup(MAKEWORD(2,0),&wsaData);
	//创建一根电线
	SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	SOCKADDR_IN sa;
	sa.sin_family=AF_INET;
	//设置电线连接服务器端的端口
	sa.sin_port = htons(IPPORT_TIMESERVER);
	//sa.sin_port = htons(IPPORT_FTP);
	//128.138.141.172是ping time.nist.gov出来的
	sa.sin_addr.S_un.S_addr = inet_addr("128.138.141.172");

	if(connect(sock,(SOCKADDR *)&sa,sizeof(sa))==SOCKET_ERROR)
	{
		ShowError(hwnd);
		return;
	}
	
	unsigned long uTime;
	recv(sock,(char *)&uTime,sizeof(unsigned long),0);
	
	uTime = ntohl(uTime);  //从网络字节顺序转换为主机字节顺序。不同的CPU中处理整数的方式不一样,有的是低位在前、有的是高位在前。

	SYSTEMTIME st;
	ConvertToSystemTime(uTime,&st);
	
	//SetSystemTime(&st);//设置系统时间
	
	TCHAR timeStr[256];
	ZeroMemory(timeStr,sizeof(timeStr)/sizeof(TCHAR));	
	wsprintf(timeStr,"时间正确吗?%i年%i月%i日%i时%i分%i秒",st.wYear,st.wMonth,st.wDay,st.wHour+8,st.wMinute,st.wSecond);
	MessageBox(NULL,timeStr,TEXT("网络时间"),MB_OK);

	closesocket(sock);
	WSACleanup();
}


void ConvertToSystemTime(DWORD dwTime,LPSYSTEMTIME st)
{
	UINT64 uiCurTime, uiBaseTime, uiResult; 
	uiBaseTime = ((UINT64) HIGHTIME << 32) + LOWTIME; 
	uiCurTime = (UINT64)dwTime * (UINT64)10000000;   
        uiResult = uiBaseTime + uiCurTime; 
        FileTimeToSystemTime((LPFILETIME)&uiResult, st);  
}

void ShowError(HWND hwnd)
{
TCHAR* lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| //自动分配消息缓冲区
FORMAT_MESSAGE_FROM_SYSTEM, //从系统获取信息
NULL,GetLastError(), //获取错误信息标识
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),//使用系统缺省语言
(LPTSTR)&lpMsgBuf, //消息缓冲区
0,
NULL);
MessageBox(hwnd,lpMsgBuf,"",MB_ICONERROR); 
}


posted @ 2014-07-12 17:25  Leytton  阅读(284)  评论(0编辑  收藏  举报