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

📄 clientdlg.cpp

📁 包括了基于网络芯片8019的单片机源程序和pc端的网络程序(VC++6.0)
💻 CPP
字号:
// ClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "testnet.h"
#include "ClientDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog

#include "winsock.h"
#pragma comment(lib, "wsock32")

CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CClientDlg)
	m_nServerIP = _T("");
	m_nConIP = 0;
	m_nConPort = 0;
	m_nClientRecv = _T("");
	m_nCSend = _T("");
	//}}AFX_DATA_INIT
}


void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClientDlg)
	DDX_Text(pDX, IDC_SERVERIP, m_nServerIP);
	DDX_Text(pDX, IDC_CONNECTIP, m_nConIP);
	DDX_Text(pDX, IDC_CONPORT, m_nConPort);
	DDX_Text(pDX, IDC_EDITRECV, m_nClientRecv);
	DDX_Text(pDX, IDC_EDIT3, m_nCSend);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
	//{{AFX_MSG_MAP(CClientDlg)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_BN_CLICKED(IDC_CRECEIVE, OnCreceive)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_UDP, OnUdp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers

void CClientDlg::OnOK() 
{
	

}

void CClientDlg::OnConnect() 
{

	if(m_hSocket != NULL){
		closesocket(m_hSocket);
		m_hSocket = NULL;
	}
	if(m_hSocket == NULL){
		m_hSocket = socket(AF_INET, SOCK_STREAM,0);
		ASSERT(m_hSocket != NULL);
	}
	UpdateData();
    
	m_addr.sin_family = AF_INET;
	m_addr.sin_addr.S_un.S_addr = inet_addr("202.119.101.8");
	m_addr.sin_port = htons(m_nConPort);

	int ret = 0;
	int error = 0;
	ret = connect(m_hSocket, (LPSOCKADDR)&m_addr, sizeof(m_addr));
	if(ret == SOCKET_ERROR){
		TRACE("Connect Error: %d \n", (error = WSAGetLastError()));
		if(error == 10061)
			AfxMessageBox(_T("请确认服务器确实已经打开并工作在同样的端口!"));
		return ;
	}
	else 
		AfxMessageBox("Connected!");
	
	
}

void CClientDlg::OnSend() //发送文本框里的字符
{
	UpdateData();
	char* buf = m_nCSend.GetBuffer(0);
	int ret=0;
	ret = send(m_hSocket, buf, m_nCSend.GetLength(), 0);
	if(ret != m_nCSend.GetLength()){
		TRACE("Send data error: %d\n", WSAGetLastError());
		return ;
	}	
	else
	{
		MessageBox("Send Success!");
	}
}

void CClientDlg::OnCreceive() //接收发过来的数据
{
	TIMEVAL tv01 = {0, 1};//1ms钟延迟,实际为0-10毫秒 
	int nSelectRet; 
	int nErrorCode; 
	FD_SET fdr = {1, m_hSocket}; 
	nSelectRet=::select(0, &fdr, NULL, NULL, &tv01);//检查可读状态 

	if(SOCKET_ERROR==nSelectRet) 
	{ 
	nErrorCode=WSAGetLastError(); 
	TRACE("select read status errorcode=%d",nErrorCode); 
	MessageBox("Socket ERROR");
	::closesocket(m_hSocket); 
	} 
	if(nSelectRet==0)//超时发生,无可读数据 
	{ 
	MessageBox("NoData");
	} 
	else 
	{ 
	int error=0;
	int ret=0;
	char buf[256];
	ret = recv(m_hSocket, buf, 256, 0);
		if(ret == 0 || ret == SOCKET_ERROR ){
		TRACE("Recv data error: %d\n", WSAGetLastError());
		return ;
	}
	buf[ret]='\0';

	CString str(buf);
    m_nClientRecv=str;
	UpdateData(FALSE);

//	MessageBox(str);
	}  

}

void CClientDlg::OnClose() 
{
  if(m_hSocket != NULL){
		closesocket(m_hSocket);
		m_hSocket = NULL;
	}
	CDialog::OnCancel();	
	CDialog::OnClose();
}

void CClientDlg::OnUdp() 
{
	if(m_hSocket != NULL){
		closesocket(m_hSocket);
		m_hSocket = NULL;
	}
	if(m_hSocket == NULL){
		if((m_hSocket = socket(AF_INET, SOCK_DGRAM,0))==INVALID_SOCKET)
		{
			MessageBox("dfsdf");
		}
		
	}
	char* buf = m_nCSend.GetBuffer(0);
	UpdateData();
    
	/*m_addr.sin_family = AF_INET;
	m_addr.sin_addr.S_un.S_addr = inet_addr("169.254.165.10");
	m_addr.sin_port = htons(1025);

    if(bind(m_hSocket,(LPSOCKADDR)&m_addr,sizeof(m_addr)))
	{
		MessageBox("dfsdf1");
		closesocket(m_hSocket);
		return;
	}*/
	SOCKADDR_IN to;
	to.sin_family = AF_INET;
	to.sin_addr.S_un.S_addr = inet_addr("169.254.165.11");
	to.sin_port = htons(1025);
	int iError=sendto(m_hSocket,buf,m_nCSend.GetLength(),0,(LPSOCKADDR)&to,sizeof(to));
	if(iError==SOCKET_ERROR){
		MessageBox("dfsdf2");
		closesocket(m_hSocket);
		return;
	}
		AfxMessageBox("ok!");
		
}

⌨️ 快捷键说明

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