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


C++ TCPClient类代码示例

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


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

示例1: assert

void TCPClient::AfterRecv(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf)
{
    TcpClientCtx* theclass = (TcpClientCtx*)handle->data;
    assert(theclass);
    TCPClient* parent = (TCPClient*)theclass->parent_server;
    if (nread < 0) {
        if (parent->reconnectcb_) {
            parent->reconnectcb_(NET_EVENT_TYPE_DISCONNECT, parent->reconnect_userdata_);
        }
        if (!parent->StartReconnect()) {
            fprintf(stdout, "Start Reconnect Failure.\n");
            return;
        }
        if (nread == UV_EOF) {
            fprintf(stdout, "Server close(EOF), Client %p\n", handle);
            LOG(INFO) << ("Server close(EOF)");
        } else if (nread == UV_ECONNRESET) {
            fprintf(stdout, "Server close(conn reset),Client %p\n", handle);
            LOG(INFO) << ("Server close(conn reset)");
        } else {
            fprintf(stdout, "Server close,Client %p:%s\n", handle, GetUVError(nread).c_str());
            LOG(INFO) << "Server close" << GetUVError(nread);
        }
        uv_close((uv_handle_t*)handle, AfterClientClose);//close before reconnect
        return;
    }
    parent->send_inl(NULL);
    if (nread > 0) {
        theclass->packet_->recvdata((const unsigned char*)buf->base, nread);
    }
}
开发者ID:Leon718,项目名称:simple_libuv_server,代码行数:31,代码来源:tcpclient.cpp

示例2: GetUVError

void TCPClient::AfterConnect(uv_connect_t* handle, int status)
{
    TcpClientCtx* theclass = (TcpClientCtx*)handle->handle->data;
    TCPClient* parent = (TCPClient*)theclass->parent_server;
    if (status) {
        parent->connectstatus_ = CONNECT_ERROR;
        parent->errmsg_ = GetUVError(status);
        LOG(ERROR)<<"client(" << parent << ") connect error:" << parent->errmsg_;
        fprintf(stdout, "connect error:%s\n", parent->errmsg_.c_str());
        if (parent->isreconnecting_) {//reconnect failure, restart timer to trigger reconnect.
            uv_timer_stop(&parent->reconnect_timer_);
            parent->repeat_time_ *= 2;
            uv_timer_start(&parent->reconnect_timer_, TCPClient::ReconnectTimer, parent->repeat_time_, parent->repeat_time_);
        }
        return;
    }

    int iret = uv_read_start(handle->handle, AllocBufferForRecv, AfterRecv);
    if (iret) {
        parent->errmsg_ = GetUVError(status);
        LOG(ERROR)<<"client(" << parent << ") uv_read_start error:" << parent->errmsg_;
        fprintf(stdout, "uv_read_start error:%s\n", parent->errmsg_.c_str());
        parent->connectstatus_ = CONNECT_ERROR;
    } else {
        parent->connectstatus_ = CONNECT_FINISH;
        LOG(INFO) << "client(" << parent << ")run";
    }
    if (parent->isreconnecting_) {
        fprintf(stdout, "reconnect succeed\n");
        parent->StopReconnect();//reconnect succeed.
        if (parent->reconnectcb_) {
            parent->reconnectcb_(NET_EVENT_TYPE_RECONNECT, parent->reconnect_userdata_);
        }
    }
}
开发者ID:Leon718,项目名称:simple_libuv_server,代码行数:35,代码来源:tcpclient.cpp

示例3: RecvThread

void TCPClient::RecvThread(void* p)
{
    TCPClient* pSelf = reinterpret_cast<TCPClient*>(p);
    if (NULL != pSelf)
    {
        pSelf->DoRecv();
    }
}
开发者ID:wuzangsama,项目名称:communication,代码行数:8,代码来源:TCPClient.cpp

示例4: ReconnectThrd

void TCPClient::ReconnectThrd(void* p)
{
    TCPClient* pSelf = reinterpret_cast<TCPClient*>(p);
    if (NULL != pSelf)
    {
        pSelf->DoReconnect();
    }
}
开发者ID:wuzangsama,项目名称:communication,代码行数:8,代码来源:TCPClient.cpp

示例5: sendToGraphite

//
// TCP stuff
//
void sendToGraphite( String id, String value )
{
    TCPClient client;
    
    if ( client.connect( "rpi2", 2003 ) )
    {
        client.println( id + " " + value + " " + String( Time.now() ) );
        client.stop();
    }
}
开发者ID:lemontongs,项目名称:AllSpark,代码行数:13,代码来源:main.cpp

示例6: AsyncCB

