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

📄 clientbysocketdlg.cpp

📁 自己编写的SOCKET程序 源码有详尽的注释 可以用来学习C/S程序的编写方法
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*
 *	Copyleft Abelworkshop.	
 *	文件名称:ClientBySocketDlg.cpp
 *	摘    要:SOCKET客户端程序,用来与服务器端进行通信
 *	单    位:信息学院
 *	作    者:罗涛,2120060434
 *	完成日期:2006年11月21日
 */

#include "stdafx.h"
#include "socket.h"
#include "ClientBySocket.h"
#include "ClientBySocketDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
uchar gettime = 0;
uchar getudp = 0;
uchar gotudp = 0;
uchar connected = 0;
DWORD serv_ip;
int try_conn = 0;
cal_T sCal_T;

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()

/////////////////////////////////////////////////////////////////////////////
// CClientBySocketDlg dialog

CClientBySocketDlg::CClientBySocketDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CClientBySocketDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CClientBySocketDlg)
	m_udptxt = _T("");
	m_catlog_txt = _T("");
	m_conn_to_port = 0;
	m_curlog = _T("");
	m_curstat = _T("");
	m_expression = _T("");
	m_result = _T("");
	sCal_expression = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CClientBySocketDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClientBySocketDlg)
	DDX_Control(pDX, IDC_OPLIST, m_oplist);
	DDX_Control(pDX, IDC_IPCONN, m_ipconn);
	DDX_Control(pDX, IDC_CATLOG, m_catlog);
	DDX_Control(pDX, IDC_IPLIST, m_iplist);
	DDX_Text(pDX, IDC_UDPTXT, m_udptxt);
	DDV_MaxChars(pDX, m_udptxt, 100);
	DDX_Text(pDX, IDC_CATLOG, m_catlog_txt);
	DDX_Text(pDX, IDC_PORT, m_conn_to_port);
	DDX_Text(pDX, IDC_CURLOG, m_curlog);
	DDX_Text(pDX, IDC_CURSTAT, m_curstat);
	DDX_Text(pDX, IDC_TCP_EXPRESSION, m_expression);
	DDX_Text(pDX, IDC_TCP_RESULT, m_result);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CClientBySocketDlg, CDialog)
	//{{AFX_MSG_MAP(CClientBySocketDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_UDPECHO, On_UDPECHO)
	ON_BN_CLICKED(IDC_TCPTIME, On_TCPTIME)
	//ON_BN_CLICKED(IDC_UDPSEND, On_UDP_SEND_CLK)
	ON_BN_CLICKED(ID_CONN, On_CONN)
	ON_BN_CLICKED(IDC_DISCONN, On_Disconn)
	ON_BN_CLICKED(IDCLOSE, On_Close)
	ON_BN_CLICKED(IDC_BUTTON1, OnOperation)
	ON_MESSAGE(NETWORK_EVENT, OnNetEvent)
	ON_MESSAGE(NETWORK_LOG, OnLogDisp)
	ON_BN_CLICKED(IDC_TCPSEND, OnTcpsend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientBySocketDlg message handlers

BOOL CClientBySocketDlg::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
	
	// TODO: Add extra initialization here
	rmot_udp_addr_len = sizeof(rmot_udp_addr);
	memset(&rmot_udp_addr, 0, rmot_udp_addr_len);
	nSele = 0;
	InitNetwork();//初始化网络
	while (pHost->h_addr_list[nSele++]) {
		m_iplist.AddString(ipTostr(*(ulint*)(pHost->h_addr_list[nSele - 1])));
	}
	m_iplist.SetCurSel(0);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CClientBySocketDlg::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 CClientBySocketDlg::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 CClientBySocketDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CClientBySocketDlg::On_UDPECHO() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_UDPTXT)->EnableWindow(TRUE);
	GetDlgItem(IDC_UDPSEND)->EnableWindow(TRUE);
}

