#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested
WSADATA wsaData
int err
wVersionRequested = MAKEWORD( 1, 1 )
//初始化winsocket
err = WSAStartup( wVersionRequested, &wsaData )
if ( err != 0 ) {
return
}
如果本地windows系统地socket库版本号不是1.1,则退出
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( )
return
}
//创建socket服务,使用udp协议
SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,0)
SOCKADDR_IN addrSrv
//本地服务地址为192.168.1.107,端口号为6000
addrSrv.sin_addr.S_un.S_addr=inet_addr("192.168.1.107")
addrSrv.sin_family=AF_INET
addrSrv.sin_port=htons(6000)
char recvBuf[100]//接收数据包
char sendBuf[100]//发送数据包
char tempBuf[200]//临时存储区
int len=sizeof(SOCKADDR)
while(1)
{
printf("Please input data:\n")
gets(sendBuf)
//向客户端提交字符串数据包
sendto(sockClient,sendBuf,strlen(sendBuf)+1,0,
(SOCKADDR*)&addrSrv,len)
//从客户端接收应答消息
recvfrom(sockClient,recvBuf,100,0,(SOCKADDR*)&addrSrv,&len)
//如果接收的应答字符串首位字符为“q”,则告知对方对话结束
if('q'==recvBuf[0])
{
sendto(sockClient,"q",strlen("q")+1,0,
(SOCKADDR*)&addrSrv,len)
printf("Chat end!\n")
break
}
//打印出对方聊天的ip和聊天内容
sprintf(tempBuf,"%s say : %s",inet_ntoa(addrSrv.sin_addr),recvBuf)
printf("%s\n",tempBuf)
}
//关闭socket服务
closesocket(sockClient)
WSACleanup()
}
首先,新建对话框,拖拽几个控件,包括2个输入框,"登录"和"取消"按钮,下边是一个登陆框的例子。#if !defined(AFX_LOGINDLG_H__5FFFF6A5_8CF5_411A_9796_A597EB8FE9A4__INCLUDED_)#define AFX_LOGINDLG_H__5FFFF6A5_8CF5_411A_9796_A597EB8FE9A4__INCLUDED_#if _MSC_VER >1000
#pragma once
#endif // _MSC_VER >1000
// LoginDlg.h : header file
//
#include "BtnST.h"
/////////////////////////////////////////////////////////////////////////////
// CLoginDlg dialog
typedef struct _tagUser
{
char name[18]
int oper
char password[16]
}USERINFOclass CLoginDlg : public CDialog
{
// Construction
public:
CLoginDlg(CWnd* pParent = NULL) // standard constructor
DWORD GetUserInfo()constCButtonST m_btnButton2
CButtonST m_btnButton3// Dialog Data
//{{AFX_DATA(CLoginDlg)<br>enum { IDD = IDD_LOGIN }
// NOTE: the ClassWizard will add data members here
CComboBox m_uList
CString m_pas
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CLoginDlg)<br>protected:<br>virtual void DoDataExchange(CDataExchange* pDX) // DDX/DDV support<br>//}}AFX_VIRTUAL// Implementation
protected:
virtual BOOL OnInitDialog()
virtual void OnOK()// Generated message map functions
//{{AFX_MSG(CLoginDlg)<br> // NOTE: the ClassWizard will add member functions here<br>DWORD m_dwUser<br>DWORD m_times<br>//}}AFX_MSG
DECLARE_MESSAGE_MAP()
}//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_LOGINDLG_H__5FFFF6A5_8CF5_411A_9796_A597EB8FE9A4__INCLUDED_)
CPP文件 // LoginDlg.cpp : implementation file
//#include "stdafx.h"
#include "SuperWords.h"
#include "LoginDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__
#endif/////////////////////////////////////////////////////////////////////////////
// CLoginDlg dialog
CLoginDlg::CLoginDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLoginDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLoginDlg)<br> // NOTE: the ClassWizard will add member initialization here<br> m_times = 2<br>m_dwUser = 0<br>m_pas = _T("")<br>//}}AFX_DATA_INIT
}
void CLoginDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX)
//{{AFX_DATA_MAP(CLoginDlg)<br> // NOTE: the ClassWizard will add DDX and DDV calls here<br>DDX_Control(pDX, IDC_DLOG_CUSER, m_uList)<br>DDX_Text(pDX, IDC_DLOG_CPASWORD, m_pas)<br>DDV_MaxChars(pDX, m_pas, 16)<br>//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLoginDlg, CDialog)
//{{AFX_MSG_MAP(CLoginDlg)<br> // NOTE: the ClassWizard will add message map macros here<br>//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CLoginDlg message handlers
DWORD CLoginDlg::GetUserInfo() const
{
return m_dwUser
}
BOOL CLoginDlg::OnInitDialog()
{
CDialog::OnInitDialog()
USERINFO theUser
char * strOper[] = { {"信息录入员"},{"系统管理员"} }
UINT size = sizeof(USERINFO)
LPBYTE bt = new unsigned char[sizeof(theUser)]
CString strID
strID.Format("User%04u",0)
int lastID
lastID = AfxGetApp()->GetProfileInt("用户","LastID",0)
for(int i=0i<lastID &&AfxGetApp()->GetProfileBinary("用户",strID,&bt,&size)i++)
{
memcpy(&theUser,bt,sizeof(theUser))
CString strInfo
strInfo.Format("%s [%s]",theUser.name,strOper[theUser.oper])
m_uList.AddString(strInfo)
strID.Format("User%04u",i+1)
}
if(lastID >0)
m_uList.SetCurSel(0)
delete [] btm_btnButton2.SubclassDlgItem(IDOK, this)
m_btnButton2.SetIcon(IDI_ICON1)
m_btnButton3.SubclassDlgItem(IDCANCEL, this)
m_btnButton3.SetIcon(IDI_ICON2)
return TRUE
}
void CLoginDlg::OnOK()
{
UpdateData()
USERINFO theUser
UINT size = sizeof(theUser)
LPBYTE bt = new unsigned char[sizeof(theUser)]
CString strID
int id = m_uList.GetCurSel()
strID.Format("User%04u",id)
if(AfxGetApp()->GetProfileBinary("用户",strID,&bt,&size))
{
memcpy(&theUser,bt,sizeof(theUser))
delete [] bt
if(!m_pas.Compare(theUser.password))
{
m_dwUser = theUser.oper
CDialog::OnOK()
return
}
}
else
{
delete [] bt
MessageBox("没有该用户的信息,请与系统管理员联系。","读取用户数据错误",MB_OK|MB_ICONSTOP)
return
}
if(m_times)
{
m_times--
MessageBox("用户密码不正确!请再次尝试...","登陆失败...",MB_OK|MB_ICONSTOP)
GetDlgItem(IDC_DLOG_CPASWORD)->SetFocus()
}
else
{
MessageBox("用户密码不正确!\n您的登陆失败次数已达三次,程序将退出。\n请与系统管理员联系。","登陆失败...",MB_OK|MB_ICONSTOP)
CDialog::OnCancel()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)