zoukankan      html  css  js  c++  java
  • MFC网络编程,CSocket,网络通信,网络聊天室




    CSocketServer

    BOOL CCSocketServerDlg::OnInitDialog()
    {
    //时刚开始接收的按钮无效
    m_recv.EnableWindow(false);


    return TRUE;  // return TRUE  unless you set the focus to a control
    }
    void CCSocketServerDlg::OnListen() 
    {
    // TODO: Add your control notification handler code here
    // m_server.Create(1900);//端口
    m_server.Bind(4001);
    m_server.Listen();//监听
    m_server.Accept(m_receive);
    m_recv.EnableWindow(true);//接收按钮有效
    m_listen.EnableWindow(false);//监听按钮无效

    }


    void CCSocketServerDlg::OnReceive() 
    {
    // TODO: Add your control notification handler code here
    char buf[255]="";
    m_receive.Receive(buf,255,0);//接收消息到buf
    CString s_msg;
    s_msg.Format("%s",buf);//buf的值传到字符串s_mg
    m_EDITValue=m_EDITValue+s_msg;
    UpdateData(false);
    }
    CSocketClient
    BOOL CCSocketClientDlg::OnInitDialog()
    { m_ip=CString("127.0.0.1");//默认的ip地址
    UpdateData(false);
    m_send.EnableWindow(false);//发送按钮失效
    return TRUE;  // return TRUE  unless you set the focus to a control
    }
    void CCSocketClientDlg::OnSend() 
    {
    // TODO: Add your control notification handler code here
    UpdateData(true);
    m_client.Send(m_message,255);
    m_msg.SetSel(0,-1);//发送全部文字
    m_msg.ReplaceSel("",true);//发送框置空

    }


    void CCSocketClientDlg::OnConnect() 
    {
    // TODO: Add your control notification handler code here
    UpdateData(true);
    m_client.Create(4001);//创建端口,采用默认的端口号
    if(m_client.Connect(m_ip,4001))
    {
    MessageBox("客户端连接成功!");
    m_send.EnableWindow(true);
    m_connect.EnableWindow(false);//禁止再连接
    }
    else
    {
    MessageBox("客户端连接失败!");
    }

    }


    void CCSocketClientDlg::OnDestroy() 
    {
    CDialog::OnDestroy();

    // TODO: Add your message handler code here
    m_client.Close();
    }
  • 相关阅读:
    20181113-2 每周例行报告
    20181030-4 每周例行报告
    20180925-5 代码规范,结对要求
    20181023-3 每周例行报告
    20181016-10 每周例行报告
    PSP总结报告
    作业要求 20181204-1 每周例行报告
    公开感谢
    附加作业 软件工程原则的应用实例分析
    作业要求 20181127-2每周例行报告
  • 原文地址:https://www.cnblogs.com/zhangaihua/p/3718088.html
Copyright © 2011-2022 走看看