📄 tcp_serversocket.cxx
字号:
/* ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */static const char* const TcpServerSocket_cxx_Version = "$Id: Tcp_ServerSocket.cxx,v 1.8.2.1 2003/03/01 01:28:27 sprajpat Exp $";#ifndef __vxworks#include "global.h"#include <errno.h>#include <unistd.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdio.h>#ifndef WIN32#include <strings.h>#else#include <string.h>#endif#include "VNetworkException.hxx"#include "TransportCommon.hxx"#include "Tcp_ServerSocket.hxx"#include "VEnvVar.hxx"#include "cpLog.h"#include "NetworkAddress.h"#include "NetworkConfig.hxx"#define LISTENQ 15TcpServerSocket::TcpServerSocket(int servPort) throw (VNetworkException&){ listenOn(servPort);}voidTcpServerSocket::listenOn(int servPort) throw (VNetworkException&){ struct addrinfo hints, *res, *sRes; memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_PASSIVE; hints.ai_family = NetworkConfig::instance().getAddrFamily(); hints.ai_socktype = SOCK_STREAM; char serv[56], err; sprintf(serv, "%u", servPort); if((err= getaddrinfo(NULL, serv, &hints, &res)) != 0) { char buf[256]; sprintf(buf, "getaddrinfo failed, reason:%s", gai_strerror(errno)); cpLog(LOG_ERR, buf); throw VNetworkException(buf, __FILE__, __LINE__, errno); } sRes = res; do { _serverConn._connId = ::socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (_serverConn._connId < 0) { continue; } int on = 1;#ifndef WIN32 if ( setsockopt ( _serverConn._connId, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<sockbuf_t *>(&on), sizeof ( on )) ) { // this is an error -- can't set it char buf[256]; sprintf(buf, "setsockopt failed, reason: %s", strerror(errno)); cpLog(LOG_ALERT, "%s", buf); }#endif if (::bind(_serverConn._connId, res->ai_addr, res->ai_addrlen) == 0) { //Success delete []_serverConn._connAddr; _serverConn._connAddr = (struct sockaddr*) new char[res->ai_addrlen]; memcpy(_serverConn._connAddr, res->ai_addr, res->ai_addrlen); cpLog(LOG_INFO, "(%s) TCP server bound to port %d", (res->ai_family == PF_INET) ? "IPv4" : "IPv6", servPort); break; } ::close(_serverConn._connId); } while((res = res->ai_next) != NULL); freeaddrinfo(sRes); if(res == NULL) { char buf[256]; sprintf(buf, "Failed to initialize TCP server on port %d, reason: %s", servPort, strerror(errno)); cpLog(LOG_ALERT, "%s", buf); throw VNetworkException(buf, __FILE__, __LINE__, errno); } if (::listen(_serverConn._connId, LISTENQ)) { char buf[256]; sprintf(buf, "listen failed, reason:%s", strerror(errno)); cpLog(LOG_ALERT, "%s", buf); throw VNetworkException(buf, __FILE__, __LINE__, errno); }}TcpServerSocket::TcpServerSocket(const TcpServerSocket& other){ _serverConn = other._serverConn; _clientConn = other._clientConn;}TcpServerSocket&TcpServerSocket::operator=(TcpServerSocket& other){ if (this != &other) { _serverConn = other._serverConn; _clientConn = other._clientConn; } return *this;}TcpServerSocket::TcpServerSocket() throw (VNetworkException&){ int port = VEnvVar::VPS_PORT; listenOn(port);}TcpServerSocket::~TcpServerSocket(){ close();}intTcpServerSocket::accept(Connection& con) throw (VNetworkException&){ if ((con._connId = ::accept(_serverConn._connId, (SA*) con._connAddr, &con._connAddrLen)) < 0) { char buf[256]; sprintf(buf, "Failed to accept the connection, reason:%s", strerror(errno)); cpLog(LOG_DEBUG, buf); throw VNetworkException(buf, __FILE__, __LINE__, errno); } cpLog(LOG_DEBUG_STACK, "Connection from %s", con.getDescription().c_str()); con._live = true; con.setState(); return con._connId;}voidTcpServerSocket::close(){ #if !defined(WIN32) ::close(_serverConn.getConnId()); #else ::closesocket(_serverConn.getConnId()); #endif}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -