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);

}

posted @ 2006-09-28 18:44  ahuo  阅读(2319)  评论(0编辑  收藏  举报