javacommons/comms/ipclib/socket/src/serversocket.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 #include <sys/socket.h>
       
    19 #include <unistd.h>
       
    20 #include <errno.h>
       
    21 #include <netinet/in.h>
       
    22 #include <string.h>
       
    23 
       
    24 #include "logger.h"
       
    25 
       
    26 #include "serversocket.h"
       
    27 
       
    28 const int INVALID_SOCKET = 0;
       
    29 
       
    30 namespace java
       
    31 {
       
    32 namespace comms
       
    33 {
       
    34 
       
    35 
       
    36 ServerSocket::ServerSocket() : mSocket(INVALID_SOCKET)
       
    37 {
       
    38 //  JELOG2(EJavaComms);
       
    39 }
       
    40 
       
    41 ServerSocket::~ServerSocket()
       
    42 {
       
    43     close();
       
    44 }
       
    45 
       
    46 int ServerSocket::open(int aPort)
       
    47 {
       
    48 //    JELOG2(EJavaComms);
       
    49 
       
    50     mSocket = socket(AF_INET, SOCK_STREAM, 0);
       
    51     if (mSocket == -1)
       
    52     {
       
    53         return -1;
       
    54     }
       
    55 
       
    56     int flag = 1;
       
    57     setsockopt(mSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(int));
       
    58 
       
    59     sockaddr_in addr;
       
    60     addr.sin_family      = AF_INET;
       
    61     addr.sin_port        = htons(aPort);
       
    62     addr.sin_addr.s_addr = htonl(INADDR_ANY);
       
    63 
       
    64     int rc = bind(mSocket, (sockaddr*)&addr, sizeof(addr));
       
    65     if (rc != -1)
       
    66     {
       
    67         rc = listen(mSocket, 3);
       
    68         if (rc != -1)
       
    69         {
       
    70             return 0;
       
    71         }
       
    72     }
       
    73 
       
    74     ELOG2(EJavaComms, "listen failed %d - %s", errno, strerror(errno));
       
    75     close();
       
    76     return -1;
       
    77 }
       
    78 
       
    79 int ServerSocket::accept()
       
    80 {
       
    81     sockaddr_in sa;
       
    82     socklen_t saLen = sizeof(sa);
       
    83 
       
    84     int socket = ::accept(mSocket, (sockaddr*)&sa, &saLen);
       
    85     if (socket<0) ELOG2(EJavaComms, "accept failed %d - %s", errno, strerror(errno));
       
    86 
       
    87     return socket;
       
    88 }
       
    89 
       
    90 int ServerSocket::close()
       
    91 {
       
    92     int rc = 0;
       
    93     if (mSocket !=INVALID_SOCKET)
       
    94     {
       
    95         rc = ::close(mSocket);
       
    96         if (rc<0) ELOG2(EJavaComms, "close failed %d - %s", errno, strerror(errno));
       
    97 
       
    98         mSocket = INVALID_SOCKET;
       
    99     }
       
   100     return rc;
       
   101 }
       
   102 
       
   103 int ServerSocket::getSocket() const
       
   104 {
       
   105     return mSocket;
       
   106 }
       
   107 
       
   108 } // namespace comms
       
   109 } // namespace java
       
   110