• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C++ TcpClient类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: while

bool TcpServer::processAccept(int efd)
{
    if (this->stopping) return false;

    int socket;
    while (true)
    {
        ISocket *sock = this->listenSocket->accept();
        if (sock == 0)
        {
            if (errno == EAGAIN)
            {
                return true;
            }
            else
            {
                LOG("error while accepting. returned " << errno);
                return false;
            }
        }
        socket = sock->getSocket();
        int flags = fcntl(socket,F_GETFL,0);
        fcntl(socket, F_SETFL, flags | O_NONBLOCK);
        TcpClient* client = this->clientFactory->create(sock);
        clientList[socket] = client;
        client->setControlEventFd(this->controlEventFd);
        client->onConnected();
        addFdToEpoll(efd,socket);
    }

    return true;
}
开发者ID:pdumais,项目名称:DumaisLib,代码行数:32,代码来源:TcpServer.cpp


示例2: main

int main(int argc, char** argv) {
	const std::string& endpoint = (const std::string&)argv[1];

	pid_t pid = fork();
    if (pid == 0) {
		pid_t pid2 = fork();
	    if (pid2 == 0) {
	    	UdpClient *client = new UdpClient(1);
			client->Execute(endpoint);
	    } else {
	    	TcpClient *client = new TcpClient(2);
	        client->Execute(endpoint);
	    }
    } else {
		pid_t pid3 = fork();
	    if (pid3 == 0) {
        	UdpClient *client = new UdpClient(3);
    		client->Execute(endpoint);
        } else {
        	TcpClient *client = new TcpClient(4);
            client->Execute(endpoint);
        }
    }
	return 0;
}
开发者ID:deforestg,项目名称:TestClient,代码行数:25,代码来源:TestClient.cpp


示例3: ClientTCP

void ClientTCP()
{
    TcpClient client;
    Packet data;
    data.SetByteOrder(CX_PACKET_BIG_ENDIAN);
    if(!client.InitializeSocket(HOSTNAME, PORT)) 
    {
        cout << "Unable to connect to host " << HOSTNAME << endl;
        return;
    }
    cout << "Initialized TCP Client Socket\n";
    char key = 0;
    while(key != 27) 
    {

        if((key = GetChar()) > 0) 
        {
            data.Clear();       //  Clear packet and encode data.
            data.Write(key);
            client.Send(data);  //  Send data (could send character array also if desired
            /* Alternative way to send:
               client.Send(&key, 1);    //  Send buffer/array.
            */
            data.Clear();
            if(client.Recv(data, 50, 0))
            {
                cout << *data.Ptr() << endl;
            }
            //cout << key;
        }
    }
}
开发者ID:ShowLove,项目名称:Robotics_Club,代码行数:32,代码来源:example_networking.cpp


示例4: DEBUG_OUTPUT

void LocalPC::tcpServerReceiveData(void *tcp, char *buffer, int size)
{
    DEBUG_OUTPUT("[LocalPC]receive:\n%s\n", buffer_format(buffer, size));
    HeartbeatProtocol protocol;
    Heartbeat *hb = protocol.find(buffer, size);
    if(hb!=NULL)
    {
        delete hb;

        char *p = NULL;
        int size = 0;
        bool slave = isSlave();
        double timePoint = getSetupTime();
        Heartbeat *t = protocol.makeHeartbeat(slave, timePoint);
        if(NULL!=t)
        {
            if(t->makeBuffer(&p, size))
            {
                TcpClient *client = (TcpClient *)tcp;
                client->send(p, size);
                delete p;
            }
        }
    }
}
开发者ID:zhlgh603,项目名称:contron-psm70,代码行数:25,代码来源:localpc.cpp


示例5: main

int main(int argc, char** argv)
{

  const unsigned int IP_ARG_IDX = 1;
  TcpClient connection;
  ros::init(argc, argv, "state_interface");

  if (argc != 1) //Only one argument, the robot IP address is accepted
  {
    ROS_INFO("Robot state connecting to IP address: %s", argv[IP_ARG_IDX]);
    connection.init(argv[IP_ARG_IDX], StandardSocketPorts::STATE);

    std::vector<std::string> joint_names;
    joint_names.push_back("joint_s");
    joint_names.push_back("joint_l");
    joint_names.push_back("joint_e");
    joint_names.push_back("joint_u");
    joint_names.push_back("joint_r");
    joint_names.push_back("joint_b");
    joint_names.push_back("joint_t");

    RobotStateInterface rsi;
    if (rsi.init(&connection, joint_names))
    {
      rsi.run();
    }

  }
  else
  {
    ROS_ERROR("Missing command line arguments, usage: robot_state <robot ip address>");
  }

  return 0;
}
开发者ID:CloPeMa,项目名称:motoman,代码行数:35,代码来源:robot_state.cpp


