00001 //--------------------------------------------------------------------------- 00002 // net_socket 00003 //--------------------------------------------------------------------------- 00004 #ifndef __NET_SOCKET_H 00005 #define __NET_SOCKET_H 00006 00007 #include "net_stdafx.h" 00008 #include "net_host.h" 00009 #include "system.h" 00010 #include <string> 00011 00013 namespace Net{ 00014 using namespace std; 00015 00017 class ConnectionException: public System::Exception{ 00018 public: 00020 ConnectionException(const char* msg, ...); 00022 void show(); 00023 }; 00024 00026 #define NS_SOCKET_BUFFER_LEN 1024 00027 00028 class Socket{ 00029 protected: 00031 int sockId; 00032 00033 public: 00035 Socket(){}; 00037 00042 void get(char* buffer, int size); 00044 00049 void set(char* buffer, int size); 00051 00054 int getOnce(char* buffer, int size); 00056 void destroy(); 00057 //bool waitingData(); 00059 string peerName(); 00061 void operator=(Socket s){ sockId = s.sockId; }; 00062 }; 00063 00065 class ClientSocket: public Socket{ 00066 public: 00068 ClientSocket(){}; 00070 ClientSocket(Host host); // connect to host 00072 void operator=(ClientSocket s){ sockId = s.sockId; }; 00073 00074 }; 00075 00077 class AcceptedSocket: public Socket{ 00078 protected: 00080 Host host; 00081 public: 00083 AcceptedSocket() {}; 00085 AcceptedSocket(Host h,int fd): host(h) {sockId = fd;}; 00087 void operator=(AcceptedSocket s){ sockId = s.sockId; }; 00088 00089 }; 00090 00092 class ServerSocket: public Socket{ 00093 public: 00095 ServerSocket(){}; 00097 ServerSocket(unsigned int port); 00099 AcceptedSocket wait(); 00101 AcceptedSocket wait(Host &host); 00103 void operator=(ServerSocket s){ sockId = s.sockId; }; 00104 00105 }; 00106 00107 } // end of namespace 00108 00109 #endif // __NET_SOCKET_H