呵呵,那就贴下代码,还有个问题,就是我里面的全局变量不知道用的对不对,帮忙一起看下啊~
// testDlg.cpp : implementation file
//
#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#include <WinSock2.h>
#pragma comment (lib,"Ws2_32.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int lcport=8914;
CString port;
CString hip;
SOCKET sk;
SOCKET ssk;//accept()函数接收的sock
sockaddr_in skadrin;
sockaddr_in rskadrin;//远程主机发送过来的sockaddr_in
WSADATA wsa;
/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
m_lcport = 8914;
m_hip = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
DDX_Text(pDX, IDC_lport, m_lcport);
DDX_LBString(pDX, IDC_hostlist, m_hip);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)//添加消息映射
ON_BN_CLICKED(IDC_save, Onsave)
ON_BN_CLICKED(IDC_close, Onclose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers
BOOL CTestDlg::OnInitDialog()//初始化
{
char *tip;
in_addr addr;
unsigned long aip;
CDialog::OnInitDialog();
((CEdit *)this->GetDlgItem(IDC_lport))->GetWindowText(port);
lcport=atol(port);
WSAStartup(MAKEWORD(2,0),&wsa);
sk=socket(AF_INET,SOCK_STREAM,0);
skadrin.sin_family=AF_INET;
skadrin.sin_port=htons(lcport);
skadrin.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
bind(sk,(sockaddr *)&skadrin,sizeof(sockaddr));
listen(sk,10);
int len=sizeof(rskadrin);
while(1)
{
ssk=accept(sk,(sockaddr *)&rskadrin,&len);
aip=rskadrin.sin_addr.S_un.S_addr;
memcpy(&addr.S_un.S_addr,&aip,sizeof(rskadrin.sin_addr.S_un.S_addr));
tip=inet_ntoa(addr);
hip=*tip+"\n";
m_hip=hip;//hostlist 与 edit控件关联的变量
UpdateData(false);
}
// 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 CTestDlg::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 CTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
#include "Resource.h"
void CTestDlg::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
::MessageBox(NULL,"test","test",MB_OK);
}
void CTestDlg::Onsave()
{
((CEdit *)this->GetDlgItem(IDC_lport))->GetWindowText(port);
lcport=atol(port);
m_lcport=lcport;//IDC_lport 控件关联的变量
}
void CTestDlg::Onclose()
{
closesocket(sk);
closesocket(ssk);
WSACleanup();
}