void CClientBySocketDlg::On_TCPTIME() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_UDPTXT)->EnableWindow(FALSE);
	GetDlgItem(IDC_UDPSEND)->EnableWindow(FALSE);
	//UpdateData(TRUE);											//取界面数据回变量
	//m_catlog.SetSel(m_catlog_txt.GetLength(), m_catlog_txt.GetLength(), 0);//光标选择最尾
	//m_catlog.ReplaceSel("获得时间\r\n");
	//MessageBox("服务器时间是:08:35:55", "TCPTIME", MB_OK);
	//recv(Sock, rubsh, sizeof(rubsh), 0);//为保证兼容性清空SOCKET流剩余缓冲,因为只用了2个字节,20061122
	SendMessage(NETWORK_LOG, LOG_send_itm, LPARAM(getTIME));//写日志
	gettime = 1;
	send(Sock, getTIME, strlen(getTIME) + 1, 0);
}

BOOL CClientBySocketDlg::InitNetwork()
{
	if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0){
		MessageBox("无法初始化SOCKET!", "SOCKET", MB_OK);
		SendMessage(WM_CLOSE);
	}
	//初始化 Raw Socket 
	if ((Sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR){
		MessageBox("建立SOCKET出错!", "SOCKET", MB_OK);
		closesocket(Sock);
		WSACleanup();
		SendMessage(WM_CLOSE);
	}
	closesocket(Sock);//在初始化时测试socket是否能正常建立:)
	//设置IP头操作选项,其FLAG设置为TRUE,亲自对IP进行处理
	//if (setsockopt(sockRaw, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag)) == SOCKET_ERROR){
	//	;//MessageBox("setsockopt IP_HDRINCL error!", "RAWSOCKET", MB_OK);
	//}
	//获取本机名 
	if (gethostname((char*)LocalName, sizeof(LocalName)) == SOCKET_ERROR){
		MessageBox("无法取得本机名!", "SOCKET", MB_OK);
		SendMessage(WM_CLOSE);
	}
	//获取本地 IP 地址 
	if ((pHost = gethostbyname((char*)LocalName)) == NULL){
		MessageBox("无法取得IP地址!", "SOCKET", MB_OK);
		SendMessage(WM_CLOSE);
	}
	return TRUE;
}

void CClientBySocketDlg::On_UDP_SEND_CLK() 
{
	// TODO: Add your control notification handler code here
	//GetDlgItem(IDC_UDPTXT)->EnableWindow(FALSE);
	//GetDlgItem(IDC_UDPSEND)->EnableWindow(FALSE);
	UpdateData(TRUE);//取回界面内容回内部变量
	//MessageBox(m_udptxt, "UDP", MB_OK);
	SendMessage(NETWORK_LOG, LOG_send_str, LPARAM(&m_udptxt));//写日志
	sendto(Sock_U, m_udptxt, strlen(m_udptxt), 0, (PSOCKADDR)&serv_udp_addr, sizeof(serv_udp_addr));
}

void CClientBySocketDlg::On_CONN() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);											//取界面数据回变量
	gettime = 0;
	getudp = 0;
	gotudp = 0;
	connected = 0;
	try_conn = MAX_TRY;
	GetDlgItem(ID_CONN)->EnableWindow(FALSE);
	GetDlgItem(IDC_IPLIST)->EnableWindow(FALSE);
	GetDlgItem(IDC_IPCONN)->EnableWindow(FALSE);
	GetDlgItem(IDC_PORT)->EnableWindow(FALSE);
	
	Sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	nSele = m_iplist.GetCurSel();
	addr_in.sin_addr  = *(in_addr*)pHost->h_addr_list[nSele]; //IP
	ulSeleIP = *(ulint*)(pHost->h_addr_list[nSele]);
	addr_in.sin_family = AF_INET; 
	addr_in.sin_port  = 0;							//对客户端,一定要让系统自动分配空闲端口,尽管这样也可能会冲突20061121
	if (bind(Sock, (PSOCKADDR)&addr_in, sizeof(addr_in)) == SOCKET_ERROR){ 

⌨️ 快捷键说明

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