• 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();
    }
  • 相关阅读:
    java 多线程4: java线程的优先级
    Thread.currentThread().getName() ,对象实例.getName() 和 this.getName()区别
    go http
    go redis
    go tcp
    go 单元测试
    go 定时器
    go channel
    go goroutine
    go 错误处理
  • 原文地址:https://www.cnblogs.com/zhangaihua/p/3718088.html
Copyright © 2020-2023  润新知