⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketclientdlg.cpp

📁 基于vc++mfc 最全的一个socket框架
💻 CPP
字号:
// SocketClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SocketClient.h"
#include "SocketClientDlg.h"
#include "string.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSocketClientDlg dialog

CSocketClientDlg::CSocketClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSocketClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSocketClientDlg)
	m_username = _T("");
	m_userID = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSocketClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSocketClientDlg)
	DDX_Text(pDX, IDC_EDIT1, m_username);
	DDX_Text(pDX, IDC_EDIT2, m_userID);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSocketClientDlg, CDialog)
//{{AFX_MSG_MAP(CSocketClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_ONSEND, OnButtonOnsend)
ON_MESSAGE(NETWORK_EVENT,OnNetEvent)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSocketClientDlg message handlers

BOOL CSocketClientDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	InitNetwork();
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSocketClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSocketClientDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		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;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSocketClientDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CSocketClientDlg::InitNetwork()
{
	
	status=WSAStartup(MAKEWORD(1, 1), &Data);
	if (status != 0)
	{
		MessageBox("ERROR: WSAStartup unsuccessful!","pht",MB_OK);
	}
    ClientSock=socket(AF_INET, SOCK_STREAM, 0);
	ClientAddr=inet_addr(DEST_IP_ADDR);		//暂时定为本机使用
	memcpy(&ClientSockAddr.sin_addr, &ClientAddr,sizeof(ClientAddr));
	ClientSockAddr.sin_port=htons(8888);
	ClientSockAddr.sin_family=AF_INET;
	
	if (ClientSock == INVALID_SOCKET)
	{
		MessageBox("ERROR: socket unsuccessful!","pht",MB_OK);
		status=WSACleanup();
		if (status == SOCKET_ERROR)
			MessageBox("ERROR: WSACleanup unsuccessful!","pht",MB_OK);
		return false;
	}
	MessageBox("成功");
	status=connect(ClientSock, (LPSOCKADDR) &ClientSockAddr,sizeof(ClientSockAddr));
	if (status == SOCKET_ERROR)
	{
		MessageBox("ERROR: llllt unsuccessful!","pht",MB_OK);
		status=closesocket(ClientSock);
		if (status == SOCKET_ERROR)
			MessageBox("ERROR: closesocket unsuccessful!","pht",MB_OK);
		status=WSACleanup();
		if (status == SOCKET_ERROR)
			MessageBox("ERROR: WSACleanup unsuccessful!","pht",MB_OK);
		
	}


	return TRUE;
	
}
void CSocketClientDlg::OnButtonOnsend() 
{
	
	UpdateData();
	if(m_username.IsEmpty())
	{
		MessageBox("请输入信息!","中华配货通",MB_OK);
	}
	else
		PHT();
	SetDlgItemText(IDC_EDIT1,"");
	SetDlgItemText(IDC_EDIT2,"");
}

BOOL CSocketClientDlg::PHT()
{
	
	
	char sendText[100];
	int numsnt,status;
	sprintf(sendText,"%s,%s",m_username,m_userID);
	numsnt=send(ClientSock, sendText, strlen(sendText) + 1, NO_FLAGS_SET);
	 
	if (numsnt!= (int)strlen(sendText)+1)
    {
		MessageBox("ERROR: Connection terminated!","中华配货通",MB_OK);
		status=closesocket( ClientSock);
		if (status == SOCKET_ERROR)
			MessageBox("ERROR: closesocket unsuccessful!","中华配货通",MB_OK);
		status=WSACleanup();
		if (status == SOCKET_ERROR)
			MessageBox("ERROR: WSACleanup unsuccessful!","中华配货通",MB_OK);
		
    }
		MessageBox("连接成功");
			if(WSAAsyncSelect(ClientSock, m_hWnd, NETWORK_EVENT,  FD_CLOSE | FD_READ ) == SOCKET_ERROR)
	{ 
		MessageBox("注册网络异步事件失败!"); 
		WSACleanup(); 
		return false; 
	} 
	MessageBox("异步成功");

	return true;	
}
void CSocketClientDlg::OnNetEvent(WPARAM wParam, LPARAM lParam)
{
	
    int iEvent = WSAGETSELECTEVENT(lParam); 
	//调用Winsock API函数,得到发生此事件的客户端套接字
	SOCKET CurSock= (SOCKET)wParam;
	switch(iEvent) 
	{ 
		
	case FD_READ: //网络数据包到达事件
		OnReceive( CurSock); 
		break; 

	
	case FD_CLOSE: //客户端断开事件
		OnClose( CurSock); 
		break; 
	default: break;
	}
}

void CSocketClientDlg::OnClose(SOCKET CurSock)
{
       
    WSACleanup();
	//结束与相应的客户端的通信,释放相应资源
}

void CSocketClientDlg::OnSend(SOCKET CurSock)
{
  
}

void CSocketClientDlg::OnReceive(SOCKET CurSock)
{
	char recvText[5025];
	int numrcv;
   	numrcv=recv(ClientSock, recvText, 5025, 0);
	if ((numrcv == 0) || (numrcv == SOCKET_ERROR))
    {
		MessageBox("ERROR: Connection terminated!","中华配货通",MB_OK);
		status=closesocket(ClientSock);
		if (status == SOCKET_ERROR)
			MessageBox("ERROR: closesocket unsuccessful!","中华配货通",MB_OK);
		status=WSACleanup();
		if (status == SOCKET_ERROR)
			MessageBox("ERROR: WSACleanup unsuccessful!","中华配货通",MB_OK);
		
    }
	recvText[numrcv]='\0';
	MessageBox(recvText);
	//////////////////////////////////////////////////////////////////////////////
	/////////////////////读取返回///////////////////////////////////
	//char Flag[10];
	
	if(recvText=="aaa")
	{
		MessageBox("欢迎登陆http://www.peihuo.com!","中华配货通",MB_OK);
	}
	///////////////////////////////////////////////////////////////////////////////
	///////////////////////将用户信息写入与用户名对应的文件////////////////////////
	else
	{
		MessageBox("对不起!请注册http://www.peihuo.com!","中华配货通",MB_OK);
		
	}
	//读出网络缓冲区中的数据包
	
}

void CSocketClientDlg::OnConnect(SOCKET CurSock)
{
	

	
	
}

void CSocketClientDlg::OnButton1() 
{

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -