1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * Definition of the Socket class |
---|
4 | * |
---|
5 | * ====================================================================== |
---|
6 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
7 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
8 | * |
---|
9 | * Copyright (c) 2004-2006 Purdue Research Foundation |
---|
10 | * |
---|
11 | * See the file "license.terms" for information on usage and |
---|
12 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
13 | * ====================================================================== |
---|
14 | */ |
---|
15 | #ifndef _SOCKET_H_ |
---|
16 | #define _SOCKET_H_ |
---|
17 | |
---|
18 | #include <errno.h> |
---|
19 | #include <stdlib.h> |
---|
20 | #include <stdio.h> |
---|
21 | #include <stdarg.h> |
---|
22 | #include <sys/types.h> |
---|
23 | #include <sys/socket.h> |
---|
24 | #include <netinet/in.h> |
---|
25 | #include <netdb.h> |
---|
26 | #include <unistd.h> |
---|
27 | #include <string> |
---|
28 | #include <arpa/inet.h> |
---|
29 | #include <iostream> |
---|
30 | |
---|
31 | |
---|
32 | const int MAXHOSTNAME = 200; |
---|
33 | const int MAXCONNECTIONS = 5; |
---|
34 | //const int MAXRECV =500; |
---|
35 | //const int MAXRECV = 512*512*4*sizeof(float)+1; |
---|
36 | const int MAXRECV = 512*512*4+1; |
---|
37 | |
---|
38 | void error(int status, int err, const char *fmt, ... ); |
---|
39 | void set_address(char *hname, char *sname, struct sockaddr_in *sap, char *protocol); |
---|
40 | void parse_GET_string(char *_str, char keys[256][256], char values[256][256], int *count); |
---|
41 | |
---|
42 | ssize_t readn(int fd, void *vptr, size_t n); |
---|
43 | ssize_t writen(int fd, void *vptr, size_t n); |
---|
44 | |
---|
45 | class Socket |
---|
46 | { |
---|
47 | public: |
---|
48 | Socket(); |
---|
49 | virtual ~Socket(); |
---|
50 | |
---|
51 | // Server initialization |
---|
52 | bool create(); |
---|
53 | bool bind ( const int port ); |
---|
54 | bool listen() const; |
---|
55 | bool accept ( Socket& ) const; |
---|
56 | |
---|
57 | // Client initialization |
---|
58 | bool connect ( const std::string host, const int port ); |
---|
59 | |
---|
60 | // Data Transimission |
---|
61 | bool send ( const std::string ) const; |
---|
62 | bool send (char* s, int size) const; |
---|
63 | int recv ( std::string& ) const; |
---|
64 | int recv ( char* s, int size) const; |
---|
65 | |
---|
66 | void set_non_blocking ( const bool ); |
---|
67 | |
---|
68 | bool is_valid() const { return m_sock != -1; } |
---|
69 | |
---|
70 | int m_sock; |
---|
71 | |
---|
72 | private: |
---|
73 | sockaddr_in m_addr; |
---|
74 | }; |
---|
75 | |
---|
76 | |
---|
77 | |
---|
78 | #endif |
---|