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