MFC之csocket

1、一般使用流程(阻塞模式)

1)添加头文件

#include "afxsock.h"

2)初始化

一般在InitInstance()的开始部分增加:

 if (!AfxSocketInit())
 {
  AfxMessageBox("Windows 通信端口初始化失败");
  return FALSE;
 }

3)定义对象

CSocket m_socketRec;

4)发送端:

 if (m_socketListenS.Create(::atoi(sendPort)))
 {
  GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
  
  m_socketListenS.Bind(::atoi(sendPort),sendIp);
  if (m_socketListenS.Listen())
  {
   m_socketListenS.Accept(m_socketSend);
   m_socketListenS.Close();
   static int nIdex = 100;
   CString str;
   str.Format("%010d",nIdex);
   m_socketSend.Send(str,10,0);
         m_edit_send.SetWindowText(str);
   //SetTimer(1,3000,NULL);
  }
 }
 else
 {
  AfxMessageBox("socket创建失败");
  return;
 }

5)接收端

 if (m_socketRec.Create())
 {
  m_socketRec.Connect(recIp,atoi(recPort));
  CString str;
  m_socketRec.Receive((LPVOID)str.GetBuffer(10),10,0);
     m_edit_rec.SetWindowText(str);
  //SetTimer(2,1000,NULL);
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值