读书人

【新手】SOCKET中程序总在accept()函

发布时间: 2012-10-11 10:16:10 作者: rapoo

【新手求助】SOCKET中,程序总在accept()函数卡住
用accept函数就被卡住,不用就没事

为什么呢?谢谢各位!

源代码如下:
// ServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"


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

SOCKET m_hServerSocket;
SOCKET NewSock;
struct sockaddr_in dstclient_addr;
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog

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

void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CServerDlg)
DDX_Text(pDX, IDC_EVENT_READ, m_sRead);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
//{{AFX_MSG_MAP(CServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_START_CONNECT, OnStartConnect)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers

BOOL CServerDlg::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


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

void CServerDlg::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 CServerDlg::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 CServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//点击开始连接
void CServerDlg::OnStartConnect()
{
// TODO: Add your control notification handler code here
MyInitSock();

if(!AcceptData())
m_sRead+="连接失败";


}

//启动套接字,并设为监听
BOOL CServerDlg::MyInitSock()
{
int Status;
WSADATA WSAData;
WORD wVersionReqd=MAKEWORD(1,1);
Status= WSAStartup(wVersionReqd,&WSAData);
m_sRead+="套接字初始化成功!\n";
UpdateData(FALSE);
if(Status!=0)
{
return FALSE;
}
m_hServerSocket=socket(AF_INET,SOCK_STREAM,0);
m_sRead+="套接字创建成功!\n";
UpdateData(FALSE);

//设定本机端口和IP
dstclient_addr.sin_family=PF_INET;
dstclient_addr.sin_port=htons(4096);
dstclient_addr.sin_addr.S_un.S_addr=INADDR_ANY;

//绑定
Status=bind(m_hServerSocket,(struct sockaddr far*)&dstclient_addr,sizeof(dstclient_addr));

if(Status!=0)
{
return FALSE;
}

//设为监听
Status=listen(m_hServerSocket,1);
if(Status!=0)
{
return FALSE;
}
return TRUE;
}
//接受数据
BOOL CServerDlg::AcceptData()
{
int len = sizeof(dstclient_addr);
//接受新连接
NewSock=accept(m_hServerSocket,(struct sockaddr far*)&dstclient_addr,&len);
if(NewSock<0)
{
closesocket(m_hServerSocket);
return FALSE;
}
m_sRead+="连接成功!";

//获得屏幕的分辨率
SysWidth = GetSystemMetrics(SM_CXSCREEN);
SysHeight = GetSystemMetrics(SM_CYSCREEN);
m_sRead+="已获得屏幕分辨率";
closesocket(m_hServerSocket);
closesocket(NewSock);
WSACleanup();
return TRUE;
}

[解决办法]
程序阻塞,界面卡在那里会造成无响应~
新建个线程用来监听,或者设置成异步模式
[解决办法]
函数阻塞
你点击开始连接MyInitSock()函数在Accept阻塞。。
可以开一个线程去解决吧
在OnStartConnect() HANDLE hThread = CreateThread( );在线程函数里面 写入MyInitSock里面的代码

[解决办法]
Accept()一般不放在主线程中,而另开一线程来监听;

读书人网 >C++

热点推荐