1 | #include "Socket.h" |
---|
2 | |
---|
3 | #include <sys/types.h> |
---|
4 | #include <iostream> |
---|
5 | #include <unistd.h> |
---|
6 | |
---|
7 | #ifdef WIN32 |
---|
8 | #include <WinSock2.h> |
---|
9 | #define socklen_t int |
---|
10 | #else |
---|
11 | #include <sys/socket.h> |
---|
12 | #include <sys/errno.h> |
---|
13 | #include <netinet/in.h> |
---|
14 | #include <netdb.h> |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifdef WIN32 |
---|
18 | int Socket::m_nbSockets = 0; |
---|
19 | #endif |
---|
20 | |
---|
21 | Socket::Socket() throw (std::string) { |
---|
22 | |
---|
23 | #ifdef WIN32 |
---|
24 | if(m_nbSockets++ == 0) { |
---|
25 | WSADATA info; |
---|
26 | if (WSAStartup(MAKEWORD(2,0), &info)) { |
---|
27 | std::string err_msg("Could not start WSA"); |
---|
28 | throw err_msg; |
---|
29 | } |
---|
30 | } |
---|
31 | #endif |
---|
32 | |
---|
33 | m_socket = 0; |
---|
34 | |
---|
35 | int s = socket(AF_INET, SOCK_STREAM, 0); |
---|
36 | if (s == -1) { |
---|
37 | std::string err_msg("Unable to create socket"); |
---|
38 | throw err_msg; |
---|
39 | } |
---|
40 | |
---|
41 | m_socket = s; |
---|
42 | } |
---|
43 | |
---|
44 | Socket::Socket(const int s) { |
---|
45 | m_socket = s; |
---|
46 | } |
---|
47 | |
---|
48 | Socket::~Socket() { |
---|
49 | if (m_socket) |
---|
50 | close(m_socket); |
---|
51 | |
---|
52 | #ifdef WIN32 |
---|
53 | if (--m_nbSockets == 0) { |
---|
54 | WSACleanup(); |
---|
55 | } |
---|
56 | #endif |
---|
57 | } |
---|
58 | |
---|
59 | int Socket::Send(const char *data, const int length) throw (std::string) { |
---|
60 | int nb_sent = send(m_socket, data, length, 0); |
---|
61 | //std::clog << "Socket::Send: " << nb_sent << std::endl; |
---|
62 | |
---|
63 | if (nb_sent == -1) { |
---|
64 | std::string err_msg("Unable to send data"); |
---|
65 | throw err_msg; |
---|
66 | } |
---|
67 | |
---|
68 | return nb_sent; |
---|
69 | } |
---|
70 | |
---|
71 | int Socket::Receive(const ssize_t buf_len, char * buf) throw (std::string) { |
---|
72 | |
---|
73 | ssize_t nb_received = recv(m_socket, buf, buf_len, 0); |
---|
74 | |
---|
75 | if (nb_received == -1) { |
---|
76 | std::string err_msg("Unable to receive data"); |
---|
77 | throw err_msg; |
---|
78 | } |
---|
79 | |
---|
80 | return nb_received; |
---|
81 | } |
---|
82 | |
---|
83 | ////////////////////////////// |
---|
84 | SocketClient::SocketClient(const std::string &host, const int port) throw (std::string) |
---|
85 | :Socket() |
---|
86 | { |
---|
87 | //std::clog << "SocketClient::SocketClient: " << host << " - " << port << std::endl; |
---|
88 | |
---|
89 | std::string err_msg; |
---|
90 | sockaddr_in sa; |
---|
91 | int err; |
---|
92 | |
---|
93 | struct hostent *hs; |
---|
94 | hs = gethostbyname(host.c_str()); |
---|
95 | |
---|
96 | if (!hs) { |
---|
97 | std::cerr << "SocketClient::SocketClient: gethostbyname failed" << std::endl; |
---|
98 | err_msg = strerror(errno); |
---|
99 | throw err_msg; |
---|
100 | } |
---|
101 | |
---|
102 | sa.sin_family = AF_INET; |
---|
103 | sa.sin_port = htons(port); |
---|
104 | sa.sin_addr = *((in_addr *)hs->h_addr); |
---|
105 | memset(&(sa.sin_zero), 0, 8); |
---|
106 | |
---|
107 | |
---|
108 | err = connect(m_socket, (sockaddr *) &sa, sizeof(sockaddr)); |
---|
109 | |
---|
110 | if (err == -1) { |
---|
111 | std::cerr << "SocketClient::SocketClient: connect failed" << std::endl; |
---|
112 | err_msg = strerror(errno); |
---|
113 | throw err_msg; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | ////////////////////////////// |
---|
118 | SocketServer::SocketServer(const int port) throw (std::string) |
---|
119 | :Socket() |
---|
120 | { |
---|
121 | std::string err_msg; |
---|
122 | sockaddr_in sa; |
---|
123 | int err; |
---|
124 | |
---|
125 | memset(&sa, 0, sizeof(sockaddr_in)); |
---|
126 | sa.sin_family = AF_INET; |
---|
127 | sa.sin_port = htons(port); |
---|
128 | |
---|
129 | err = bind(m_socket, (sockaddr *)&sa, sizeof(sockaddr_in)); |
---|
130 | if (err == -1) { |
---|
131 | err_msg = strerror(errno); |
---|
132 | throw err_msg; |
---|
133 | } |
---|
134 | |
---|
135 | err = listen(m_socket, MAX_CLIENTS); |
---|
136 | if (err == -1) { |
---|
137 | std::string err_msg("Unable to receive data"); |
---|
138 | throw err_msg; |
---|
139 | } |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | Socket* SocketServer::Accept() throw (std::string) { |
---|
144 | std::string err_msg; |
---|
145 | sockaddr_in sa; |
---|
146 | socklen_t sa_size = (socklen_t)sizeof(sockaddr_in); |
---|
147 | int client_socket; |
---|
148 | |
---|
149 | client_socket = accept(m_socket, (sockaddr *)&sa, &sa_size); |
---|
150 | |
---|
151 | if (client_socket == -1) { |
---|
152 | err_msg = "Accept failed"; |
---|
153 | throw err_msg; |
---|
154 | } |
---|
155 | |
---|
156 | std::cerr << "Connection from " << sa.sin_addr.s_addr << std::endl; |
---|
157 | |
---|
158 | return new Socket(client_socket); |
---|
159 | } |
---|