ebusd_send added.
TCPListener renamed to TCPServer. TCPClient added.
This commit is contained in:
+44
-3
@@ -18,8 +18,14 @@
|
||||
*/
|
||||
|
||||
#include "tcpsocket.h"
|
||||
#include <cstdlib>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <string.h>
|
||||
|
||||
TCPSocket::TCPSocket(int sfd, struct sockaddr_in* address) : m_sfd(sfd)
|
||||
@@ -39,7 +45,43 @@ bool TCPSocket::isValid()
|
||||
}
|
||||
|
||||
|
||||
int TCPListener::start()
|
||||
TCPSocket* TCPClient::connect(const std::string& server, const int& port)
|
||||
{
|
||||
struct sockaddr_in address;
|
||||
int ret;
|
||||
|
||||
memset((char*) &address, 0, sizeof(address));
|
||||
|
||||
if (inet_addr(server.c_str()) == INADDR_NONE) {
|
||||
struct hostent* he;
|
||||
|
||||
he = gethostbyname(server.c_str());
|
||||
if (he == NULL)
|
||||
return NULL;
|
||||
|
||||
memcpy(&address.sin_addr, he->h_addr_list[0], he->h_length);
|
||||
} else {
|
||||
ret = inet_aton(server.c_str(), &address.sin_addr);
|
||||
if (ret == 0)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = port;
|
||||
|
||||
int sfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sfd < 0)
|
||||
return NULL;
|
||||
|
||||
ret = ::connect(sfd, (struct sockaddr*) &address, sizeof(address));
|
||||
if (ret < 0)
|
||||
return NULL;
|
||||
|
||||
return new TCPSocket(sfd, &address);
|
||||
}
|
||||
|
||||
|
||||
int TCPServer::start()
|
||||
{
|
||||
if (m_listening == true)
|
||||
return 0;
|
||||
@@ -72,7 +114,7 @@ int TCPListener::start()
|
||||
return result;
|
||||
}
|
||||
|
||||
TCPSocket* TCPListener::newSocket()
|
||||
TCPSocket* TCPServer::newSocket()
|
||||
{
|
||||
if (m_listening == false)
|
||||
return NULL;
|
||||
@@ -83,7 +125,6 @@ TCPSocket* TCPListener::newSocket()
|
||||
memset(&address, 0, sizeof(address));
|
||||
|
||||
int sfd = accept(m_lfd, (struct sockaddr*) &address, &len);
|
||||
|
||||
if (sfd < 0)
|
||||
return NULL;
|
||||
|
||||
|
||||
+16
-5
@@ -29,7 +29,8 @@ class TCPSocket
|
||||
{
|
||||
|
||||
public:
|
||||
friend class TCPListener;
|
||||
friend class TCPClient;
|
||||
friend class TCPServer;
|
||||
|
||||
~TCPSocket() { close(m_sfd); }
|
||||
|
||||
@@ -47,18 +48,28 @@ private:
|
||||
int m_port;
|
||||
std::string m_ip;
|
||||
|
||||
TCPSocket(int sd, struct sockaddr_in* address);
|
||||
TCPSocket(int sfd, struct sockaddr_in* address);
|
||||
|
||||
};
|
||||
|
||||
class TCPListener
|
||||
class TCPClient
|
||||
{
|
||||
|
||||
public:
|
||||
TCPListener(int port, std::string address)
|
||||
TCPSocket* connect(const std::string& server, const int& port);
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
class TCPServer
|
||||
{
|
||||
|
||||
public:
|
||||
TCPServer(const int port, const std::string address)
|
||||
: m_lfd(0), m_port(port), m_address(address), m_listening(false) {}
|
||||
|
||||
~TCPListener() { if (m_lfd > 0) {close(m_lfd);} }
|
||||
~TCPServer() { if (m_lfd > 0) {close(m_lfd);} }
|
||||
|
||||
int start();
|
||||
TCPSocket* newSocket();
|
||||
|
||||
Reference in New Issue
Block a user