• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/Windows/DcSocketServer.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net)
00003 This file is part of Beesnest.
00004 
00005 Beesnest is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 Beesnest is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with Beesnest; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 
00021 /*
00022  * DcSocketServer.cpp: Implementation of CdcSocketServer class.
00023  * for documentation see the header file.
00024  */
00025 
00026 #include "DcSocketServer.h"
00027 
00028 #define DEFAULT_PORT    80
00029 
00030 int CdcSocketServer::nObjCounter = 0;
00031 
00032 CdcSocketServer* CdcSocketServer::GetSocketServer (int port) throw (CdcException)
00033 {
00034     /* Check for just one server socket object. */
00035     if ( nObjCounter > 0 )
00036         throw CdcException ("More than one server socket.");
00037     nObjCounter++;
00038 
00039     SOCKADDR_IN local;
00040     SOCKET servsock;
00041 
00042     /* Init socket address Struct. */
00043     local.sin_family = AF_INET;
00044     local.sin_addr.s_addr = INADDR_ANY;
00045     local.sin_port = htons (port);  /* Port MUST be in Network Byte Order. */
00046 
00047     /* Make socket object. */
00048     servsock = socket (AF_INET, SOCK_STREAM, 0);    /* TCP socket */
00049     if (servsock == INVALID_SOCKET)
00050         throw CdcExceptionSocket (WSAGetLastError());
00051 
00052     /* Bind the server socket. */
00053     if ( bind (servsock,(struct sockaddr*)&local, sizeof(local) )
00054         == SOCKET_ERROR)
00055         throw CdcExceptionSocket (WSAGetLastError());
00056 
00057     /* Init the wrapping object. */
00058     CdcSocketServer* psocsrv = new CdcSocketServer;
00059     psocsrv->Init (servsock);   /* may throw CdcException. */
00060 
00061     /* Return by copy the object. */
00062     return psocsrv;
00063 }
00064 
00065 
00066 CdcSocketServer* CdcSocketServer::GetSocketServer () throw (CdcException)
00067 {
00068     return GetSocketServer (DEFAULT_PORT);
00069 }
00070 
00071 
00072 CdcSocket* CdcSocketServer::Listen () throw (CdcException)
00073 {
00074     SOCKADDR_IN from;
00075     int fromlen = sizeof(from);
00076 
00077     /* Listen to the port. */
00078     int ret = listen (oSocket, SOMAXCONN);
00079 
00080     /* Some error. */
00081     if (ret  == SOCKET_ERROR)
00082         throw CdcExceptionSocket (WSAGetLastError());
00083 
00084     /* Accept connection. */
00085     SOCKET msgsock = accept (oSocket, (struct sockaddr*)&from, &fromlen);
00086 
00087     /* Wrap in new CdcSocket object. */
00088     CdcSocket* psock = CdcSocket::GetNewSocket (msgsock, from);
00089 
00090     /* Return the pointer to the object. */
00091     return psock;
00092 }
00093 
00094 
00095 CdcSocket* CdcSocketServer::ListenNonBlock (int wait) throw (CdcException)
00096 {
00097     FD_SET set;
00098     FD_ZERO (&set);
00099     FD_SET (oSocket, &set);
00100     struct timeval swait = {0, wait * 1000};
00101     int ret;
00102     SOCKADDR_IN from;
00103     int fromlen = sizeof(from);
00104 
00105     /* Put socket in listen mode. */
00106     ret = listen (oSocket, 0);
00107     if (ret == SOCKET_ERROR)
00108         throw CdcExceptionSocket (WSAGetLastError());
00109     /* Wait for 'wait' mSec for an incoming connection. */
00110     ret = select (1, &set, NULL, NULL, &swait);
00111     /* Time out. */
00112     if (ret == 0) return NULL;
00113     /* Some error. */
00114     if (ret  == SOCKET_ERROR)
00115         throw CdcExceptionSocket (WSAGetLastError());
00116     /* Accept connection. */
00117     SOCKET msgsock = accept (oSocket, (struct sockaddr*)&from, &fromlen);
00118     /* Wrap in new CdcSocket object. */
00119     CdcSocket* psock = CdcSocket::GetNewSocket (msgsock, from);
00120     /* Return the pointer to the object. */
00121     return psock;
00122 }

Generated on Mon Oct 11 2010 16:23:25 for Beesnest by  doxygen 1.7.2