?? connectsocket.cpp
字號:
/* mySocket, ConnectSocket Copyright (C) 2006 Kornel Csernai <csko@csko.hu> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA*/#include <ConnectSocket.h>#include <SocketHandler.h>#include <StringLib.h>using namespace StringLib;// ConstructorConnectSocket::ConnectSocket() : RealSocket(-1){ addr = ""; port = -1; connected = false; memset(&saddr, 0, sizeof(saddr)); }// DestructorConnectSocket::~ConnectSocket(){// OnClose(); }// Connect with sockaddr_invoid ConnectSocket::Connect(sockaddr_in _saddr, SocketType _type, bool _blocking){ Connect( (string) inet_ntoa(_saddr.sin_addr), ntohs(_saddr.sin_port), _type, _blocking); }// Connect to host, port void ConnectSocket::Connect(string _addr, int _port, SocketType _type, bool _blocking){ if(IsConnected() or _addr == "" or _port <= 0) return; // throw needed type = _type; blocking = _blocking; if(GetSock() < 0) MakeSock(); debug("Socket created."); string resolved = Resolv(_addr); if(resolved != ""){ debug(_addr + " resolved to " + resolved); }else{ debug("Could not resolve " + _addr); throw SocketException(RESOLV_FAILED); } saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = inet_addr(resolved.c_str()); saddr.sin_port = htons(_port); if(!blocking){#ifdef _WIN32 u_long b = 1; ioctlsocket(GetSock(), FIONBIO, &b);#else int flags = fcntl(GetSock(), F_GETFL, 0); if (fcntl(GetSock(), F_SETFL, flags | O_NONBLOCK ) == -1 ) throw SocketException(NONBLOCK_FAILED);#endif } debug("Connecting to " + _addr + ":" + itos(_port) + " ..."); int ret;#ifdef _WIN32 while((ret = connect(GetSock(), (SOCKADDR*) &saddr, sizeof(saddr))) == -1 and (errno == 115 or errno == 114)){ Sleep(15000);#else while((ret = connect(GetSock(), (const struct sockaddr*) &saddr, sizeof(saddr))) == -1 and (errno == 115 or errno == 114)){ usleep(15000);#endif } if(ret == -1){ debug("Failed to connect: " + GetLastError()); throw SocketException(CONNECT_FAILED); } SocketHandler* handler = SocketHandler::getInstance(); if(!handler) throw SocketException(SOCKETHANDLING_FAILED); debug("Connection successful."); __OnConnected(); handler -> Add(this);#ifdef HAVE_SSL if(is_SSL){ if ((ret = SSL_connect(ssl)) != 1) { char err[300]; snprintf(err, 300, "SSL_connect failed: %s", strerror(errno)); error(err); throw SocketException(SSL_CONNECT_FAILED); } }#endif addr = _addr; port = _port; connected = true; }// Disconnectvoid ConnectSocket::Disconnect(){ if(!IsConnected()) return; Close(); addr = ""; port = -1; memset(&saddr, 0, sizeof(saddr)); connected = false; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -