18,356
社区成员
发帖
与我相关
我的任务
分享
BOOL CSocketTestDlg::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
sock = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addr_in;
addr_in.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addr_in.sin_family = AF_INET;
addr_in.sin_port = 6001;
int length = sizeof(addr_in);
if(INVALID_SOCKET == sock)
{
m_edit.SetWindowTextA(_T("create socket failed!"));
}
if(bind(sock,(LPSOCKADDR)&addr_in,length))
{
m_edit.SetWindowTextA(_T("bind failed"));
closesocket(sock);
}
if(SOCKET_ERROR == listen(sock,5))
{
m_edit.SetWindowTextA(_T("Error listening!"));
}
else
m_list.InsertItem(listNum,"listening......");
return TRUE; // return TRUE unless you set the focus to a control
}
void CSocketTestDlg::CreateSocket()
{
//sockcnnNum++;
int len=sizeof(SOCKADDR);
while(1)
{
sockcnn = accept(sock,(SOCKADDR*)&addrClient,&len);
if(INVALID_SOCKET != sockcnn)
{
m_list.InsertItem(listNum++,"one client has been connected!");
AfxBeginThread(ListenProc,this);
//sockcnnNum++;
}
}
}
UINT CSocketTestDlg::ListenProc(LPVOID pParam)
{
CSocketTestDlg * p=(CSocketTestDlg*)pParam;
char bufMessage[300];
while(1)
{
CString str;
int relt;
if(-1 != (relt=recv(p->sockcnn,bufMessage,sizeof(bufMessage)-1,0)))//注意:少接受一个字符,留给0结束符
{
bufMessage[relt]='\0';
str+=bufMessage;
if(str.GetLength() == 0)
{
break;
}
int i=0;
//p->m_edit.SetWindowText(str);
p->m_list.InsertItem(p->listNum++,str);
/*SetDlgItemText(0,str);*/
bufMessage[0]='\0';
str = "";
//p->m_ctrText.LineScroll(p->m_ctrText.GetLineCount());
//p->UpdateData(FALSE);
}
}
return 1;
}