📄 ksock.cpp
字号:
/* This file is part of the KDE libraries Copyright (C) 1997 Torben Weis (weis@kde.org) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*//* * $Id: ksock.cpp,v 1.1 2003/09/08 19:42:05 jasonk Exp $ * $Log: ksock.cpp,v $ * Revision 1.1 2003/09/08 19:42:05 jasonk * Addition of packages directory and associated files. * * Revision 1.1.1.1 2003/08/07 21:18:32 jasonk * Initial import of PIXIL into new cvs repository. * * Revision 1.1.1.1 2003/06/23 22:04:23 jasonk * * * Revision 1.1.1.1 2000/07/07 16:10:59 jasonk * Initial import of ViewML * * Revision 1.24.4.5 1999/08/23 09:16:04 waba * WABA: Don't kill the application upon problems. * * Revision 1.24.4.4 1999/07/09 21:16:41 porten * patch for bug #1336,1549 from Ingo <schneidi@informatik.tu-muenchen.de> * * Revision 1.24.4.3 1999/04/18 19:49:23 kulow * fixing bug reported by litsch.iep@t-online.de (Stephan Litsch) * * Revision 1.24.4.2 1999/04/01 20:43:44 pbrown * socket patch from Dirk A. Mueller <dmuell@gmx.net>, forwarded to kde-devel * by Torben, applied. * * Revision 1.24.4.1 1999/02/24 12:49:17 dfaure * getdtablesize() -> getrlimit(). Fixes #447 and removes a #ifdef HPUX. * * Revision 1.25 1999/02/24 12:47:34 dfaure * getdtablesize() -> getrlimit(). Fixes #447 and removes a #ifdef HPUX. * * Revision 1.24 1999/01/18 10:56:25 kulow * .moc files are back in kdelibs. Built fine here using automake 1.3 * * Revision 1.23 1999/01/15 09:30:42 kulow * it's official - kdelibs builds with srcdir != builddir. For this I * automocifized it, the generated rules are easier to maintain than * selfwritten rules. I have to fight with some bugs of this tool, but * generally it's better than keeping them updated by hand. * * Revision 1.22 1999/01/11 23:09:51 thufir * fix: fixes bug where it would loop n - tries, after a successful connect (would cause a pause after successful connect) * * Revision 1.21 1998/11/11 00:02:54 thufir * addes ability to set a connect Time Out, breaks binary compatability * * Revision 1.20 1998/08/02 14:49:31 kalle * ANother try at the socket problem. Hope that this works on _all_ platforms now. * * Revision 1.19 1998/07/29 12:39:17 kalle * Don't hardcode maximum of pending connections in listen(). Should work on all platforms including QNX. * * Revision 1.18 1998/02/20 02:37:25 torben * Torben: Fixes permissions * * Revision 1.17 1998/01/23 11:25:28 kulow * Solaris doesn't define UNIX_PATH_MAX * * Revision 1.16 1998/01/23 02:23:38 torben * Torben: Supports UNIX domain sockets now. * * Revision 1.15 1998/01/18 14:39:03 kulow * reverted the changes, Jacek commited. * Only the RCS comments were affected, but to keep them consistent, I * thought, it's better to revert them. * I checked twice, that only comments are affected ;) * * Revision 1.13 1997/12/18 01:56:24 torben * Torben: Secure string operations. Use instead of QString::sprintf * * Revision 1.12 1997/11/29 17:58:48 kalle * Alpha patches * * Revision 1.11 1997/11/09 01:52:47 torben * Torben: Fixed port number bug * * Revision 1.10 1997/10/21 20:44:52 kulow * removed all NULLs and replaced it with 0L or "". * There are some left in mediatool, but this is not C++ * * Revision 1.9 1997/10/16 11:15:03 torben * Kalle: Copyright headers * kdoctoolbar removed * * Revision 1.8 1997/09/18 12:16:04 kulow * corrected some header dependencies. Removed most of them in drag.h and put * them in drag.cpp. Now it should compile even under SunOS 4.4.1 ;) * * Revision 1.7 1997/08/30 08:32:54 kdecvs * Coolo: changed the location of the include files to get rid of the * hardcoded HAVE_STDC_HEADERS * * Revision 1.6 1997/07/27 13:43:58 kalle * Even more SGI and SCC patches, security patch for kapp, various fixes for ksock * * Revision 1.5 1997/07/17 18:43:18 kalle * Kalle: new stopsign.xpm * KConfig: environment variables are resolved in readEntry() * (meaning you can write() an entry with an environment * variable and read it back in, and the value gets properly * expanded). * Fixed three bugs in ksock.cpp * Added KFloater * Added KCombo * Added KLineEdit * New KToolbar * New KToplevelWidget * * Revision 1.4 1997/05/30 20:04:38 kalle * Kalle: * 30.05.97: signal handler for reaping zombie help processes reinstalls itself * patch to KIconLoader by Christian Esken * slightly better look for KTabCtl * kdecore Makefile does not expect current dir to be in path * Better Alpha support * * Revision 1.3 1997/05/09 15:10:11 kulow * Coolo: patched ltconfig for FreeBSD * removed some stupid warnings * * Revision 1.2 1997/04/28 06:57:46 kalle * Various widgets moved from apps to libs * Added KSeparator * Several bugs fixed * Patches from Matthias Ettrich * Made ksock.cpp more alpha-friendly * Removed XPM-Loading from KPixmap * Reaping zombie KDEHelp childs * WidgetStyle of KApplication objects configurable via kdisplay */#include <qapplication.h>#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdio.h>#include <errno.h>#ifdef STDC_HEADERS#include <stdlib.h>#include <string.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#ifdef HAVE_SYSENT_H#include <sysent.h>#endif#include <sys/types.h>#include <sys/stat.h>#if TIME_WITH_SYS_TIME# include <sys/time.h># include <time.h>#else# if HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#ifdef HAVE_SYS_SOCKET_H// on Linux/libc5, this includes linux/socket.h where SOMAXCONN is defined#include <sys/socket.h>#endif// Play it safe, use a reasonable default, if SOMAXCONN was nowhere defined.#ifndef SOMAXCONN#warning Your header files do not seem to support SOMAXCONN#define SOMAXCONN 5#endif#include <sys/resource.h>#include <sys/time.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include <fcntl.h>#include <signal.h>#include "ksock.h"#ifndef UNIX_PATH_MAX#define UNIX_PATH_MAX 108 // this is the value, I found under Linux#endifKSocket::KSocket( const char *_host, unsigned short int _port, int _timeout ) : sock( -1 ), readNotifier( 0L ), writeNotifier( 0L ){ timeOut = _timeout; domain = PF_INET; connect( _host, _port );}KSocket::KSocket( const char *_host, unsigned short int _port ) : sock( -1 ), readNotifier( 0L ), writeNotifier( 0L ){ timeOut = 30; domain = PF_INET; connect( _host, _port );}KSocket::KSocket( const char *_path ) : sock( -1 ), readNotifier( 0L ), writeNotifier( 0L ){ domain = PF_UNIX; connect( _path );}void KSocket::enableRead( bool _state ){ if ( _state ) { if ( !readNotifier ) { readNotifier = new QSocketNotifier( sock, QSocketNotifier::Read ); QObject::connect( readNotifier, SIGNAL( activated(int) ), this, SLOT( slotRead(int) ) ); } else readNotifier->setEnabled( true ); } else if ( readNotifier ) readNotifier->setEnabled( false );}void KSocket::enableWrite( bool _state ){ if ( _state ) { if ( !writeNotifier ) { writeNotifier = new QSocketNotifier( sock, QSocketNotifier::Write ); QObject::connect( writeNotifier, SIGNAL( activated(int) ), this, SLOT( slotWrite(int) ) ); } else writeNotifier->setEnabled( true ); } else if ( writeNotifier ) writeNotifier->setEnabled( false );}void KSocket::slotRead( int ){ char buffer[2]; int n = recv( sock, buffer, 1, MSG_PEEK ); if ( n <= 0 ) emit closeEvent( this ); else emit readEvent( this );}void KSocket::slotWrite( int ){ emit writeEvent( this );}/* * Initializes a sockaddr structure. Do this after creating a socket and * before connecting to any other socket. Here you must specify the * host and port you want to connect to. */bool KSocket::init_sockaddr( const char *hostname, unsigned short int port ){ if ( domain != PF_INET ) return false; struct hostent *hostinfo; server_name.sin_family = AF_INET; server_name.sin_port = htons( port ); hostinfo = gethostbyname( hostname ); if ( !hostinfo ) { warning("Unknown host %s.\n",hostname); return false; } server_name.sin_addr = *(struct in_addr*) hostinfo->h_addr; return true;}/* * Connects the PF_UNIX domain socket to _path. */bool KSocket::connect( const char *_path ){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -