00001 // LICENSE: (Please see the file COPYING for details) 00002 // 00003 // NUS - Nemesis Utilities System: A C++ application development framework 00004 // Copyright (C) 2006, 2008 Otavio Rodolfo Piske 00005 // 00006 // This file is part of NUS 00007 // 00008 // This library is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU Lesser General Public 00010 // License as published by the Free Software Foundation version 2.1 00011 // of the License. 00012 // 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 // Lesser General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU Lesser General Public 00019 // License along with this library; if not, write to the Free Software 00020 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 // 00022 #ifndef NTCPSOCKET_H 00023 #define NTCPSOCKET_H 00024 00033 #include "ndefs.h" 00034 #include "ndatastream.h" 00035 #include "nstring.h" 00036 #include "nabstractsocket.h" 00037 00038 #include <unistd.h> 00039 #include <sys/types.h> 00040 #include <sys/socket.h> 00041 00042 #if defined(__CYGWIN__) 00043 #define MSG_DONTWAIT 0 00044 #endif // defined(__CYGWIN__) 00045 00050 class NTcpSocket: public NAbstractSocket { 00051 public: 00055 NTcpSocket(void); 00056 00061 NTcpSocket(NHostAddress &host); 00062 00068 NTcpSocket(const NString &host, nuint16 port); 00069 00073 ~NTcpSocket(void); 00074 00079 void setPort(nint16 port); 00080 void setSocketOptions(nint32 opt); 00081 00086 NHostAddress peerAddress(void) const; 00087 00092 NString peerName(void) const; 00093 00098 nuint16 peerPort(void) const; 00099 00100 void open(void); 00101 void closeDevice(void); 00102 00103 void disconnectFromHost(void); 00104 void connectToHost(); 00105 void execListen(void); 00106 00112 NTcpSocket *waitForConnected(void); 00113 00114 nint32 read(NDataStream *buffer); 00115 nint32 read(NDataStream *buffer, nuint32 bytes = 0); 00116 nint32 readLine(NDataStream *buffer); 00117 nint32 write(const NDataStream &data, nuint32 bytes = 0); 00118 nint32 writeLine(const NDataStream &data, nuint32 bytes = 0); 00119 00120 private: 00121 struct sockaddr_in m_addr; 00122 int m_sock; 00123 00124 NTcpSocket(nuint16 timeout, nint32 sock, struct sockaddr_in client); 00125 00126 void init(void); 00127 void execBind(void); 00128 00129 void setPeer(struct sockaddr_in addr); 00130 void setSock(int sock); 00131 void setupNewSocket(NTcpSocket *sock, struct sockaddr_in addr); 00132 }; 00133 00134 #endif // NTCPSOCKET_H