void TCPClient::AsyncCB(uv_async_t* handle)
{
    TCPClient* theclass = (TCPClient*)handle->data;
    if (theclass->isuseraskforclosed_) {
        theclass->closeinl();
        return;
    }
    //check data to send
    theclass->send_inl(NULL);
}
开发者ID:Leon718,项目名称:simple_libuv_server,代码行数:10,代码来源:tcpclient.cpp

示例7: main

int main()
{
    signal(SIGPIPE, SIG_IGN);
    char buf[BUFSIZ];
    int clientCount = 0;
    try
    {
        TCPServer server(8001);
        int listenfd = server.getfd();
        Epoll epoll;
        // 将监听套接字注册到epoll
        epoll.addfd(server.getfd(), EPOLLIN, true);
        while (true)
        {
            int nReady = epoll.wait();
            for (int i = 0; i < nReady; ++i)
                // 如果是监听套接字发生了可读事件
                if (epoll.getEventOccurfd(i) == listenfd)
                {
                    int connectfd = accept(listenfd, NULL, NULL);
                    if (connectfd == -1)
                        err_exit("accept error");
                    cout << "accept success..." << endl;
                    cout << "clientCount = " << ++ clientCount << endl;
                    setUnBlock(connectfd, true);
                    epoll.addfd(connectfd, EPOLLIN, true);
                }
                else if (epoll.getEvents(i) & EPOLLIN)
                {
                    TCPClient *client = new TCPClient(epoll.getEventOccurfd(i));
                    memset(buf, 0, sizeof(buf));
                    if (client->read(buf, sizeof(buf)) == 0)
                    {
                        cerr << "client connect closed..." << endl;
                        // 将该套接字从epoll中移除
                        epoll.delfd(client->getfd());
                        delete client;
                        continue;
                    }
                    cout << buf;
                    client->write(buf);
                }
        }
    }
    catch (const SocketException &e)
    {
        cerr << e.what() << endl;
        err_exit("TCPServer error");
    }
    catch (const EpollException &e)
    {
        cerr << e.what() << endl;
        err_exit("Epoll error");
    }
}
开发者ID:Tachone,项目名称:LinuxPorgDemo,代码行数:55,代码来源:epollsrv.cpp

示例8: receiver

void *
receiver(void * v) {
    TCPClient *client = (TCPClient*) v;

    assert(client != NULL);
    assert (client->get_sock() >= 0);

    while (client->running) {
        if (!client->running) {
            break;
        }
        
        char buffer[RECEIVE_BUFFER_SIZE];
        int allBytesRead = 0;
        int actBytesRead = 0;
        
        read_modes act_read_mode = client->get_read_mode();

        while (allBytesRead < (RECEIVE_BUFFER_SIZE-1)) {
            actBytesRead = recv(client->get_sock(), buffer, sizeof(buffer), 0);

            if (actBytesRead < 0) {
                // perror("receive failed!");
                break;
            } else if (actBytesRead == 0) {
                std::cout << "connection reset.. " << std::endl;

                if (allBytesRead > 0) {
                    buffer[allBytesRead+1] = '\0';
                    std::cout << "last bytes received: " << buffer << std::endl;
                }

                client->close_conn();
                client->running = false;
                return 0;
            }

            allBytesRead += actBytesRead;
            if (act_read_mode == INCOME) {
                break;
            }
        }

        if (allBytesRead > 0) {
            buffer[allBytesRead] = '\0';
            std::cout << "received: " << buffer << std::endl;
        }
    }

    return 0;
}
开发者ID:namderwanderer,项目名称:tcp-communication,代码行数:51,代码来源:TCPClient.cpp

示例9: loop

void loop() {
  if (nextTime > millis()) return;
  g_connected = client.connect(host, g_port);
  if (!g_connected) {
    client.stop();
    // If TCP Client can't connect to host, exit here.
    return;
  }
  g_https_complete = false;
  g_bytes_received = 0;
  if (g_https_trace) {
    freemem = System.freeMemory();
    Serial.print("free memory: ");
    Serial.println(freemem);
  }
  int32 rc;
  if ((rc = httpsClientConnection(httpRequestContent, 0, NULL) < 0)) {
    // TODO: When massive FAIL
    httpsclientCleanUp();
    digitalWrite(anomalyLed, HIGH);
    delay(500);
    digitalWrite(anomalyLed, LOW);
    delay(500);
    digitalWrite(anomalyLed, HIGH);
    delay(500);
    digitalWrite(anomalyLed, LOW);
  } else {
    digitalWrite(heartbeatLed, HIGH);
    delay(250);
    digitalWrite(heartbeatLed, LOW);
  }
  client.stop();
  nextTime = millis() + 5000;
}
开发者ID:Nicowow,项目名称:httpsclient-particle,代码行数:34,代码来源:timeapi-test.cpp