示例6: main

int main(int argc, char** argv) {
	net_prepare();

	// Connect the AR.Drone video port
	TcpClient* videoClient = new  TcpClient(ARDRONE_IP, VIDEO_PORT);
	if (!videoClient->is_valid()) {
		cerr << "can't connnected to the video tcp..." << endl;
		abort();
	}

	int count = 0; // control the size of frames
	std::ofstream videoIO("video.h264", ios::binary);

	const int max_buf_size = VideoEncap::TcpPackSize;
	char* buf = (char*)malloc(max_buf_size);
	VideoEncap encap;
	while (true) {
		if (count>1500) {
			break;
		}
		
		videoClient->recv(buf, max_buf_size, NULL);
		videoIO.write((char*)buf, max_buf_size);
		//vardump_hex(buf, max_buf_size); // output the data (hex format)
		//if (encap.process(buf)) {
		//	videoIO.write((char*)encap.get_data(), encap.get_data_size());
		//	count++;
			//vardump_hex(encap.get_data(), encap.get_data_size());
		//}
		count++;
	}
	videoIO.close();
	net_end();
	return 0;
}
开发者ID:NonII,项目名称:ARDroneAPI,代码行数:35,代码来源:video_display.cpp


示例7: fakeMessagePassing

// Message passing routine, used to send and receive a typed message
// Useful for checking the packing and unpacking of message data.
void fakeMessagePassing(TypedMessage &send, TypedMessage &recv)
{
  const int tcpPort = TEST_PORT_BASE+401;
  char ipAddr[] = "127.0.0.1";

  TcpClient tcpClient;
  TcpServer tcpServer;
  SimpleMessage msgSend, msgRecv;

  send.toTopic(msgSend);

  // Construct server

  tcpServer.init(tcpPort);

  // Construct a client
  tcpClient.init(&ipAddr[0], tcpPort);
  tcpClient.makeConnect();  // make tcp client connection

  tcpServer.makeConnect();      // make tcp server connection

  tcpClient.sendMsg(msgSend);   // send message
  tcpServer.receiveMsg(msgRecv); // physically receive message
  recv.init(msgRecv); // copy received message into reference
}
开发者ID:usnistgov,项目名称:el-robotics-core,代码行数:27,代码来源:rosthread.cpp


示例8: time

/*************************************(*TcpClient::PerTransFun)*********************************************/
void *TcpClient::PerTransFun(void *object) {

	TcpClient *pt = (TcpClient *) object;
	pt->setstatus(true);
	time_t startTime = time(NULL);
	time_t endTime = time(NULL);

	//创建需要发送的消息sendMsg

	cout << "Client(" << getpid() << "):Packet send begin at "
			<< asctime(localtime(&endTime));

	while ((time(NULL) - startTime) <= TESTDURATION) {
		pt->period.tv_sec = PERIODSENDMSGSEC;
		pt->period.tv_usec = PERIODSENDMSGUSEC;
		int status = select(0, NULL, NULL, NULL, &(pt->period));
		if (0 == status) {
			pt->senddata(pt->clientMsg());

		}
	}

	//cout << "Client(" << getpid() << "):go into sleep! "<<endl;
	//sleep(KEEPALIVEDURATION);

	pt->setstatus(false);
	endTime = time(NULL);
	cout << "Client(" << getpid() << "):Packet send end in "
			<< asctime(localtime(&endTime));
	pthread_exit(0);
}
开发者ID:finder-hu,项目名称:LocationServer,代码行数:32,代码来源:TcpClient.cpp


示例9: while

TcpServer::~TcpServer() {
    DEBUG;

    while (!clientConnections.isEmpty()) {
        TcpClient *client = clientConnections.takeLast();
        client->disconnect(this);
        delete client;
    }
}
开发者ID:Trakk77,项目名称:ExtPlane,代码行数:9,代码来源:tcpserver.cpp


示例10: qDebug

TcpServer::~TcpServer() {
    qDebug() << Q_FUNC_INFO;

    while (!clientConnections.isEmpty()) {
        TcpClient *client = clientConnections.takeLast();
        client->disconnect(this);
        delete client;
    }
}
开发者ID:bobgates,项目名称:ExtPlane,代码行数:9,代码来源:tcpserver.cpp


示例11: main

