本文主要是介绍VC网络协议,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// PCControlDlg.cpp : 实现文件
//#include "stdafx.h"
#include "PCControl.h"
#include "PCControlDlg.h"
#include "afxdialogex.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif// 用于应用程序“关于”菜单项的 CAboutDlg 对话框#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif#define WM_KEYSTROKE (WM_USER + 755)//This message is recieved when key is down/up
#define WM_NC (WM_USER+1001)#define SOCKET_PORT htons(0xE917);//7016
//#define SOCKET_PORT 59670/
// CAboutDlg dialog used for App AboutSOCKET ServerSock;
SOCKET connectSock;
struct sockaddr_in dstclient_addr;
char recv_message_server[256] = {0};
char send_message[256] = {0};HANDLE hThread;
DWORD ThreadID;
volatile BOOL ThreadRun = false;const char* WINDOWNHANDLERNAME = "liyue.control.pc";//server
BOOL InItServerSock() //初始化服务端socket
{//Variable Defineint Status;WORD wMajorVersion , wMinorVersion; WORD wVersionReqd;WSADATA lpmyWSAData; //版本信息//InI WinsockwMajorVersion = 1;wMinorVersion = 2;wVersionReqd = MAKEWORD(wMajorVersion,wMinorVersion); //指定版本 ;(高,低)//Startup WinSockStatus = WSAStartup(wVersionReqd, &lpmyWSAData); //加载socket库;(版本, 返回版本信息lpmyWSAData)if (Status != 0)return FALSE; //SocketServerSock = socket(AF_INET , SOCK_STREAM , 0); //创建socket ; (默认, 流式socket, 自动选择)if (ServerSock == INVALID_SOCKET) return FALSE;dstclient_addr.sin_family = PF_INET; // 地址族 ; PF_INET表示这是搞IP地址//dstclient_addr.sin_port = SOCKET_PORT;dstclient_addr.sin_port = htons(6000); // 指定分配给socket的端口号 ; htons()将主机的无符号短整形数转换成网络字节顺序dstclient_addr.sin_addr.s_addr = INADDR_ANY; //一个主机可能有多个网卡,每个网卡有自己的IP,设置为INADDR_ANY简化操作。//将指定socket绑定到本地地址与端口 ; (要绑定的socket, 本地地址信息强制转换 , 长度) Status = bind(ServerSock,(struct sockaddr far *)&dstclient_addr,sizeof(dstclient_addr));if (Status != 0)return FALSE;//将指定socket设置为监听模式; (指定的socket, 等待连接队列长度)Status = listen(ServerSock,1); if (Status != 0)return FALSE;char name[255]; PHOSTENT hostinfo; //PHOSTENT结构记录主机的信息,包括主机名、别名、地址类型、地址长度和地址列表。if( gethostname (name, sizeof(name)) == 0) //得到主机名的指针 { if((hostinfo = gethostbyname(name)) != NULL) //得到主机名字和地址信息的指针{ CString local_IP_address; local_IP_address = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list); //本地IP地址u_short port = ((dstclient_addr.sin_port & 0xFF00)>>8) | ((dstclient_addr.sin_port & 0x00FF)<<8); //端口号转换成u_shortCString temp;temp.Format("请连接这个IP地址:\n%s:%d",local_IP_address, port); //显示SetDlgItemText(FindWindow(NULL, WINDOWNHANDLERNAME), IDC_STATIC_IP, temp);} }return TRUE; //创建成功
}BOOL waiteAccept()
{//ACCEPTint len = sizeof(dstclient_addr);//接受客户端发送的连接请求 ; (已设为监听状态的服务端socket , 指向缓冲区的指针保存客户端的IP与端口信息 , 长度)//accept函数非常地痴情,痴心不改:如果没有客户端套接字去请求,它便会在那里一直痴痴地等下去,直到永远(阻塞式)。connectSock = accept(ServerSock,(struct sockaddr far *)&dstclient_addr,&len);if (connectSock < 0){closesocket(connectSock);return FALSE;}//GetSCREENint SysWidth = GetSystemMetrics(SM_CXSCREEN);int SysHeight = GetSystemMetrics(SM_CYSCREEN);return TRUE;
}DWORD WINAPI threadFunc(LPVOID threadNum) //线程函数
{int length; CString temp;if(!InItServerSock()) //创建服务端socketreturn 0;if(!waiteAccept()) //等待连接return 0;//等待连接的时候,连接可能被取消。if(ThreadRun){CString temp;temp.Format("%s:%d\n已经连接上!", inet_ntoa(dstclient_addr.sin_addr), dstclient_addr.sin_port);//remoteSockAddr->sa_dataSetDlgItemText(FindWindow(NULL, WINDOWNHANDLERNAME), IDC_STATIC_IP, temp);}else{SetDlgItemText(FindWindow(NULL, WINDOWNHANDLERNAME), IDC_STATIC_IP, "请启动服务!");return 0;}//send(NewSock,(char*)&FALG,sizeof(FALG)+1,MSG_OOB); while(ThreadRun){//length = recv(connectSock,(char*)recv_message,sizeof(recv_message)+1,0);length = recv(connectSock,(char*)recv_message_server,sizeof(recv_message_server),0);if( length>0){//MessageBox(0, recv_message, "接收到的信息", MB_OK); temp.Format("接收到的信息:\n%s",recv_message_server);CString opt ;opt.Format(_T("%s") , recv_message_server) ;if(opt == "A")AfxMessageBox(_T("关机!")) ;else if(opt == "B")AfxMessageBox(_T("开机!")) ;SetDlgItemText(FindWindow(NULL, WINDOWNHANDLERNAME), IDC_STATIC_IP, temp);memset(recv_message_server, 0, sizeof(recv_message_server));}}return 0;
}class CAboutDlg : public CDialogEx
{
public:CAboutDlg();// 对话框数据enum { IDD = IDD_ABOUTBOX };protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持// 实现
protected:DECLARE_MESSAGE_MAP()
};CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);
}BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()// CPCControlDlg 对话框CPCControlDlg::CPCControlDlg(CWnd* pParent /*=NULL*/): CDialogEx(CPCControlDlg::IDD, pParent)
{m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CPCControlDlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);
}BEGIN_MESSAGE_MAP(CPCControlDlg, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON_START, &CPCControlDlg::OnBnClickedButtonStart)ON_BN_CLICKED(IDC_BUTTON_SERVER, &CPCControlDlg::OnBnClickedButtonServer)
END_MESSAGE_MAP()// CPCControlDlg 消息处理程序BOOL CPCControlDlg::OnInitDialog()
{CDialogEx::OnInitDialog();// 将“关于...”菜单项添加到系统菜单中。// IDM_ABOUTBOX 必须在系统命令范围内。ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){BOOL bNameValid;CString strAboutMenu;bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);ASSERT(bNameValid);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动// 执行此操作SetIcon(m_hIcon, TRUE); // 设置大图标SetIcon(m_hIcon, FALSE); // 设置小图标// TODO: 在此添加额外的初始化代码return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}void CPCControlDlg::OnSysCommand(UINT nID, LPARAM lParam)
{if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialogEx::OnSysCommand(nID, lParam);}
}// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。void CPCControlDlg::OnPaint()
{if (IsIconic()){CPaintDC dc(this); // 用于绘制的设备上下文SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);// 使图标在工作区矩形中居中int cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// 绘制图标dc.DrawIcon(x, y, m_hIcon);}else{CDialogEx::OnPaint();}
}//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CPCControlDlg::OnQueryDragIcon()
{return static_cast<HCURSOR>(m_hIcon);
}void CPCControlDlg::OnBnClickedButtonStart()
{// TODO: 在此添加控件通知处理程序代码if(ThreadRun == false){ThreadRun = true;hThread=CreateThread(NULL, //创建线程0,threadFunc, //线程函数地址NULL,//传递的参数0,&ThreadID);SetDlgItemText(IDC_BUTTON_START, _T("停止服务"));}else{ ThreadRun = false;closesocket(connectSock);closesocket(ServerSock); SetDlgItemText(IDC_BUTTON_START, _T("开启服务"));}
}void CPCControlDlg::OnBnClickedButtonServer()
{// TODO: 在此添加控件通知处理程序代码GetDlgItemText(IDC_EDIT_SERVER,send_message,sizeof(send_message));send(connectSock,(char*)send_message, strlen(send_message) + 1 , MSG_OOB);
}
这篇关于VC网络协议的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!