示例10: loop

void loop() {
  delay(1);
  if (!WiFi.ready()) {
    Particle.process();
    WiFi.connect();
    while(WiFi.connecting()) {Particle.process();}
  }// if (!WiFi.ready())
  else
  {
    complete = false;
    lastTime = millis();
    while ((!complete) &&  (millis() - lastTime < 10000)){
      if (client.connect( server, serverPort)) {
        if (client.connected()) {
          out(clientmsg);
          lastTime = millis();
           while ((!client.available()) && (millis() - lastTime < 10000)) {Particle.process();}//wait for response
            in(inmsg,10);//10 pure trial and error
            myInStr =inmsg;
            if (myInStr.indexOf(replymsg)  >= 0) {
              digitalWrite(D7, 1);          // Flashes the LED
              lastTime = millis();
              while ( millis()-lastTime < 5) { } //5
              digitalWrite(D7, 0);          // Flashes the LED
              complete = true;
            }//if (myInStr.indexOf(replymsg)  >= 0)
        }//if (client.connected())
      }//if (client.connect( server, serverPort))
    }//while (!complete)
  }//else
}//loop
开发者ID:garthoff,项目名称:photon-simple-tcpclientserver,代码行数:31,代码来源:tcpclientFast.cpp

示例11: loop

void loop() {

    char tmp[3];
    int charPosition = 0;

    // While there is data in the buffer, lets read it.
    while(client.available()) {

        char c = client.read();

        // Check if we reached the end of the data
        if(c == ',') {

            // use the accumulated value
            pData(tmp);

            // Reset
            tmp[0];
            charPosition = 0;

        } else {

            // Save char in to the temp array
            tmp[charPosition] = c;

            // Increment the position of the array
            charPosition++;

        }

    }

}
开发者ID:TheraPDX,项目名称:IoT-Raw-Sockets-Examples,代码行数:33,代码来源:tcpParticleClient.cpp

示例12: DlgProc

INT_PTR WINAPI DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_INITDIALOG:
    ShowWindow(hWnd, SW_SHOW);
    while (!_client.ConnectServer("92.241.225.221", 1000))
      Sleep(1000);
		break;
  case WM_COMMAND:
    switch(LOWORD(wParam))
    {
      case IDC_BUTTON2:
      {
        char* data = new char[256];
        for (int i = 0; i < 256; i++)
          data[i] = '\0';
        GetDlgItemText(hWnd, IDC_EDIT1, data, 256);
        _client.SendData(data, strlen(data));
        break;
      }
    }
    break;
	case WM_CLOSE:
		EndDialog(hWnd,0);
		break;
	default:
		return false;
	}
	return true;
}
开发者ID:jprothwell,项目名称:winSockets,代码行数:31,代码来源:main.cpp

示例13: loop

void loop()
{
    if (client.connected())
    {
        while (client.available())
        {
            in(inmsg,1000);
            myInStr =inmsg;
            if (myInStr.indexOf(clientmsg)  >= 0) {
                digitalWrite(D7, 1);
                delay(50);
                digitalWrite(D7, 0);
                sprintf(outmsg,"%c",replymsg);
                out(outmsg);
//THIS WORKS TOO
//                IPAddress myIP = WiFi.localIP();
//                sprintf(outmsg, "%d.%d.%d.%d,%d,2,%d,3,4/e", myIP[0], myIP[1], myIP[2], myIP[3],counter,simulate_temp);
//                out(outmsg); //but don't yet have a tcpclient that can handle such a response
            }//if (myInStr.indexOf("7")  >= 0)
        }//while (client.available())
    }//if (client.connected())
    client.read();
    client.flush();
//    client.stop(); //apparently unnecessary
    client = server.available();
 //keep connection alive long enough for client to receive reply
    lastTime = millis();
    while ( millis()-lastTime < 300) {}
}//loop
开发者ID:garthoff,项目名称:photon-simple-tcpclientserver,代码行数:29,代码来源:tcpserverSimple.cpp

示例14: tcpLog

int tcpLog(String message) {
    if (tcp.connected()) {
        tcp.print(STX + Spark.deviceID() + ETX + message + EOT);
        delay(250); // try not to flood socket with too many writes
        return 1;
    } else {
        return -1;
    }
}
开发者ID:crstffr,项目名称:crstffr-spark-logger,代码行数:9,代码来源:firmware.cpp

示例15: loop

void loop() {
  if (client.connected()) {
    Firmata.sendString("Hello World!");
    delay(1000);
  } else {
    Serial.println("Disconnected.");
    client.stop();
    while(1) {}; // loop forever  
  }
}
开发者ID:blakejakopovic,项目名称:firmata-spark,代码行数:10,代码来源:FirmataTCP.cpp


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