SocketDx.cpp
上传用户:aphszp
上传日期:2007-01-09
资源大小:42k
文件大小:1k
源码类别:

Telnet服务器

开发平台:

Visual C++

  1. // SocketDx.cpp: implementation of the CSocketDx class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Telnet.h"
  6. #include "Telnet.h"
  7. #include "SocketDx.h"
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char THIS_FILE[]=__FILE__;
  11. #define new DEBUG_NEW
  12. #endif
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CSocketDx::CSocketDx(char *strIP,int nPort)
  17. {
  18. unsigned long ip;
  19.   if((*strIP <= '9') && (*strIP >= '0'))
  20.   {
  21.      if((ip = inet_addr(strIP)) == INADDR_NONE)
  22.        printf("invalid host ip given");
  23.   }
  24.   else
  25.   {
  26.     hostent* ent = gethostbyname(strIP);
  27.     if(!ent) printf("nErrorn");
  28.     ip = *(unsigned long*)(ent->h_addr);
  29.   }
  30. m_sockaddr_in.sin_family = AF_INET;
  31. m_sockaddr_in.sin_port = htons(nPort);
  32. m_sockaddr_in.sin_addr = *(in_addr*)&ip;
  33. }
  34. CSocketDx::~CSocketDx()
  35. {
  36. }
  37. int CSocketDx::Create()
  38. {
  39.   m_hSocket = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  40.   if ( m_hSocket == INVALID_SOCKET) return -1;
  41.   return 0;
  42. }
  43. int CSocketDx::Connect()
  44. {
  45. int nRet;
  46.   nRet = connect(m_hSocket,(sockaddr*)&m_sockaddr_in,sizeof(sockaddr));
  47.   if ( nRet == SOCKET_ERROR ) return -1;
  48.   return 0;
  49. }
  50. SOCKET CSocketDx::TelnetConnect()
  51. {
  52. int nRet;
  53. nRet = Create();
  54. if ( nRet < 0 ) return NULL;
  55. nRet = Connect();
  56. if ( nRet < 0 ) return NULL;
  57. return m_hSocket;
  58. }