/* Generated automatically by jlpp - do not edit. */ #include static jakelib::lang::String* jakelib2_strings[] = {null, null, null, null}; // "Cannot open socket." static jchar chars_jakelib2_str_0[] = {67,97,110,110,111,116,32,111,112,101,110,32,115,111,99,107,101,116,46}; // "Unknown host: " static jchar chars_jakelib2_str_1[] = {85,110,107,110,111,119,110,32,104,111,115,116,58,32}; // "Cannot find Winsock 1.1 or later." static jchar chars_jakelib2_str_2[] = {67,97,110,110,111,116,32,102,105,110,100,32,87,105,110,115,111,99,107,32,49,46,49,32,111,114,32,108,97,116,101,114,46}; // "Cannot get peer name - " static jchar chars_jakelib2_str_3[] = {67,97,110,110,111,116,32,103,101,116,32,112,101,101,114,32,110,97,109,101,32,45,32}; #line 1 "net/Socket.jlc" // -*- 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(JAKELIB_ONDEMAND(jakelib2_strings[0], new jakelib::lang::String(chars_jakelib2_str_0, 0, 19)) ->plus( JAKELIB_AT2("jakelib.net.Socket.init"))); } struct hostent* hostEntry; hostEntry = gethostbyname(JAKELIB_LATIN1(host)); if (hostEntry == null) { throw new IOException(JAKELIB_ONDEMAND(jakelib2_strings[1], new jakelib::lang::String(chars_jakelib2_str_1, 0, 14)) ->plus( host )->plus( 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()) ->plus( 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(JAKELIB_ONDEMAND(jakelib2_strings[2], new jakelib::lang::String(chars_jakelib2_str_2, 0, 33)) ->plus( 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(JAKELIB_ONDEMAND(jakelib2_strings[3], new jakelib::lang::String(chars_jakelib2_str_3, 0, 23)) ->plus( System::explainErrorCode(getLastError()) )->plus( JAKELIB_AT2("jakelib.net.Socket.getInetAddress"))); } else { return new InetAddress((char*) &sockAddr.sin_addr); } }