00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <cc++/socket.h>
00042 #include <iostream>
00043 #include <cstdlib>
00044
00045 #ifdef CCXX_NAMESPACES
00046 using namespace std;
00047 using namespace ost;
00048 #endif
00049
00050 class myTCPSocket : public TCPSocket
00051 {
00052 protected:
00053 bool onAccept(const InetHostAddress &ia, tpport_t port);
00054
00055 public:
00056 myTCPSocket(InetAddress &ia);
00057 };
00058
00059 myTCPSocket::myTCPSocket(InetAddress &ia) : TCPSocket(ia, 4096)
00060 {
00061 cout << "binding segsize: " << getSegmentSize() << endl;
00062 }
00063
00064 bool myTCPSocket::onAccept(const InetHostAddress &ia, tpport_t port)
00065 {
00066 cout << "accepting from: " << ia << ":" << port << endl;;
00067 return true;
00068 }
00069
00070 int main(int argc, char *argv[])
00071 {
00072 tpport_t port;
00073 int i;
00074 TCPStream tcp;
00075 InetAddress addr;
00076 addr = "255.255.255.255";
00077 cout << "testing addr: " << addr << ":" << 4096 << endl;
00078
00079 addr = "127.0.0.1";
00080 cout << "binding for: " << addr << ":" << 4096 << endl;
00081
00082 Thread::setException(Thread::throwException);
00083
00084 try
00085 {
00086 myTCPSocket server(addr);
00087 while(server.isPendingConnection(30000))
00088 {
00089 tcp.connect(server);
00090
00091
00092 tcp << "welcome to " << addr << "; segment size=" << tcp.getSegmentSize() << endl;
00093 tcp << "connected from " << tcp.getPeer(&port) << endl;
00094
00095 if(tcp.isPending(Socket::pendingInput, 2000))
00096 {
00097 tcp >> i;
00098 tcp << "user entered " << i << endl;
00099 }
00100 tcp << "exiting now" << endl;
00101 tcp.disconnect();
00102 }
00103 }
00104 catch(SockException& e)
00105 {
00106 cout << e.getString() << ": " << e.getSystemErrorString() << endl;
00107 exit(-1);
00108 }
00109 cout << "timeout after 30 seconds inactivity, exiting" << endl;
00110 return 0;
00111 }
00112