Revision 35, 0.9 KB
(checked in by trnewman, 16 years ago)
|
adding c++
|
Rev | Line | |
---|
[35] | 1 | // Definition of the Socket class |
---|
| 2 | |
---|
| 3 | #ifndef Socket_class |
---|
| 4 | #define Socket_class |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | #include <sys/types.h> |
---|
| 8 | #include <sys/socket.h> |
---|
| 9 | #include <netinet/in.h> |
---|
| 10 | #include <netdb.h> |
---|
| 11 | #include <unistd.h> |
---|
| 12 | #include <string> |
---|
| 13 | #include <arpa/inet.h> |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | const int MAXHOSTNAME = 200; |
---|
| 17 | const int MAXCONNECTIONS = 5; |
---|
| 18 | const int MAXRECV = 500; |
---|
| 19 | //const int MSG_NOSIGNAL = 0; // defined by dgame |
---|
| 20 | |
---|
| 21 | class Socket |
---|
| 22 | { |
---|
| 23 | public: |
---|
| 24 | Socket(); |
---|
| 25 | virtual ~Socket(); |
---|
| 26 | |
---|
| 27 | // Server initialization |
---|
| 28 | bool create(); |
---|
| 29 | bool bind ( const int port ); |
---|
| 30 | bool listen() const; |
---|
| 31 | bool accept ( Socket& ) const; |
---|
| 32 | |
---|
| 33 | // Client initialization |
---|
| 34 | bool connect ( const std::string host, const int port ); |
---|
| 35 | |
---|
| 36 | // Data Transimission |
---|
| 37 | bool send ( const std::string ) const; |
---|
| 38 | int recv ( std::string& ) const; |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | void set_non_blocking ( const bool ); |
---|
| 42 | |
---|
| 43 | bool is_valid() const { return m_sock != -1; } |
---|
| 44 | |
---|
| 45 | private: |
---|
| 46 | |
---|
| 47 | int m_sock; |
---|
| 48 | sockaddr_in m_addr; |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | }; |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | #endif |
---|