| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 999 人关注过本帖
标题:【求助,急】对话框编程+sock问题
取消只看楼主 加入收藏
gpp6025
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2010-7-8
结帖率:84.62%
收藏
已结贴  问题点数:20 回复次数:5 
【求助,急】对话框编程+sock问题
写了个小程序,用的是对话框编程功能上用到了sock,编译什么的也过了,就是最后运行的时候,出问题了。
界面一开始跳出来也很正常,然后开启端口,防火墙跳出来阻止还是解除阻止的询问也来了,解除阻止后,界面就消失了(),看了下任务管理器,还有看了下本地开启的端口显示程序正在监听连接。很郁闷,为什么会出现这种情况啊??
路过的各位大大们,请不吝赐教啊。~~~急啊~~~
搜索更多相关主题的帖子: 对话框 sock 
2010-07-12 13:23
gpp6025
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2010-7-8
收藏
得分:0 
呵呵,那就贴下代码,还有个问题,就是我里面的全局变量不知道用的对不对,帮忙一起看下啊~
// 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();
}
2010-07-12 15:09
gpp6025
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2010-7-8
收藏
得分:0 
回复 2楼 东海一鱼
呵呵,那就贴下代码,还有个问题,就是我里面的全局变量不知道用的对不对,帮忙一起看下啊~
// 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();
}
2010-07-12 15:09
gpp6025
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2010-7-8
收藏
得分:0 
回复 5楼 东海一鱼
~~晕~~,谢谢了。
那这里面的这个监听是不是放到他界面画完之后,在进行啊?那这样的话,具体实现是不是要在这个线程外,或者给这个while()里面判断一个界面是否绘完的标志?还是要怎么写啊,大哥给点提示吧~~

[ 本帖最后由 gpp6025 于 2010-7-12 16:46 编辑 ]
2010-07-12 16:38
gpp6025
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2010-7-8
收藏
得分:0 
回复 7楼 东海一鱼
谢谢,~~小菜没学多久,以后还望大大多多帮助
2010-07-13 07:34
gpp6025
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2010-7-8
收藏
得分:0 
回复 8楼 hao0716
谢了,~~小菜没学多久,以后还望多多帮助,
2010-07-13 07:35
快速回复:【求助,急】对话框编程+sock问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029943 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved