SocketDx.cpp
资源名称:Telnet.rar [点击查看]
上传用户:net1718
上传日期:2016-03-27
资源大小:36k
文件大小:1k
源码类别:
Telnet客户端
开发平台:
Visual C++
- // SocketDx.cpp: implementation of the CSocketDx class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Telnet.h"
- #include "Telnet.h"
- #include "SocketDx.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CSocketDx::CSocketDx(char *strIP,int nPort)
- {
- unsigned long ip;
- if((*strIP <= '9') && (*strIP >= '0'))
- {
- if((ip = inet_addr(strIP)) == INADDR_NONE)
- printf("invalid host ip given");
- }
- else
- {
- hostent* ent = gethostbyname(strIP);
- if(!ent) printf("nErrorn");
- ip = *(unsigned long*)(ent->h_addr);
- }
- m_sockaddr_in.sin_family = AF_INET;
- m_sockaddr_in.sin_port = htons(nPort);
- m_sockaddr_in.sin_addr = *(in_addr*)&ip;
- }
- CSocketDx::~CSocketDx()
- {
- }
- int CSocketDx::Create()
- {
- m_hSocket = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
- if ( m_hSocket == INVALID_SOCKET) return -1;
- return 0;
- }
- int CSocketDx::Connect()
- {
- int nRet;
- nRet = connect(m_hSocket,(sockaddr*)&m_sockaddr_in,sizeof(sockaddr));
- if ( nRet == SOCKET_ERROR ) return -1;
- return 0;
- }
- SOCKET CSocketDx::TelnetConnect()
- {
- int nRet;
- nRet = Create();
- if ( nRet < 0 ) return NULL;
- nRet = Connect();
- if ( nRet < 0 ) return NULL;
- return m_hSocket;
- }