int main (int argc, char** argv)
{
    QCoreApplication app(argc, argv);

    TcpClient t;
    t.connect("127.0.0.1", 4000);

    return app.exec();
}
开发者ID:GrenobleRoboticLab,项目名称:RemoteAdventurer,代码行数:9,代码来源:test_client.cpp


示例12: TcpClient

void TcpServer::incomingConnection(int handle)
{
    TcpClient *pClient = new TcpClient(this, handle);
    pClient->setSocketDescriptor(handle);
    connect(pClient, SIGNAL(clientReadData(int, QString, int, QByteArray)), \
            this, SIGNAL(clientReadData(int, QString, int, QByteArray)));
    connect(pClient, SIGNAL(clientDisConnect(int, QString, int)), \
            this, SLOT(disconnect(int, QString, int)));
    emit clientConnect(handle, pClient->peerAddress().toString(), pClient->peerPort());
    m_clientList.append(pClient);
    m_clientNum++;
}
开发者ID:Zimuge,项目名称:wfhs,代码行数:12,代码来源:tcpserver.cpp


示例13: tcpServerClientReceive

bool tcpServerClientReceive (TcpClient& client, char *data, int size)
{
	debugf("Application DataCallback : %s, %d bytes \r\n",client.getRemoteIp().toString().c_str(),size );
	debugf("Data : %s", data);
	client.sendString("sendString data\r\n", false);
	client.writeString("writeString data\r\n",0 );
	if (strcmp(data,"close") == 0)
	{
		debugf("Closing client");
		client.close();
	};
	return true;
}
开发者ID:MrAPierce,项目名称:Sming,代码行数:13,代码来源:application.cpp


示例14: main

int main(int argc, char *argv[])
{
    TcpClient tcpClient;
    string    ip;
    int       port = 9966;
    
    if(argc >= 3) {
        for(auto i = 1; i < argc; ++i) {
            if(!string(argv[i]).compare("--help")) {
                print_usage();
            } else if(!string(argv[i]).compare("-ip")) {
                ++i;
                if(i < argc) 
                    ip = argv[i];
            } else if(!string(argv[i]).compare("-port")) {
                ++i;
                if(i < argc) 
                    port = atoi(argv[i]);
            }
        }
    }

    if(ip.empty() ) 
        print_usage();

    if(tcpClient.clientInit(ip, port, AF_INET) < 0) {
        cout << "can not connect to " << ip << ":" << port << endl;
        exit(0);
    }
    cout << "connect to " << ip << ":" << port << endl;

    Socket *cli = tcpClient.getConnection();
    if(!cli) {
        printf("getConnection() fail\n");
    }
    for(int i = 0; i < 1000; ++i) {
        char buf[1024] = {0};

        sprintf(buf, "cnt %d\n", i);
        printf("%s", buf);
        cli->write(buf, strlen(buf));
        memset(buf, 0, sizeof(buf));
        cli->read(buf, sizeof(buf));
        printf("%s", buf);
        sleep(1);
    }

    tcpClient.releaseConnection(cli);

    return 0;
}
开发者ID:lookup69,项目名称:myNetworking,代码行数:51,代码来源:tcpcli.cpp


示例15: processReceive

int TcpServer::processReceive(int socket)
{
    auto it = clientList.find(socket);
    if (it == clientList.end()) return 0;
    
    TcpClient* client = it->second;

    char tmpBuffer[1024];  //TODO: should get a buffer from a buffer pool
    size_t n = 1;
    while (n > 0)
    {
        if (this->stopping)
        {
            n = 0;
            break;
        }
        n = client->getSocket()->read((char*)&tmpBuffer[0], 1024);

        if (n == 0)
        {
            client->onDisconnected();
            clientList.erase(it);
            delete client;
        }
        else if (n == -1)
        {
            n = 0;
            if (errno == ECONNRESET)
            {
                client->onDisconnected();
                clientList.erase(it);
                delete client;
            }
            else 
            {
                if (errno != EAGAIN)
                {
                    LOG("Read error. Returned " << errno);
                    abort();
                }
            }
            break;
        }
        else
        {
            client->onReceive((char*)&tmpBuffer[0], n);            
        }
    }
    
    return n;
}
开发者ID:pdumais,项目名称:DumaisLib,代码行数:51,代码来源:TcpServer.cpp


示例16: handleFunc

