Mfc网络编程
Udp
//server
CSocket t;
t.Create(2000,SOCK_DGRAM,"127.0.0.1");
t.Bind(2000,"127.0.0.1");
char buf[1024]="hi1";
t.SendTo(buf,strlen(buf),2000,"127.0.0.1",0);
//client
CString add="127.0.0.1";
UINT port=2000;
::AfxMessageBox(buf);
int len=t.ReceiveFrom(buf,1023,add,port,0);
buf[len+1]='\0';
::AfxMessageBox(buf);
Tcp
BOOL CNetDlg::OnInitDialog()
{
……………..
cs.Create(2222);
cs.Listen(5);
cs.m_dlg=this;
}
void CNetDlg::accept()
{
ccs t;
if(!cs.Accept(t))
return;
char buf[1024];
int len=t.Receive(buf,1024,0);
buf[len+1]='\0';
m_l.AddString(buf);
t.Close();
}
Csocket继承类
void ccs::OnAccept(int nErrorCode) //消息
{
// TODO: Add your specialized code here and/or call the base class
m_dlg->accept();
CSocket::OnAccept(nErrorCode);
}