// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: Socket.jlc,v 1.7 2003/09/27 08:10:28 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/net/Socket.h" #include "jakelib2/net/SocketInputStream.h" #include "jakelib2/net/SocketOutputStream.h" #include "jakelib2/net/InetAddress.h" #include "jakelib2/lang/System.h" #include #ifdef WIN32 # include #else # include # include # include # include #endif using namespace jakelib::net; using namespace jakelib::lang; using namespace jakelib::io; JAKELIB_IMPLEMENT_CLASS("jakelib.net.Socket", Socket, Object); #ifdef HAVE_ERRNO_H #include #endif jboolean Socket::winsockInitialized = false; /*****************************************************************************\ * Socket | *****************************************************************************/ Socket::Socket(String *host, int port) { init(host, port); outputStream = null; inputStream = null; } Socket::Socket(JL2_SOCKET sock, int port) { initWinsock(); this->sock = sock; this->port = port; this->host = null; outputStream = null; inputStream = null; } /*****************************************************************************\ * init | *****************************************************************************/ void Socket::init(String* host, int port) { initWinsock(); this->host = host; this->port = port; sock = socket(PF_INET, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { throw new IOException(`"Cannot open socket."` .. JAKELIB_AT2("jakelib.net.Socket.init")); } struct hostent* hostEntry; hostEntry = gethostbyname(JAKELIB_LATIN1(host)); if (hostEntry == null) { throw new IOException(`"Unknown host: "` .. host .. JAKELIB_AT2("jakelib.net.Socket.init")); } struct sockaddr_in sockAddr; memset( &sockAddr, 0, sizeof(struct sockaddr_in) ); sockAddr.sin_family = AF_INET; sockAddr.sin_addr = *((in_addr*) *hostEntry->h_addr_list); sockAddr.sin_port = htons( (u_short) port); if (connect(sock, (struct sockaddr *) &sockAddr, sizeof(struct sockaddr))) { throw new IOException(System::explainErrorCode(getLastError()) .. JAKELIB_AT2("jakelib.net.Socket.init")); } } /*****************************************************************************\ * ~Socket | *****************************************************************************/ Socket::~Socket() { close(); } /*****************************************************************************\ * initWinsock | *****************************************************************************/ void Socket::initWinsock() { #ifdef WIN32 WSADATA wsdata; if (winsockInitialized) return; if (WSAStartup(MAKEWORD(1, 1), &wsdata)) throw new IOException(`"Cannot find Winsock 1.1 or later."` .. JAKELIB_AT2("jakelib.net.Socket.initWinsock")); winsockInitialized = true; #endif } /*****************************************************************************\ * getLastError | *****************************************************************************/ int Socket::getLastError() { #ifdef WIN32 return WSAGetLastError(); #else return errno; #endif } /*****************************************************************************\ * close | *****************************************************************************/ void Socket::close() { if (sock != 0) #ifdef WIN32 closesocket(sock); #else ::close(sock); #endif sock = 0; inputStream = null; outputStream = null; } /*****************************************************************************\ * getOutputStream | *****************************************************************************/ SocketOutputStream* Socket::getOutputStream() { if (outputStream == null) { outputStream = new SocketOutputStream(sock); } return outputStream; } /*****************************************************************************\ * getInputStream | *****************************************************************************/ SocketInputStream* Socket::getInputStream() { if (inputStream == null) { inputStream = new SocketInputStream(sock); } return inputStream; } /*****************************************************************************\ * getPort | *****************************************************************************/ int Socket::getPort() { return port; } /*****************************************************************************\ * getInetAddress | *****************************************************************************/ InetAddress* Socket::getInetAddress() { struct sockaddr_in sockAddr; #if defined(WIN32) || defined(MACOSX) int len; #else unsigned int len; #endif len = sizeof(sockAddr); if (getpeername(sock, (struct sockaddr *) &sockAddr, &len) != 0) { throw new IOException(`"Cannot get peer name - "` .. System::explainErrorCode(getLastError()) .. JAKELIB_AT2("jakelib.net.Socket.getInetAddress")); } else { return new InetAddress((char*) &sockAddr.sin_addr); } }