void* TcpClient::handleFunc(TcpClient* arg)
{
	TcpClient* pClient = (TcpClient*)arg;

	bool bCon(false);
	for (int i = 0; i < CONN_TIMES; i++)
	{
		bCon = pClient->getSocket()->connectToTcp("192.168.208.128", 9999);
		if (bCon)
			break;

		pClient->getSocket()->close();
		//pClient->getSocket()
	}
   
	if (bCon)
	{  
		G.m_nStatus = STATUS_CONNECTED;
		
		thread t(responseFunc, pClient);
		t.detach();		
		while (!pClient->m_bStop)
		{
			unique_lock<mutex> lock(pClient->m_writeMutex);
			if (pClient->m_writeData.getPos() == 0)
				pClient->m_writeCv.wait(lock);

			int nRet = pClient->m_socket.send(pClient->m_writeData.getBuf(), pClient->m_writeData.getPos());
			
			if (nRet < 0)
			{
				pClient->closeAndReConn();
				break;
			}
			else
			{
				pClient->m_writeData.pop(nRet);
			}
		}
		//pClient->requestLogin(G.getSeq());
	}
	else
	{  
        G.m_nStatus = STATUS_DISCONNECT;
    }     
     
    return NULL;      
}
开发者ID:yangyong3108,项目名称:project-001,代码行数:48,代码来源:TcpClient.cpp


示例17: read_cb

//从socket读
void TcpClient::read_cb(struct bufferevent *bev, void *arg)
{
    TcpClient *pClient = (TcpClient *)arg;
    const int MAX_LENGHT = 1024;
    char cbData[MAX_LENGHT];
    int n;
    //读数据:bufferevent_read
    //写数据:bufferevent_write
    while (n = bufferevent_read(bev, cbData, MAX_LENGHT))
    {
        if (n <= 0)
            break;
        //处理接收到的数据
        pClient->DealWithData(bev,cbData, n);
    }
}
开发者ID:jjfsq1985,项目名称:StudyLab,代码行数:17,代码来源:TcpClient.cpp


示例18: onClientComplete

void TelnetServer::onClientComplete(TcpClient& client, bool succesfull)
{
    if ( &client == curClient)
    {
        delete commandExecutor;
        commandExecutor = nullptr;
        curClient = nullptr;
        debugf("TelnetServer onClientComplete %s", client.getRemoteIp().toString().c_str() );
    }
    else
    {
        debugf("Telnet server unconnected client close");
    }

    debugf("TelnetServer onClientComplete %s", client.getRemoteIp().toString().c_str() );
    TcpServer::onClientComplete(client, succesfull);
}
开发者ID:robotiko,项目名称:Sming,代码行数:17,代码来源:TelnetServer.cpp


示例19: onClientComplete

void TcpServer::onClientComplete(TcpClient& client, bool succesfull)
{
	activeClients--;
	debugf("TcpSever onComplete : %s activeClients = %d\r\n",client.getRemoteIp().toString().c_str(),activeClients );
	if (clientCompleteDelegate)
	{
		clientCompleteDelegate(client,succesfull);
	}
}
开发者ID:Hukuma23,项目名称:WS,代码行数:9,代码来源:TcpServer.cpp


示例20: pthread_detach

void * TcpClient::handleRecvMsg(void *arg)
{
	pthread_detach(pthread_self());
	TcpClient *ptc = (TcpClient*)arg;
	RecvStream recvstream,rs;
	int i,r = 0;

	while(1)
	{
		pthread_mutex_lock(&mutexRecvStream);
		if(recvbuf.size <=0)
		{
			pthread_cond_wait(&condRecv,&mutexRecvStream);
		}
		r = recvbuf.getDataFromBuf(recvstream.stream, &(recvstream.size));
		pthread_mutex_unlock(&mutexRecvStream);
		 if(r)
		 {
			 for(int t=0; t<recvstream.size;t++)
			                 printf("%02x ", recvstream.stream[t]);
			             printf("\n");
			 ptc->toOriginalMsg(recvstream.stream, recvstream.size, rs.stream, &rs.size);
			i = ptc->checkCode(rs.stream, rs.size);
			if(i<0)
			{
				continue;
			}
			if(i==1)
			{
			//parse msg
				MsgHeader header;
				header.fromStream(rs.stream);
				switch(header.msgId)
				{
					case 0x8100: handleRegisterAck(&rs);break;
					case 0x8001: handlePlatformAck(&rs);break;
					default:break;
				}
			}
			r=0;
		 }
	}
	return NULL;
}
开发者ID:lsoftp,项目名称:czzd,代码行数:44,代码来源:tcpclient.cpp



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ TcpConnection类代码示例发布时间:2022-05-31
下一篇:
C++ Tcp类代码示例发布时间:2022-05-31
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap