00001 #ifndef _SOCKETS_H_
00002 #define _SOCKETS_H_
00003
00004 #include <string>
00005 #include <list>
00006
00007 using namespace std;
00008
00009 class TCP_Client {
00010 public:
00011 TCP_Client(bool enable_exception);
00012 ~TCP_Client();
00013 void connect(string ip, string port);
00014 void disconnect(void);
00015 void send(const string *data, int opt = 0);
00016 void send(const string data, int opt = 0);
00017 void send(void *data, int len, int opt = 0);
00018 void recv(string &r, int opt = 0);
00019 protected:
00020 bool ex;
00021 int fd;
00022 char *buf;
00023 };
00024
00025 class Socket {
00026 public:
00027 enum stype {TYPE_TCP, TYPE_UDP};
00028 enum state {STATE_EMPTY, STATE_IN, STATE_DISCONNECT};
00029
00030 Socket(string ip, int port, stype _t = TYPE_TCP);
00031 ~Socket();
00032
00033 bool recv(string &rez);
00034 bool send(void *data, int len);
00035 bool send(const string *data);
00036 bool send(const string data);
00037 bool send2v(void **v_ptr, int *v_len);
00038 bool send3v(void **v_ptr, int *v_len);
00039
00040 static int poll(list<Socket *> &s);
00041 void listen(int in);
00042 Socket *accept(void);
00043
00044
00045 inline state state_refresh(void) {
00046 state st = _state;
00047 _state = STATE_EMPTY;
00048 return st;
00049 }
00050 int get_fd(void) { return fd;};
00051 int fd;
00052 string source_ip(void) {
00053 return ip;
00054 }
00055 protected:
00056 Socket(int _fd, string _ip, int _port, stype _t);
00057
00058 string ip;
00059 int port;
00060 stype type;
00061
00062 state _state;
00063
00064 int ttl;
00065 unsigned short ip_id;
00066 bool _is_multicast(string ip);
00067 unsigned long ip_to_number(string ip);
00068 bool is_multicast;
00069
00070
00071
00072
00073
00074
00075 };
00076
00077 #endif