kdecore Library API Documentation

ksock.h

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (C) 1997 Torben Weis (weis@kde.org)
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2 of the License, or (at your option) any later version.
00009  *
00010  *  This library 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 GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  *  Boston, MA 02111-1307, USA.
00019  */
00020 #ifndef KSOCK_H
00021 #define KSOCK_H
00022 
00023 #include <qobject.h>
00024 #include <sys/types.h>
00025 // we define STRICT_ANSI to get rid of some warnings in glibc
00026 #ifndef __STRICT_ANSI__
00027 #define __STRICT_ANSI__
00028 #define _WE_DEFINED_IT_
00029 #endif
00030 #include <sys/socket.h>
00031 #ifdef _WE_DEFINED_IT_
00032 #undef __STRICT_ANSI__
00033 #undef _WE_DEFINED_IT_
00034 #endif
00035 
00036 #include <sys/un.h>
00037 
00038 #include <netinet/in.h>
00039 class QSocketNotifier;
00040 
00041 #ifdef KSOCK_NO_BROKEN
00042 // This is here for compatibility with old applications still using the constants
00043 // Never use them in new programs
00044 
00045 // Here are a whole bunch of hackish macros that allow one to
00046 // get at the correct member of ksockaddr_in
00047 // But since ksockaddr_in is IPv4-only, and deprecated...
00048 
00049 typedef sockaddr_in ksockaddr_in;
00050 #define get_sin_addr(x) x.sin_addr
00051 #define get_sin_port(x) x.sin_port
00052 #define get_sin_family(x) x.sin_family
00053 #define get_sin_paddr(x) x->sin_addr
00054 #define get_sin_pport(x) x->sin_port
00055 #define get_sin_pfamily(x) x->sin_family
00056 #endif
00057 
00058 #define KSOCK_DEFAULT_DOMAIN PF_INET
00059 
00060 class KSocketPrivate;
00061 class KServerSocketPrivate;
00062 
00081 class KSocket : public QObject
00082 {
00083     Q_OBJECT
00084 public:
00089     KSocket( int _sock );
00096     KSocket( const char *_host, unsigned short int _port, int timeOut = 30);
00097 
00102     KSocket( const char * _path );
00103 
00107     virtual ~KSocket();
00108 
00113     int socket() const { return sock; }
00114 
00123     void enableRead( bool enable );
00124 
00136     void enableWrite( bool enable );
00137 
00138 #ifdef KSOCK_NO_BROKEN
00139     // BCI: remove in libkdecore.so.4
00147     unsigned long ipv4_addr();
00148 
00149     // BCI: remove in libkdecore.so.4
00155     static bool initSockaddr(ksockaddr_in *server_name, const char *hostname, unsigned short int port, int domain = PF_INET);
00156 #endif
00157 
00158 signals:
00166     void readEvent( KSocket *s );
00167 
00179     void writeEvent( KSocket *s );
00180 
00185     void closeEvent( KSocket *s );
00186 
00187 public slots:
00195     void slotWrite( int x);
00196 
00204     void slotRead( int x );
00205 
00206 protected:
00207     bool connect( const QString& _host, unsigned short int _port, int timeout = 0 );
00208     bool connect( const char *_path );
00209 
00210     /******************************************************
00211      * The file descriptor for this socket. sock may be -1.
00212      * This indicates that it is not connected.
00213      */
00214     int sock;
00215 
00216 private:
00217     KSocket(const KSocket&);
00218     KSocket& operator=(const KSocket&);
00219 
00220     KSocketPrivate *d;
00221 
00222 };
00223 
00224 
00241 class KServerSocket : public QObject
00242 {
00243     Q_OBJECT
00244 public:
00252     KServerSocket( unsigned short int _port, bool _bind = true );
00253 
00261     KServerSocket( const char *_path, bool _bind = true);
00262 
00266     virtual ~KServerSocket();
00267 
00274     bool bindAndListen();
00275 
00281     int socket() const { return sock; }
00282 
00287     unsigned short int port();
00288 
00289 #ifdef KSOCK_NO_BROKEN
00290     // BCI: remove in libkdecore.so.4
00297     unsigned long ipv4_addr();
00298 #endif
00299 
00300 public slots:
00304     virtual void slotAccept( int ); // why is this virtual?
00305 
00306 signals:
00316     void accepted( KSocket*s );
00317 
00318 protected:
00319     bool init( unsigned short int );
00320     bool init( const char *_path );
00321 
00326     int sock;
00327 
00328 private:
00329     KServerSocket(const KServerSocket&);
00330     KServerSocket& operator=(const KServerSocket&);
00331 
00332     KServerSocketPrivate *d;
00333 };
00334 
00335 
00336 #endif
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:55 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001