use spaces instead of tab
This commit is contained in:
+68
-68
@@ -24,98 +24,98 @@
|
||||
#include <cstdlib>
|
||||
|
||||
TCPSocket::TCPSocket(int sfd, struct sockaddr_in* address) : m_sfd(sfd) {
|
||||
char ip[17];
|
||||
inet_ntop(AF_INET, (struct in_addr*)&(address->sin_addr.s_addr), ip, (socklen_t)sizeof(ip)-1);
|
||||
m_ip = ip;
|
||||
m_port = (uint16_t)ntohs(address->sin_port);
|
||||
char ip[17];
|
||||
inet_ntop(AF_INET, (struct in_addr*)&(address->sin_addr.s_addr), ip, (socklen_t)sizeof(ip)-1);
|
||||
m_ip = ip;
|
||||
m_port = (uint16_t)ntohs(address->sin_port);
|
||||
}
|
||||
|
||||
bool TCPSocket::isValid() {
|
||||
return fcntl(m_sfd, F_GETFL) != -1;
|
||||
return fcntl(m_sfd, F_GETFL) != -1;
|
||||
}
|
||||
|
||||
|
||||
TCPSocket* TCPClient::connect(const string& server, const uint16_t& port) {
|
||||
struct sockaddr_in address;
|
||||
int ret;
|
||||
struct sockaddr_in address;
|
||||
int ret;
|
||||
|
||||
memset(reinterpret_cast<char*>(&address), 0, sizeof(address));
|
||||
memset(reinterpret_cast<char*>(&address), 0, sizeof(address));
|
||||
|
||||
if (inet_addr(server.c_str()) == INADDR_NONE) {
|
||||
struct hostent* he;
|
||||
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;
|
||||
}
|
||||
}
|
||||
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 = (in_port_t)htons(port);
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = (in_port_t)htons(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 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) {
|
||||
return 0;
|
||||
}
|
||||
m_lfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
struct sockaddr_in address;
|
||||
if (m_listening) {
|
||||
return 0;
|
||||
}
|
||||
m_lfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
struct sockaddr_in address;
|
||||
|
||||
memset(&address, 0, sizeof(address));
|
||||
memset(&address, 0, sizeof(address));
|
||||
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = (in_port_t)htons(m_port);
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = (in_port_t)htons(m_port);
|
||||
|
||||
if (m_address.size() > 0) {
|
||||
inet_pton(AF_INET, m_address.c_str(), &(address.sin_addr));
|
||||
} else {
|
||||
address.sin_addr.s_addr = INADDR_ANY;
|
||||
}
|
||||
int optval = 1;
|
||||
setsockopt(m_lfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
|
||||
if (m_address.size() > 0) {
|
||||
inet_pton(AF_INET, m_address.c_str(), &(address.sin_addr));
|
||||
} else {
|
||||
address.sin_addr.s_addr = INADDR_ANY;
|
||||
}
|
||||
int optval = 1;
|
||||
setsockopt(m_lfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
|
||||
|
||||
int result = bind(m_lfd, (struct sockaddr*) &address, sizeof(address));
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
result = listen(m_lfd, 5);
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
m_listening = true;
|
||||
return result;
|
||||
int result = bind(m_lfd, (struct sockaddr*) &address, sizeof(address));
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
result = listen(m_lfd, 5);
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
m_listening = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
TCPSocket* TCPServer::newSocket() {
|
||||
if (!m_listening) {
|
||||
return NULL;
|
||||
}
|
||||
struct sockaddr_in address;
|
||||
socklen_t len = sizeof(address);
|
||||
if (!m_listening) {
|
||||
return NULL;
|
||||
}
|
||||
struct sockaddr_in address;
|
||||
socklen_t len = sizeof(address);
|
||||
|
||||
memset(&address, 0, sizeof(address));
|
||||
memset(&address, 0, sizeof(address));
|
||||
|
||||
int sfd = accept(m_lfd, (struct sockaddr*) &address, &len);
|
||||
if (sfd < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return new TCPSocket(sfd, &address);
|
||||
int sfd = accept(m_lfd, (struct sockaddr*) &address, &len);
|
||||
if (sfd < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return new TCPSocket(sfd, &address);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user