监听线程:
CSocket re;
CSocket listen_;
CSssssssssssssDlg *dlg=(CSssssssssssssDlg*)pParam;
listen_.Create (5000);
listen_.Listen ();
listen_.Accept(re);
listen_ .Close ();
AfxBeginThread(lisen,pParam,0,0,0,0);

re.Receive(dlg->temp,100);
dlg->str_re +=dlg->temp;
CEdit *e=(CEdit*)(dlg->GetDlgItem (IDC_EDIT2));
e->SetWindowText (dlg->temp);
re.Close ();
发送函数;
ss.Create ();
CIPAddressCtrl *ip=(CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1);
ip->GetWindowText (str_target_ip,10);
if(0!=ss.Connect(str_target_ip,5000))
{
GetDlgItem(IDC_EDIT1)->GetWindowText(str_text,200);
ss.Send( str_text,200);
GetDlgItem(IDC_EDIT1)->SetWindowText("Sent");
}
else
{
MessageBox("can not connect");
return;
}
ss.Close ();
对话框初始化:
WSAData da;
WSAStartup(0x0020,&da); CIPAddressCtrl *ip=(CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1);
char local_name[100];
gethostname(local_name,100);
hostent *host=gethostbyname(local_name);
IN_ADDR local_ip;
memmove((char*)&local_ip,host->h_addr_list [0],4);
char *str_ip=inet_ntoa(local_ip);
ip->SetWindowText (str_ip);

解决方案 »

  1.   

    对话框还有一行;
    AfxBeginThread(lisen,this,0,0,0,0);lisen就是监听线程。
      

  2.   

    是么?这我也不太清楚了。
    不过我觉得:
    1,你这种写法是不对的。不bind那你服务端就不能回复消息给客户端。
    2,可能是操作系统的问题,对于在局域网内没有bind也可以随意收数据。
    胡说一气了,谁能来解释一下?
      

  3.   

    If the socket is unbound, unique values are assigned to the local association by the system, and the socket is then ed as bound. An application can use getsockname to determine the local socket name in this case.