本文整理汇总了C++中TcpSocket::Close方法的典型用法代码示例。如果您正苦于以下问题:C++ TcpSocket::Close方法的具体用法?C++ TcpSocket::Close怎么用?C++ TcpSocket::Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TcpSocket
的用法示例。
在下文中一共展示了TcpSocket::Close方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Accept
bool Accept(TcpSocket& clientSocket) const
{
clientSocket.Close();
InetAddr& clientAddr = clientSocket.Addr();
int connFd = accept(m_Fd, (struct sockaddr*)&clientAddr.m_Addr, &clientAddr.m_AddrLen);
clientSocket.m_Fd = connFd;
return connFd != -1;
}
示例2: AcceptClients
void Server::AcceptClients()
{
TcpSocket c;
Address from;
if(c.Accept(server, from, false))
{
std::wstring name = c.GetPeer().ToString(false);
// Check password.
Packet p;
if(c.Receive(&p, sizeof(p), 1000) == sizeof(p) && (p.Control == C_CONNECT || p.Control == C_RESUME))
{
if(password == 0 || password == ntohl(p.Password))
{
// Check if a client is being booted by the new client.
if(client.IsValid())
{
std::wstring name = client.GetPeer().ToString(false);
client.Close();
if(p.Control != C_RESUME)
Log(OL_INFO, L"Replacing client %s\r\n", name.c_str());
}
if(p.Control == C_CONNECT)
Log(OL_NOTIFY | OL_INFO, L"Client connected from %s\r\n", name.c_str());
else
Log(OL_INFO, L"Client resumed\r\n");
p.Control = C_CONNECT;
c.Send(&p, sizeof(p));
client.Take(c);
}
else
{
p.Control = C_DISCONNECT;
p.Reason = 1;
Log(OL_INFO, L"Rejected client %s: Bad password\r\n", name.c_str());
}
}
else
{
p.Control = C_DISCONNECT;
p.Reason = 0;
Log(OL_INFO, L"Client failed to connect from %s\r\n", name.c_str());
}
if(p.Control == C_DISCONNECT)
{
c.Send(&p, sizeof(p));
c.Close();
}
}
}
示例3: Accept
bool Accept(TcpSocket& connection, bool blocking = true) const
{
if (!IsListening())
{
throw std::runtime_error("TcpSocket.Accept - Called on a socket that does not have listening mode enabled");
}
auto newSocket = SOCKET(0);
auto address = sockaddr_in();
auto tryAgain = bool();
do
{
tryAgain = false;
if (!Winsock::Accept(socket, address, newSocket))
{
switch (WSAGetLastError())
{
case WSAEWOULDBLOCK:
//if (!IsBlocking())
//{
// throw std::runtime_error("TcpSocket.Accept - Received would-block error for non-blocking socket");
//}
return false;
case WSAECONNRESET:
tryAgain = true;
break;
default:
return false;
}
}
} while (tryAgain);
if (connection.IsOpen())
{
connection.Close();
}
connection.socket = newSocket;
connection.isListening = false;
if (!Winsock::IoctlSocket(connection.socket, blocking))
{
throw std::runtime_error("TcpSocket.Accept - Unable to set blocking mode on new connection");
}
connection.isBlocking = blocking;
return true;
}
示例4: testPause
/**
* Test that pausing a connection works.
*/
void AdvancedTCPConnectorTest::testPause() {
ola::network::TCPSocketFactory socket_factory(
ola::NewCallback(this, &AdvancedTCPConnectorTest::AcceptedConnection));
TcpAcceptingSocket listening_socket(&socket_factory);
SetupListeningSocket(&listening_socket);
AdvancedTCPConnector connector(
m_ss,
m_tcp_socket_factory.get(),
TimeInterval(0, CONNECT_TIMEOUT_IN_MS * 1000));
// 5 per attempt, up to a max of 30
LinearBackoffPolicy policy(TimeInterval(5, 0), TimeInterval(30, 0));
// add endpoint, but make sure it's paused
connector.AddEndpoint(m_localhost, SERVER_PORT, &policy, true);
CPPUNIT_ASSERT_EQUAL(1u, connector.EndpointCount());
ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT,
AdvancedTCPConnector::PAUSED, 0);
m_ss->RunOnce(0, 500000);
// now unpause
connector.Resume(m_localhost, SERVER_PORT);
ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT,
AdvancedTCPConnector::DISCONNECTED, 0);
m_ss->Run();
CPPUNIT_ASSERT_EQUAL(1u, connector.EndpointCount());
ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT,
AdvancedTCPConnector::CONNECTED, 0);
// check our socket exists
CPPUNIT_ASSERT(m_connected_socket);
m_connected_socket->Close();
delete m_connected_socket;
OLA_INFO << "disconnecting";
connector.Disconnect(m_localhost, SERVER_PORT, true);
// state should be updated
ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT,
AdvancedTCPConnector::PAUSED, 0);
// clean up
connector.RemoveEndpoint(m_localhost, SERVER_PORT);
CPPUNIT_ASSERT_EQUAL(0u, connector.EndpointCount());
m_ss->RemoveReadDescriptor(&listening_socket);
}
示例5: Server
static void Server(String r)
{
TcpSocket server;
if(server.Listen(4000, 10)) {
TcpSocket socket;
LOG("Waiting...");
bool b = socket.Accept(server);
if(b) {
LOG("Connection accepted");
HttpHeader http;
http.Read(socket);
socket.Put(r);
socket.Close();
}
}
}
示例6: Accept
Socket::Status TcpListener::Accept(TcpSocket& socket)
{
if (GetHandle() == be::SocketBE::invalidSocket())
{
return Error;
}
sockaddr_in address;
be::SocketBE::AddrLength length = sizeof(address);
SocketHandle remote = ::accept(GetHandle(), reinterpret_cast<sockaddr*>(&address), &length);
if (remote == be::SocketBE::invalidSocket())
return be::SocketBE::getErrorStatus();
socket.Close();
socket.Create(remote);
return Done;
}
示例7: Accept
Socket::Status TcpListener::Accept(TcpSocket& socket)
{
// Make sure that we're listening
if (GetHandle() == priv::SocketImpl::InvalidSocket())
{
Err() << "Failed to accept a new connection, the socket is not listening" << std::endl;
return Error;
}
// Accept a new connection
sockaddr_in address;
priv::SocketImpl::AddrLength length = sizeof(address);
SocketHandle remote = accept(GetHandle(), reinterpret_cast<sockaddr*>(&address), &length);
// Check for errors
if (remote == priv::SocketImpl::InvalidSocket())
return priv::SocketImpl::GetErrorStatus();
// Initialize the new connected socket
socket.Close();
socket.Create(remote);
return Done;
}