/* Generated automatically by jlpp - do not edit. */ #include static jakelib::lang::String* jakelib2_strings[] = {null, null, null, null, null, null}; // "Cannot open server socket." static jchar chars_jakelib2_str_0[] = {67,97,110,110,111,116,32,111,112,101,110,32,115,101,114,118,101,114,32,115,111,99,107,101,116,46}; // "Cannot bind to port " static jchar chars_jakelib2_str_1[] = {67,97,110,110,111,116,32,98,105,110,100,32,116,111,32,112,111,114,116,32}; // " - " static jchar chars_jakelib2_str_2[] = {32,45,32}; // "Cannot listen to socket - " static jchar chars_jakelib2_str_3[] = {67,97,110,110,111,116,32,108,105,115,116,101,110,32,116,111,32,115,111,99,107,101,116,32,45,32}; // "Error listening to port " static jchar chars_jakelib2_str_4[] = {69,114,114,111,114,32,108,105,115,116,101,110,105,110,103,32,116,111,32,112,111,114,116,32}; // " - " static jchar chars_jakelib2_str_5[] = {32,45,32}; #line 1 "net/ServerSocket.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: ServerSocket.jlc,v 1.5 2003/10/03 17:57:15 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/net/ServerSocket.h" #include "jakelib2/net/Socket.h" #include "jakelib2/lang/System.h" #include #ifdef WIN32 # include #else # include # include # include # include #endif using namespace jakelib::net; using namespace jakelib::io; using namespace jakelib::lang; JAKELIB_IMPLEMENT_CLASS("jakelib.net.ServerSocket", ServerSocket, Object); /*****************************************************************************\ * ServerSocket | *****************************************************************************/ ServerSocket::ServerSocket(int port, int maxWaitingConnections) { init(port, maxWaitingConnections); } /*****************************************************************************\ * init | *****************************************************************************/ void ServerSocket::init(int port, int maxWaitingConnections) { Socket::initWinsock(); 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, 26)) ->plus( JAKELIB_AT2("jakelib.net.ServerSocket.init"))); } #ifndef WIN32 int optval = 1; setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (void *) &optval, sizeof (optval)); #endif struct sockaddr_in sockAddr; memset( &sockAddr, 0, sizeof(sockAddr) ); sockAddr.sin_family = AF_INET; sockAddr.sin_port = htons( (u_short) port); sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sock, (struct sockaddr *) &sockAddr, sizeof(struct sockaddr_in))) { close(); throw new IOException(JAKELIB_ONDEMAND(jakelib2_strings[1], new jakelib::lang::String(chars_jakelib2_str_1, 0, 20)) ->plus( port )->plus( JAKELIB_ONDEMAND(jakelib2_strings[2], new jakelib::lang::String(chars_jakelib2_str_2, 0, 3)) )->plus( System::explainErrorCode(Socket::getLastError()) )->plus( JAKELIB_AT2("jakelib.net.ServerSocket.init"))); } if (listen(sock, maxWaitingConnections)) { close(); throw new IOException(JAKELIB_ONDEMAND(jakelib2_strings[3], new jakelib::lang::String(chars_jakelib2_str_3, 0, 26)) ->plus( System::explainErrorCode(Socket::getLastError()) )->plus( JAKELIB_AT2("jakelib.net.ServerSocket.init"))); } } ServerSocket::~ServerSocket() { close(); } /*****************************************************************************\ * accept | *****************************************************************************/ Socket* ServerSocket::accept() { JL2_SOCKET con =::accept(sock, null, null); #ifdef WIN32 if (con == INVALID_SOCKET) { #else if (con == -1) { #endif throw new IOException(JAKELIB_ONDEMAND(jakelib2_strings[4], new jakelib::lang::String(chars_jakelib2_str_4, 0, 24)) ->plus( port )->plus( JAKELIB_ONDEMAND(jakelib2_strings[5], new jakelib::lang::String(chars_jakelib2_str_5, 0, 3)) )->plus( System::explainErrorCode(Socket::getLastError()) )->plus( JAKELIB_AT2("jakelib.net.ServerSocket.accept"))); } return new Socket(con, port); } /*****************************************************************************\ * getLocalPort | *****************************************************************************/ int ServerSocket::getLocalPort() { return port; } /*****************************************************************************\ * close | *****************************************************************************/ void ServerSocket::close() { if (sock != 0) #ifdef WIN32 closesocket(sock); #else ::close(sock); #endif sock = 0; }