}
explicit FreeBSDSocket(int socket): socket_(socket) { }
-
- virtual ~FreeBSDSocket() {
- if (IsValid()) {
- // Close socket.
- close(socket_);
- }
- }
+ virtual ~FreeBSDSocket() { Close(); }
// Server initialization.
bool Bind(const int port);
// Client initialization.
bool Connect(const char* host, const char* port);
+ // Close.
+ bool Close();
+
// Data Transimission
int Send(const char* data, int len) const;
int Receive(char* data, int len) const;
}
+bool FreeBSDSocket::Close() {
+ if (IsValid()) {
+ // Close socket.
+ int status = close(socket_);
+ socket_ = -1;
+ return status == 0;
+ }
+ return true;
+}
+
int FreeBSDSocket::Send(const char* data, int len) const {
int status = send(socket_, data, len, 0);
return status;
}
explicit LinuxSocket(int socket): socket_(socket) { }
-
- virtual ~LinuxSocket() {
- if (IsValid()) {
- // Close socket.
- close(socket_);
- }
- }
+ virtual ~LinuxSocket() { Close(); }
// Server initialization.
bool Bind(const int port);
// Client initialization.
bool Connect(const char* host, const char* port);
+ // Close.
+ bool Close();
+
// Data Transimission
int Send(const char* data, int len) const;
int Receive(char* data, int len) const;
}
+bool LinuxSocket::Close() {
+ if (IsValid()) {
+ // Close socket.
+ int status = close(socket_);
+ socket_ = -1;
+ return status == 0;
+ }
+ return true;
+}
+
+
int LinuxSocket::Send(const char* data, int len) const {
int status = send(socket_, data, len, 0);
return status;
}
explicit MacOSSocket(int socket): socket_(socket) { }
-
- virtual ~MacOSSocket() {
- if (IsValid()) {
- // Close socket.
- close(socket_);
- }
- }
+ virtual ~MacOSSocket() { Close(); }
// Server initialization.
bool Bind(const int port);
// Client initialization.
bool Connect(const char* host, const char* port);
+ // Close.
+ bool Close();
+
// Data Transimission
int Send(const char* data, int len) const;
int Receive(char* data, int len) const;
}
+bool MacOSSocket::Close() {
+ if (IsValid()) {
+ // Close socket.
+ int status = close(socket_);
+ socket_ = -1;
+ return status == 0;
+ }
+ return true;
+}
+
+
int MacOSSocket::Send(const char* data, int len) const {
int status = send(socket_, data, len, 0);
return status;
explicit Win32Socket(SOCKET socket): socket_(socket) { }
- virtual ~Win32Socket() {
- if (IsValid()) {
- // Close socket.
- closesocket(socket_);
- }
- }
+ virtual ~Win32Socket() { Close(); }
// Server initialization.
bool Bind(const int port);
// Client initialization.
bool Connect(const char* host, const char* port);
+ // Close.
+ bool Close();
+
// Data Transimission
int Send(const char* data, int len) const;
int Receive(char* data, int len) const;
}
+bool Win32Socket::Close() {
+ if (IsValid()) {
+ // Close socket.
+ int rc = closesocket(socket_);
+ socket_ = INVALID_SOCKET;
+ return rc != SOCKET_ERROR;
+ }
+ return true;
+}
+
+
int Win32Socket::Send(const char* data, int len) const {
int status = send(socket_, data, len, 0);
return status;
// Client initialization.
virtual bool Connect(const char* host, const char* port) = 0;
+ // Close.
+ virtual bool Close() = 0;
+
// Data Transimission
virtual int Send(const char* data, int len) const = 0;
virtual int Receive(char* data, int len) const = 0;
}
// Close the client before the listener to avoid TIME_WAIT issues.
+ client->Close();
delete client;
delete listener;
}