一个封装的C++ Socket类

一个封装的C++ Socket类
2011年06月03日
   #include "stdafx.h"
  #include "libyfun.h"
  #include "mytcp.h"
  //#include "ty_server.h"
  //构造函数
  myCTcp::myCTcp ()
  {
  m_timeout = 3000;
  m_nSock = -1;
  WSADATA sockData;
  if (WSAStartup (MAKEWORD (2, 2), &sockData))
  {
  printf("TCP加载失败\n");
  exit (0);
  }
  }
  //析构函数
  myCTcp::~myCTcp ()
  {
  Close ();
  WSACleanup ();
  }
  /*赋值
  入参:nSockfd - socket句柄
  出参:赋值后的socket句柄
  */
  int myCTcp::operator = (int nSockfd)
  {
  assert ((m_nSock == -1) && (nSockfd > -1));
  m_nSock = nSockfd;
  return m_nSock;
  }
  /*取出socket句柄
  入参:无
  出参:取出的socket句柄
  */
  int myCTcp::GetHandle () const
  {
  return m_nSock;
  }
  /*创建socket
  入参:无
  出参:1: 成功 ; 0: 失败
  */
  int myCTcp::Open ()
  {
  assert (m_nSock == -1);
  if(m_nSock != -1)
  Close ();
  m_nSock = socket (AF_INET, SOCK_STREAM, 0);
  setsockopt (m_nSock, SOL_SOCKET, SO_SNDTIMEO, (char *)&m_timeout, sizeof(m_timeout));
  setsockopt (m_nSock, SOL_SOCKET, SO_RCVTIMEO, (char *)&m_timeout, sizeof(m_timeout));
  return (m_nSock != -1);
  }
  /*关闭socket
  入参:无
  出参:1: 成功 ; 0: 失败
  */
  int myCTcp::Close ()
  {
  if (m_nSock != -1)
  {
  if(closesocket (m_nSock) != 0)
  {
  int err = WSAGetLastError();
  // DEBUG (gbl.is_debug, gbl.debug_level, gbl.log_file, "WSAGetLastError %d", err);
  }
  m_nSock = -1;
  }
  return 1;
  }
  /*连接
  入参:pHost - IP地址或主机名
  nPort - 端口
  出参:1: 成功 ; 0: 失败
  */
  int myCTcp::Connect (const char *pHost, int nPort) const
  {
  assert ((m_nSock != -1) && pHost && (nPort > 0));
  struct sockaddr_in addr;
  struct hostent *phe = NULL;
  memset (&addr, 0, sizeof (addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons (nPort);
  if ((addr.sin_addr.s_addr = inet_addr (pHost)) == (unsigned long int) -1)
  {
  if ((phe = gethostbyname (pHost)) == NULL)
  return 0;
  memcpy ((char *)&addr.sin_addr, phe->h_addr, phe->h_length);
  }
  return (connect (m_nSock, (struct sockaddr *)&addr, sizeof (addr)) == 0);
  }
  /*绑定
  入参:pIP - IP地址
  nPort - 端口
  出参:1: 成功 ; 0: 失败
  */
  int myCTcp::Bind (const char *pIP, int nPort) const
  {
  assert ((m_nSock != -1) && (nPort > 0));
  struct sockaddr_in addr;
  struct hostent *phe = NULL;
  int opt=1;
  if (setsockopt (m_nSock, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(int)) == -1)
  {
  return 0;
  }
  memset (&addr, 0, sizeof (addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons (nPort);
  if (!pIP)
  {
  addr.sin_addr.s_addr = htonl (INADDR_ANY);
  }
  else
  {
  if ((addr.sin_addr.s_addr = inet_addr (pIP)) == (unsigned long int) -1)
  {
  if ((phe = gethostbyname (pIP)) == NULL)
  return 0;
  memcpy ((char *)&addr.sin_addr, phe->h_addr, phe->h_length);
  }
  }
  return (bind (m_nSock, (struct sockaddr *)&addr, sizeof (addr)) == 0);
  }
  /*监听
  入参:nNum - 监听数目
  出参:1: 成功 ; 0: 失败
  */
  int myCTcp::Listen (int nNum) const
  {
  assert ((m_nSock != -1) && (nNum > 0));
  return (listen (m_nSock, nNum) == 0);
  }
  /*接受连接
  入参:无
  出参:其他: 连接套接字句柄 ; -1: 失败
  */
  int myCTcp::Accept () const
  {
  assert (m_nSock != -1);
  return (accept (m_nSock, (struct sockaddr *)NULL, NULL));
  }
  /*发送指定数目数据
  入参:pBuf - 发送缓存
  nCount - 需发送字节数
  nMicsec - socket超时值,单位:微妙,缺省:500000微妙
  出参:实际发送字节数 ,如果发送失败,返回负数
  */
  int myCTcp::Sendn (const void *pBuf, int nCount, int nMicsec) const
  {
  assert ((m_nSock != -1) && pBuf && (nCount > 0));
  int nLeft = nCount, nWritten = 0;
  const char *ptr = (const char *)pBuf;
  while (nLeft > 0)
  {
  if ((nWritten = send (m_nSock, ptr, nLeft, 0)) socket超时值,单位:微妙,缺省:500000微妙
  出参:实际接收字节数 ,如果接收失败,返回负数,如果对方关闭,返回0
  */
  int myCTcp::Recvn (void *pBuf, int nCount, int nMicsec) const
  {
  assert ((m_nSock != -1) && pBuf && (nCount > 0));
  int nLeft = nCount, nRead = 0;
  char *ptr = (char *)pBuf;
  while (nLeft > 0)
  {
  if ((nRead = recv (m_nSock, ptr, nLeft, 0)) < 0)
  {
  return MY_SOCK_ERROR;
  }
  else if (nRead == 0)
  {
  return 0;
  }
  nLeft -= nRead;
  ptr += nRead;
  }
  return nCount;
  }
  /*设置地址可重用
  入参:无
  出参:1: 成功 ; 0: 失败
  */
  int myCTcp::SetReuseAddr ()
  {
  assert (m_nSock != -1);
  int opt = 1;
  if (setsockopt (m_nSock, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(int)) == -1)
  {
  return 0;
  }
  return 1;
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值