小弟新学网络编程 简单的tcp连接 ,聊天软件,当点允许连接时,服务器软件就死机,怎么回事啊

youkuxiaobin 2010-05-09 03:17:57

BOOL CSRVDlg::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
SetTimer(1,1000,NULL);
// TODO: Add extra initialization here
//初始化
WORD wVersionRequest;
WSADATA ws;
wVersionRequest=MAKEWORD(1,1);

int err=WSAStartup(wVersionRequest,&ws);
if(err!=0)
return true;
if(LOBYTE(ws.wVersion)!=1||HIBYTE(ws.wVersion)!=1)
{
WSACleanup();
return true;
}
srvListener=socket(AF_INET,SOCK_STREAM,0);

ad.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
ad.sin_family=AF_INET;
ad.sin_port=htons(6000);
bind(srvListener,(SOCKADDR*)&ad,sizeof(SOCKADDR));
len=sizeof(SOCKADDR);
listen(srvListener,5);
return TRUE; // return TRUE unless you set the focus to a control
}

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

void CSRVDlg::OnButton2() //就是当点允许连接是就没响应,OnButton2()就是允许连接的处理函数{
// TODO: Add your control notification handler code here
while(1)
{
srvUse=accept(srvListener,(SOCKADDR*)&use,&len);
if(srvUse!=INVALID_SOCKET )
{
return;
}
}
}

void CSRVDlg::OnButton1() //这个事发送按钮的处理函数
{
// TODO: Add your control notification handler code here
char *a=NULL;
GetDlgItemText(IDC_EDIT2,a,1000);
send(srvUse,a,1000,0);
}

void CSRVDlg::OnTimer(UINT nIDEvent)//这个是服务器收到的处理函数,我用定时器来处理的
{
// TODO: Add your message handler code here and/or call default
char *b=NULL;
recv(srvUse,b,1000,0);
SetDlgItemText(IDC_EDIT3,b);
CDialog::OnTimer(nIDEvent);
}
...全文
109 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
AIHANZI 2010-05-09
  • 打赏
  • 举报
回复
listen,accept,recv这些函数都是阻塞的,你可以开启一个新线程去解决这个问题。。
lijianli9 2010-05-09
  • 打赏
  • 举报
回复
accept是阻塞型的,你要再开辟一个工作线程,然后在里面accept。要不你的程序会卡到你的btn1函数中的,
haihaihai627 2010-05-09
  • 打赏
  • 举报
回复
本人菜鸟,用的是UDP,也遇到同样问题,不知道那个不断监听的循环放在哪里?老是卡住了...
Eleven 2010-05-09
  • 打赏
  • 举报
回复
还有lz把accept放在主线程里,这样这界面会很卡的。。。最好放在线程里,或者用非阻塞socket
Eleven 2010-05-09
  • 打赏
  • 举报
回复
同上,你的OnTimer函数中:
char *b=NULL;
recv(srvUse,b,1000,0);
SetDlgItemText(IDC_EDIT3,b);
这个也没有分配内存空间。。。
arong1234 2010-05-09
  • 打赏
  • 举报
回复
char *a=NULL;
GetDlgItemText(IDC_EDIT2,a,1000);
a还没分配内存!!!这是C++指针一节必然会强调的吧

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