首页
社区
课程
招聘
[求助]孙鑫多线程聊天程序
发表于: 2007-11-5 21:07 6335

[求助]孙鑫多线程聊天程序

2007-11-5 21:07
6335
孙鑫的MFC视频,有个例子是多线程聊天程序,我照视频一步一步打出来的,他的程序可运行,我的就不行。打开附带的例子也不行。
retval=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));
    if(SOCKET_ERROR==retval)
        {
                closesocket(m_socket);
                MessageBox("绑定失败");
                return FALSE;
        }
就在这段程序出错,
显示“绑定失败”

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (4)
雪    币: 1946
活跃值: (248)
能力值: (RANK:330 )
在线值:
发帖
回帖
粉丝
2
Startup first
2007-11-5 23:04
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
// 152chatDlg.cpp : implementation file
//

#include "stdafx.h"
#include "152chat.h"
#include "152chatDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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

/////////////////////////////////////////////////////////////////////////////
// CMy152chatDlg dialog

CMy152chatDlg::CMy152chatDlg(CWnd* pParent /*=NULL*/)
        : CDialog(CMy152chatDlg::IDD, pParent)
{
        //{{AFX_DATA_INIT(CMy152chatDlg)
                // NOTE: the ClassWizard will add member initialization here
        //}}AFX_DATA_INIT
        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy152chatDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CMy152chatDlg)
                // NOTE: the ClassWizard will add DDX and DDV calls here
        //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy152chatDlg, CDialog)
        //{{AFX_MSG_MAP(CMy152chatDlg)
        ON_WM_SYSCOMMAND()
        ON_WM_PAINT()
        ON_WM_QUERYDRAGICON()
        ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
        //}}AFX_MSG_MAP
        ON_MESSAGE(WM_RECVDATA,OnRecvData)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy152chatDlg message handlers

BOOL CMy152chatDlg::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
        InitSocket();
    RECVPARAM *pRecvParam=new RECVPARAM;
    pRecvParam->sock=m_socket;
        pRecvParam->hwnd=m_hWnd;
        HANDLE hThread=CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam,0,NULL);
        CloseHandle(hThread);

        return TRUE;  // return TRUE  unless you set the focus to a control
}

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

BOOL CMy152chatDlg::InitSocket()
{
    m_socket=socket(AF_INET,SOCK_DGRAM,0);
        if(INVALID_SOCKET==m_socket)
        {
                MessageBox("套接字创建失败!");
                return FALSE;
        }
        SOCKADDR_IN addrSock;
        addrSock.sin_family=AF_INET;
    addrSock.sin_port=htons(6000);
        addrSock.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

    int retval;
        retval=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));
    if(SOCKET_ERROR==retval)
        {
                closesocket(m_socket);
                MessageBox("绑定失败");
                return FALSE;
        }
        return TRUE;
}

DWORD WINAPI CMy152chatDlg::RecvProc(LPVOID lpParameter)
{
        SOCKET sock=((RECVPARAM*)lpParameter)->sock;
        HWND hwnd=((RECVPARAM*)lpParameter)->hwnd;

        SOCKADDR_IN addrFrom;
        int len=sizeof(SOCKADDR);

        char recvBuf[200];
        char tempBuf[300];
        int retval;
        while(TRUE)
        {
                retval=recvfrom(sock,recvBuf,200,0,(SOCKADDR*)&addrFrom,&len);
                if(SOCKET_ERROR==retval)break;
                sprintf(tempBuf,"%s说:%s",inet_ntoa(addrFrom.sin_addr),recvBuf);
                ::PostMessage(hwnd,WM_RECVDATA,0,(LPARAM)tempBuf);
        }

    return 0;
}

void CMy152chatDlg::OnRecvData(WPARAM wParam,LPARAM lParam)
{
        CString str=(char*)lParam;
        CString strTemp;
    GetDlgItemText(IDC_EDIT_RECV,strTemp);
        str+="\r\n";
        str+=strTemp;
        SetDlgItemText(IDC_EDIT_RECV,str);
}

void CMy152chatDlg::OnBtnSend()
{
        // TODO: Add your control notification handler code here
        DWORD dwIP;
        ((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP);
        SOCKADDR_IN addrTo;
        addrTo.sin_family=AF_INET;
    addrTo.sin_port=htons(6000);
        addrTo.sin_addr.S_un.S_addr=htonl(dwIP);

        CString strSend;
        GetDlgItemText(IDC_EDIT_SEND,strSend);
        sendto(m_socket,strSend,strSend.GetLength()+1,0,(SOCKADDR*)&addrTo,sizeof(SOCKADDR));
        SetDlgItemText(IDC_EDIT_SEND,"");
}
2007-11-7 14:08
0
雪    币: 330
活跃值: (112)
能力值: ( LV6,RANK:80 )
在线值:
发帖
回帖
粉丝
4
程序太长 懒得看了 你检查一下套接字版本,1。1还是2。2 还有就是检查以下是UDP还是TCP的
2007-11-10 03:33
0
雪    币: 2105
活跃值: (424)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
5
MS有可能端口被占用
2007-11-10 13:28
0
游客
登录 | 注册 方可回帖
返回
//