class socket_base : public io_base
{
private:
- bool connect(const void* options){ return false; }
+ void connect(const void* options){}
//virtual void create()=0;
public:
public:
// Require Implelements (Virtual Methods)
- virtual bool connect(const char* host, int port)=0;
- virtual bool connect(const char* host, int port, int socktype, int family)=0;
+ virtual void connect(const char* host, int port)=0;
+ //virtual bool connect(const char* host, int port, int socktype, int family)=0;
virtual int send(const void* s, size_t size, int flag=0)=0;
virtual int recv(void* s, size_t size, int flag=0)=0;
namespace windows
{
//////////////////////////////////////////////////////////////////////////////////////
-
- class safe_sockaddr
- {
- public:
- ::addrinfo /*hints,*/ *addr;
-
- /////////////////////////////////////////////////
- safe_sockaddr(const char* host, int port, int socktype, int family)
- {
- ::addrinfo hints/*, *addr*/;
- ::memset(&hints, 0, sizeof(hints));
- hints.ai_socktype = socktype;
- hints.ai_family = family;
-
-#pragma warning(disable: 4996)
- char port_str[12];
- sprintf(port_str, "%d", port);
-#pragma warning(default: 4996)
-
- //if ( !m_impl.getaddrinfo(host, port_str, &hints, &addr) )
- if ( !::getaddrinfo(host, port_str, &hints, &addr) )
- throw socket_exception(::WSAGetLastError(), "roast::socket::socket() ::getaddrinfo() function is error.");
- }
- virtual ~safe_sockaddr()
- {
- if ( addr != NULL )
- {
- ::freeaddrinfo(addr);
- addr = NULL;
- }
- }
- };
-
- //////////////////////////////////////////////////////////////////////////////////////
class socket : public socket_base
{
private:
::SOCKET m_sock;
+ int m_af;
+ int m_socket_type;
+ int m_socket_protocol;
+
public:
socket(int af=AF_INET, int type=SOCK_STREAM, int protocol=0)
: m_sock(NULL)
{
+ m_af = af;
+ m_socket_type = type;
+ m_socket_protocol = protocol;
+
m_sock = ::socket(af, type, protocol);
if ( ::WSAGetLastError() != 0 )
- throw socket_exception(::WSAGetLastError(), "roast::socket::socket() ::socket() function is error.");
+ throw socket_exception(::WSAGetLastError(), "roast::socket::socket() ::socket() function was error.");
if ( m_sock == INVALID_SOCKET )
throw socket_exception(::WSAGetLastError(), "roast::socket::socket() m_sock is INVALID_SOCKET.");
}
//////////////////////////////////////////////////////////////////
- bool close()
+ void close()
{
if ( m_sock != INVALID_SOCKET )
{
::closesocket(m_sock);
if ( ::WSAGetLastError() != 0 )
- throw socket_exception(::WSAGetLastError(), "roast::socket::close() ::closesocket() function is error.");
+ throw socket_exception(::WSAGetLastError(), "roast::socket::close() ::closesocket() function was error.");
m_sock = INVALID_SOCKET;
}
}
- bool connect(const char* host, int port)
+ void connect(const char* host, int port)
{
- close();
+ // Already Socket was Close...?
+ //close();
- ::connect(m_sock, addr, addrlen)
- }
- bool connect(const char* host, int port, int socktype, int family)
- {
+ if ( m_sock != INVALID_SOCKET )
+ throw socket_exception(::WSAGetLastError(), "roast::socket::connect() Socket was already opened.");
+
+ ::addrinfo hints;
+ ::memset(&hints, 0, sizeof(hints));
+ hints.ai_socktype = m_socket_type;
+ hints.ai_family = m_af;
+
+#pragma warning(disable: 4996)
+ char port_str[12];
+ sprintf(port_str, "%d", port);
+#pragma warning(default: 4996)
+
+ ::addrinfo *addr=NULL;
+ if ( ::getaddrinfo(host, port_str, &hints, &addr) != 0 )
+ throw socket_exception(::WSAGetLastError(), "roast::socket::connect() ::getaddrinfo() function was error.");
+
+ if ( ::connect(m_sock, addr->ai_addr, addr->ai_addrlen) != 0 ){
+ ::freeaddrinfo(addr);
+ throw socket_exception(::WSAGetLastError(), "roast::socket::connect() ::connect() function was error.");
+ }
+ ::freeaddrinfo(addr);
}
- virtual int send(const void* s, size_t size, int flag=0)=0;
- virtual int recv(void* s, size_t size, int flag=0)=0;
+ int send(const void* s, size_t size, int flag=0){return 0;}
+ int recv(void* s, size_t size, int flag=0){return 0;}
};
}
}