当前位置: 首页>>代码示例>>C++>>正文


C++ TCPSocket::Close方法代码示例

本文整理汇总了C++中TCPSocket::Close方法的典型用法代码示例。如果您正苦于以下问题:C++ TCPSocket::Close方法的具体用法?C++ TCPSocket::Close怎么用?C++ TCPSocket::Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TCPSocket的用法示例。


在下文中一共展示了TCPSocket::Close方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: 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_server_address, &policy, true);
  OLA_ASSERT_EQ(1u, connector.EndpointCount());

  ConfirmState(__LINE__, connector, m_server_address,
               AdvancedTCPConnector::PAUSED, 0);

  m_ss->RunOnce(TimeInterval(0, 500000));

  // now unpause
  connector.Resume(m_server_address);
  // The socket may be connected immediately depending on the platform.
  AdvancedTCPConnector::ConnectionState state;
  unsigned int failed_attempts;
  connector.GetEndpointState(m_server_address, &state, &failed_attempts);
  if (state == AdvancedTCPConnector::DISCONNECTED) {
    m_ss->Run();
  }
  OLA_ASSERT_EQ(1u, connector.EndpointCount());
  ConfirmState(__LINE__, connector, m_server_address,
               AdvancedTCPConnector::CONNECTED, 0);

  // check our socket exists
  OLA_ASSERT_TRUE(m_connected_socket);
  m_connected_socket->Close();
  delete m_connected_socket;
  connector.Disconnect(m_server_address, true);

  // state should be updated
  ConfirmState(__LINE__, connector, m_server_address,
               AdvancedTCPConnector::PAUSED, 0);

  // clean up
  connector.RemoveEndpoint(m_server_address);
  OLA_ASSERT_EQ(0u, connector.EndpointCount());

  m_ss->RemoveReadDescriptor(&listening_socket);
}
开发者ID:Jurrie,项目名称:ola,代码行数:54,代码来源:AdvancedTCPConnectorTest.cpp

示例2: main

int main(int argc, char * *argv)
{
	console = new Console();

	/* Begin your application here. */
	int cc_err;

	TCPSocket * sock = new TCPSocket();
	console->WriteLine("Connecting...");
	cc_err = sock->Connect("www.example.com", 80);
	if (cc_err == CC_ERR_WOULD_BLOCK)
		console->WriteLine("Non-blocking sockets are enabled. Waiting for connection...");
	while (sock->State() == SOCKET_STATE_CONNECTING) ;
	if (sock->State() != SOCKET_STATE_CONNECTED) {
		console->WriteLine("Connection failed.");
		return 1;
	} else
		console->WriteLine("Connection seems OK.");
	console->Write("Requesting http://www.example.com/... ");
	cc_err = sock->Send("GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n");
	if (cc_err < 1)
		console->WriteLine("Failed.");
	else
		console->WriteLine("OK");

	while (sock->State() == SOCKET_STATE_CONNECTED) {
		string in;
		cc_err = sock->Read(in);
		if (cc_err == 0) {
			console->WriteLine(in);
			break;
		} else {
			if (cc_err != CC_ERR_WOULD_BLOCK)
				console->WriteLine("Read error %d (%s)", cc_err, GetErrorDescription(cc_err));
			System::ThreadSleep(10);
		}
	}

	sock->Close();

	delete sock;

	/* End your application here. */

#ifdef TARGET_OS_WINDOWS
	system("pause");
#endif

	delete console;
	return 0;
}
开发者ID:DanLinder,项目名称:crisscross,代码行数:51,代码来源:main.cpp

示例3: Accept

//---------------------------------------
bool TCPAcceptSocket::Accept(TCPSocket& po_rxConnection)
{
	if(!IsOpen()) return false;
	po_rxConnection.Close();
	WSAResetEvent(m_hEvent);

	sockaddr xA;
	int iSize=sizeof(xA);
	po_rxConnection.m_xSock=WSAAccept(m_xSock,&xA,&iSize,NULL,0);
	if((po_rxConnection.m_xSock==INVALID_SOCKET)||(po_rxConnection.m_xSock==WSAEWOULDBLOCK))
		return false;
	po_rxConnection.m_hClose=WSACreateEvent();
	WSAEventSelect(po_rxConnection.m_xSock,po_rxConnection.m_hClose,FD_CLOSE);
	return true;
}
开发者ID:marczaku,项目名称:zaku-webserver,代码行数:16,代码来源:TCPSocket.cpp

示例4: testConnect

/*
 * Test that a TCP Connect works.
 */
void AdvancedTCPConnectorTest::testConnect() {
  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));
  connector.AddEndpoint(m_server_address, &policy);
  OLA_ASSERT_EQ(1u, connector.EndpointCount());

  m_ss->Run();
  OLA_ASSERT_EQ(1u, connector.EndpointCount());

  // confirm the status is correct
  ConfirmState(__LINE__, connector, m_server_address,
               AdvancedTCPConnector::CONNECTED, 0);

  // check our socket exists
  OLA_ASSERT_TRUE(m_connected_socket);
  m_connected_socket->Close();
  delete m_connected_socket;
  OLA_INFO << "disconnecting";
  connector.Disconnect(m_server_address, true);

  // state should be updated
  ConfirmState(__LINE__, connector, m_server_address,
               AdvancedTCPConnector::PAUSED, 0);

  // remove & shutdown
  connector.RemoveEndpoint(m_server_address);
  OLA_ASSERT_EQ(0u, connector.EndpointCount());
  m_ss->RemoveReadDescriptor(&listening_socket);
}
开发者ID:basileus,项目名称:ola,代码行数:42,代码来源:AdvancedTCPConnectorTest.cpp

