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 #include <sys/socket.h>
00026 
00027 #include <sys/un.h>
00028 
00029 #include <netinet/in.h>
00030 class QSocketNotifier;
00031 
00032 #ifdef KSOCK_NO_BROKEN
00033 // This is here for compatibility with old applications still using the constants
00034 // Never use them in new programs
00035 
00036 // Here are a whole bunch of hackish macros that allow one to
00037 // get at the correct member of ksockaddr_in
00038 // But since ksockaddr_in is IPv4-only, and deprecated...
00039 
00040 typedef sockaddr_in ksockaddr_in;
00041 #define get_sin_addr(x) x.sin_addr
00042 #define get_sin_port(x) x.sin_port
00043 #define get_sin_family(x) x.sin_family
00044 #define get_sin_paddr(x) x->sin_addr
00045 #define get_sin_pport(x) x->sin_port
00046 #define get_sin_pfamily(x) x->sin_family
00047 #endif
00048 
00049 #define KSOCK_DEFAULT_DOMAIN PF_INET
00050 
00051 class KSocketPrivate;
00052 class KServerSocketPrivate;
00053 
00072 class KSocket : public QObject
00073 {
00074     Q_OBJECT
00075 public:
00080     KSocket( int _sock );
00087     KSocket( const char *_host, unsigned short int _port, int timeOut = 30);
00088 
00093     KSocket( const char * _path );
00094 
00098     virtual ~KSocket();
00099 
00104     int socket() const { return sock; }
00105 
00114     void enableRead( bool enable );
00115 
00127     void enableWrite( bool enable );
00128 
00129 #ifdef KSOCK_NO_BROKEN
00130     // BCI: remove in libkdecore.so.4
00138     unsigned long ipv4_addr();
00139 
00140     // BCI: remove in libkdecore.so.4
00146     static bool initSockaddr(ksockaddr_in *server_name, const char *hostname, unsigned short int port, int domain = PF_INET);
00147 #endif
00148 
00149 signals:
00157     void readEvent( KSocket *s );
00158 
00170     void writeEvent( KSocket *s );
00171 
00176     void closeEvent( KSocket *s );
00177 
00178 public slots:
00186     void slotWrite( int x);
00187 
00195     void slotRead( int x );
00196 
00197 protected:
00198     bool connect( const QString& _host, unsigned short int _port, int timeout = 0 );
00199     bool connect( const char *_path );
00200 
00201     /******************************************************
00202      * The file descriptor for this socket. sock may be -1.
00203      * This indicates that it is not connected.
00204      */
00205     int sock;
00206 
00207 private:
00208     KSocket(const KSocket&);
00209     KSocket& operator=(const KSocket&);
00210 
00211     KSocketPrivate *d;
00212 
00213 };
00214 
00215 
00232 class KServerSocket : public QObject
00233 {
00234     Q_OBJECT
00235 public:
00243     KServerSocket( unsigned short int _port, bool _bind = true );
00244 
00252     KServerSocket( const char *_path, bool _bind = true);
00253 
00257     virtual ~KServerSocket();
00258 
00265     bool bindAndListen();
00266 
00272     int socket() const { return sock; }
00273 
00278     unsigned short int port();
00279 
00280 #ifdef KSOCK_NO_BROKEN
00281     // BCI: remove in libkdecore.so.4
00288     unsigned long ipv4_addr();
00289 #endif
00290 
00291 public slots:
00295     virtual void slotAccept( int ); // why is this virtual?
00296 
00297 signals:
00307     void accepted( KSocket*s );
00308 
00309 protected:
00310     bool init( unsigned short int );
00311     bool init( const char *_path );
00312 
00317     int sock;
00318 
00319 private:
00320     KServerSocket(const KServerSocket&);
00321     KServerSocket& operator=(const KServerSocket&);
00322 
00323     KServerSocketPrivate *d;
00324 };
00325 
00326 
00327 #endif
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Mar 16 18:43:29 2005 by doxygen 1.3.3 written by Dimitri van Heesch, © 1997-2001