S3FC project page S3FC home page

Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

s3_socket_tcp.h

Go to the documentation of this file.
00001 /*
00002  * Stone Three Foundation Class (s3fc) provides a number of utility classes.
00003  * Copyright (C) 2001 by Stone Three Signal Processing (Pty) Ltd.
00004  *
00005  * Authored by Stone Three Signal Processing (Pty) Ltd.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  * 
00021  * Please see the file 'COPYING' in the source root directory.
00022  */
00023 
00033 #ifndef S3_SOCKET_TCP_H
00034 #define S3_SOCKET_TCP_H
00035 
00036 #include <string>
00037 #include <iostream>
00038 
00039 #include <s3fc/s3_macros.h>
00040 
00041 #ifdef _WIN32
00042 // Define NOMINMAX to prevent the definition of min and max macros in windef.h
00043 #define NOMINMAX
00044 #include <winsock.h>
00045 // Some typedefs for WinSock compatibility <jpool>
00046 typedef unsigned long in_addr_t;
00047 typedef unsigned int in_port_t;
00048 #else
00049 #include <sys/types.h>
00050 #include <sys/socket.h>
00051 #include <netinet/in.h>
00052 #include <arpa/inet.h>
00053 #include <errno.h>
00054 #include <unistd.h>
00055 #include <netdb.h>
00056 #endif
00057 
00058 #include <s3fc/s3_semaphore.h>
00059 #include <s3fc/s3_exception.h>
00060 
00061 class s3_socket_tcp
00062 {
00063   private:
00064    bool initialize();
00065   protected:
00066    bool set_reuseaddr;
00067    int sock, my_errno, my_h_errno;
00068    void set_errnos(int new_errno=0, int new_h_errno=0);
00069    void change_socket(int newsock);
00070   public:
00072    s3_socket_tcp(int newsock=INVALID_SOCKET, bool new_set_reuseaddr=true)
00073       throw(s3_generic_exception);
00074 
00076    s3_socket_tcp(const s3_socket_tcp& template_sock)
00077       throw(s3_generic_exception);
00078 
00079    virtual ~s3_socket_tcp();
00080    
00082    bool bind(in_addr_t addr, in_port_t port);
00083 
00085    bool listen(int backlog);
00086 
00088    bool accept(s3_socket_tcp& newsock, sockaddr_in* clientname=0);
00089    
00091    bool connect(const std::string& IP_address, in_port_t port);
00092 
00094    bool close(bool reinit=true) throw(s3_generic_exception);
00095 
00097    std::string get_error() const;
00098 
00100    int get_errno() const
00101    {
00102       return my_errno;
00103    }
00104 
00106    int get_h_errno() const
00107    {
00108       return my_h_errno;
00109    }
00110 
00112    bool is_closed() const
00113    {
00114       return (sock < 0);
00115    }
00116 
00118    bool is_valid() const
00119    {
00120       return (sock != INVALID_SOCKET);
00121    }
00122 
00135    virtual bool read(void *data, int size, s3_semaphore *term=0);
00136 
00151    virtual bool write(void *data, int size, int max_packet=-1,
00152             s3_semaphore *term=0);
00153 
00164    int select_rd(int timeout=1);
00165 
00176    int select_wr(int timeout=1);
00177 
00178   
00180    friend bool accept(s3_socket_tcp& newsock, sockaddr_in* clientname);   
00181 
00182    
00183 };
00184 
00185 #endif

Send comments to: s3fc@stonethree.com SourceForge Logo