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

📄 senddlg.cpp

📁 tcp、winsock编程
💻 CPP
字号:
// sendDlg.cpp : implementation file
//

#include "stdafx.h"
#include "send.h"
#include "sendDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSendDlg dialog

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

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

BEGIN_MESSAGE_MAP(CSendDlg, CDialog)
	//{{AFX_MSG_MAP(CSendDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSendDlg message handlers

BOOL CSendDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CSendDlg::OnButton1() 
{
	//Get The ComputerName And UserName
	LPTSTR SendStr;
	SendStr = new char[48];
	SendStr = "";
	LPTSTR lpBuffer;
	int nSize = 22;
	int ok;
	lpBuffer = new char[24];
	ok = GetUserName(lpBuffer,(LPDWORD)&nSize); 
	if (!ok){
		ErrorMessage("获取用户名错误");
		return;
	}
	strcpy(SendStr,lpBuffer);
	m_username = lpBuffer;
	ok = GetComputerName(lpBuffer,(LPDWORD)&nSize); 
	if (!ok){
		ErrorMessage("获取计算机名错误");
		return;
	}
	m_computername = lpBuffer;
	strcat(SendStr,lpBuffer);
	UpdateData(false);
	delete lpBuffer;

	//Use Socket to Send The UserName and ComputerName
	WORD wVersionRequested;
	WSAData  wsaData;
	struct hostent *hp;
	struct sockaddr_in client;
	unsigned long ip_addr;
	int sendlen;
	int s;
	int err; 
	wVersionRequested = MAKEWORD( 2, 0 ); 
	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) { 
		ErrorMessage("Socket0.0版本错误");
		return;
	}
	if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 ) {
		ErrorMessage("Socket1.0版本错误");
		WSACleanup( );
		return; 
	}	

    ip_addr=inet_addr("192.168.0.62");
    hp=gethostbyaddr((char *)&ip_addr,4,PF_INET);
    if(hp==NULL){
		ErrorMessage("获取IP地址信息错误");
		return;
    }
	
	memcpy(&client.sin_addr,hp->h_addr,hp->h_length);
	if((s=socket(AF_INET,SOCK_STREAM,0))<0){
		ErrorMessage("创建Socket错误");
		return;
	}
	client.sin_family=AF_INET;
	client.sin_port=htons(1024);
	if(connect(s,(struct sockaddr*)&client,sizeof(client))<0){
		closesocket(s);
		ErrorMessage("Socket连接错误");
		return;
	}
	if((sendlen=send(s,SendStr,48,0))<=0){
		ErrorMessage("Socket发送错误");
		return;
	}

	WSACleanup( );
	AfxMessageBox("发送完成!",MB_OK,0);
	return;
}

void CSendDlg::ErrorMessage(CString Msg) 
{
	LPVOID lpMsgBuf;
	FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
		NULL,
		GetLastError(),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &lpMsgBuf,
		0,
		NULL 
		);
	MessageBox((LPCTSTR )lpMsgBuf, Msg, MB_OK);
	LocalFree( lpMsgBuf );
	WSACleanup( );
}

⌨️ 快捷键说明

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