// 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 }
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; }
//===直接导出到lua里的方法=== void Net::initialize(std::string server_ip, int server_port) { initialize_profile(); tcp_client.init(server_ip, server_port); tcp_client.connect(); cocos2d::Director::getInstance()->set_battle_irrelevant_update(battle_irrelevant_update); }
int main(int argc, char** argv) { char ip[1024] = "192.168.0.50"; // Robot IP address TcpClient connection; MessageManager manager; ros::init(argc, argv, "state_interface"); ros::NodeHandle n; JointRelayHandler jr_handler(n); ROS_INFO("Setting up client"); connection.init(ip, StandardSocketPorts::STATE); connection.makeConnect(); jr_handler.init(&connection); manager.init(&connection); manager.add(&jr_handler); manager.spin(); }
void startTcpClient(const Uint16 port) { cout << "TCP Client[" << LOCATION_ADRR << ":" << port << "] Thread[" <<CurrentThread::info.tid() << "] Start!" << endl; sleep(1); TcpClient *pTcpClient = new TcpClient(); CHECK_NULLPTR_NORET(pTcpClient); CHECK_RETURN_VAL_NORET(pTcpClient->init(LOCATION_ADRR, port)); CHECK_RETURN_VAL_NORET(pTcpClient->sockConn(LOCATION_ADRR, UDPSERVER_PORT)); Int32 count(0); char buf[TEST_BUF_SIZE]; while (count < 5) { // 发送 count++; bzero(buf, sizeof(buf)); memcpy(buf, &count, sizeof(count)); Int32 nByte = pTcpClient->sendData(buf, TEST_BUF_SIZE); CHECK_RETURN_VAL_NORET(nByte); // 接收 sleep(1); bzero(buf, sizeof(buf)); nByte = pTcpClient->recvData(buf, TEST_BUF_SIZE); CHECK_RETURN_VAL_NORET(nByte); count = *(Int32*)buf; } bzero(buf, sizeof(buf)); count = END_FLAGS; memcpy(buf, &count, sizeof(count)); Int32 nByte = pTcpClient->sendData(buf, TEST_BUF_SIZE); CHECK_RETURN_VAL_NORET(nByte); sleep(1); delete pTcpClient; cout << "TCP Client[" << LOCATION_ADRR << ":" << port << "] Thread[" <<CurrentThread::info.tid() << "] End!" << endl; }