示例5: BaseCommand

void TCPConnection::BaseCommand(Params &request, ProcessDone_cb callback)
{
    Params result = request;
    if (request["0"] == "version")
    {
        cDebugDom("network") << "version";
        if (request["1"] == "?")
            result.Add("1", Utils::get_config_option("fw_version"));
    }
    else if (request["0"] == "save")
    {
        cDebugDom("network") << "save";

        Config::Instance().SaveConfigIO();
        Config::Instance().SaveConfigRule();

        if (request["1"] == "default")
        {
            //copy the default config files
            Utils::file_copy(ETC_DIR"io.xml", "/mnt/ext3/calaos/io.default");
            Utils::file_copy(ETC_DIR"rules.xml", "/mnt/ext3/calaos/rules.default");
        }
    }
    else if (request["0"] == "system")
    {
        cDebugDom("network") << "system";

        if (request["1"] == "reboot")
        {
            if (request["2"] == "calaos_gui")
            {
                int unused = system("killall -9 calaos_gui");
                unused = system("killall -9 calaos_thumb");
                (void)unused;
            }
            else if (request["2"] == "calaosd")
            {
                int unused = system("killall -9 calaosd");
                (void)unused;
            }
            else if (request["2"] == "all")
            {
                int unused = system("reboot");
                (void)unused;
            }
        }
        else if(request["1"] == "date")
        {
            vector<string> vcmd;
            for(int i=0;i<request.size();i++)
            {
                string s = Utils::to_string(i);
                vcmd.push_back(request[s]);

            }
            NTPClock::Instance().setNetworkCmdCalendarApply(vcmd);
            NTPClock::Instance().setRestartWhenApply(false);
            IPC::Instance().SendEvent("CalaosCommon::NTPClock","applyCalendar",NULL);

            result.Add("2", "ok");

            string cmd = "";
            for(int i=0;i<9;i++)
                cmd+=request[Utils::to_string(i)]+" ";
            //envoie de la commandes aux clients
            TCPSocket *sock;
            sock = new TCPSocket;
            sock->Create(UDP);
            sock->Broadcast(cmd, BCAST_UDP_PORT);
            sock->Close();
            delete sock;
        }
    }
    else if (request["0"] == "firmware")
    {
        cDebugDom("network") << "firmware";

        if (request["1"] == "webupdate")
        {
            //try to update firmware from /tmp/image.tar.bz2
            cDebugDom("network") << "save: Firmware update requested by web.";
            int unused = system("fw_update.sh");
            (void)unused;
        }
    }
    else if (request["0"] == "poll_listen")
    {
        cDebugDom("network") << "poll_listen";

        if (request["1"] == "register")
        {
            string uuid = PollListenner::Instance().Register();
            result.Add("2", uuid);
        }
        else if (request["1"] == "unregister")
        {
            if (PollListenner::Instance().Unregister(request["2"]))
                result.Add("2", "true");
            else
                result.Add("2", "false");
//.........这里部分代码省略.........
开发者ID:DjMomo,项目名称:calaos_base,代码行数:101,代码来源:BaseCommand.cpp

示例6: main

int main( int argc, char *argv[] )
{
#ifdef TEST_TCP_SOCKETS

  StartWinSock();

  TCPSocket listener;
  TCPSocket client;

  char *localIP = NULL;
  GetLocalIP( localIP );

  listener.SetIP( localIP );
  listener.SetPortNumber( 8000 );

  listener.Init();
  
  listener.Listen();

  while( !listener.Accept( client ) );
  
  while( true )
  {
    std::string message;
    Prompt( message, "Enter a message" );

    NetworkMessage netMessage;
    netMessage.msg_.Assign( message.c_str(), message.size() );

    client.Send( netMessage );
  }

  client.Shutdown();
  client.Close();

  listener.Shutdown();
  listener.Close();

  CloseWinSock();

#endif

  /*
  GameControl controller;

  try
  {
    controller.Init();

    try
    {
      controller.Run();
    }
    catch( BaseErrExcep e )
    {
      e.Print();
    }

    controller.Close();
  }
  catch( BaseErrExcep e )
  {
    e.Print();
  }
  */

    FileServer fileServer;

    try
    {
        fileServer.Init();

        try
        {
            fileServer.Run();
        }
        catch(BaseErrExcep& e)
        {
            e.Print();
        }

        fileServer.Close();
    }
    catch(BaseErrExcep& e)
    {
        e.Print();
    }
 
   
  printf( "\n Server is closing \n" );

  return WaitForInput();
}
开发者ID:GGBOY92,项目名称:cs260assignment3,代码行数:93,代码来源:ServerMain.cpp


注:本文中的TCPSocket::Close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。