ROS系统下完成TCP通信,服务端和客户端C语言编程
服务端(Server):
#include
#include "ros/ros.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 4096
int listenfd, connfd;
struct sockaddr_in servaddr;
ros::Publisher robot_flag_publish ;
void init()
{
if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(8989);
if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1)
{
printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
if( listen(listenfd, 10) == -1)
{
printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
printf("======waiting for client's request======\n");
while(1)
{
if( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1)
{
printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
continue;
}
else
{
break;
}
}
}
void receive()
{
while(1)
{
char buff[4096];
int n = recv(connfd, buff, MAXLINE, 0);
if(n > 0)
{
printf("recv msg from client: %s\n", buff);
std_msgs::Bool data;
data.data = true;
robot_flag_publish.publish(data);
}
}
}
void flag_message(const std_msgs::Bool &flag_data)
{
if(flag_data.data==true)
{
char buff_send[] = "success";
send(connfd, buff_send, strlen(buff_send),0);
printf("send msg\n");
}
}
int main(int argc, char** argv)
{
ros::init(argc,argv,"server");
ros::NodeHandle nh;
ros::NodeHandle n("~");
ros::Subscriber robot_flag_subScribe = n.subscribe("/robot1_flag",1,&flag_message);
robot_flag_publish = nh.advertise<std_msgs::Bool>("robot2_flag", 1);
init();
//std::thread recv_thread(receive);
while(ros::ok)
{
char buff[4096];
int n = recv(connfd, buff, MAXLINE, 0);
if(n > 0)
{
printf("recv msg from client: %s\n", buff);
std_msgs::Bool data;
data.data = true;
robot_flag_publish.publish(data);
}
ros::spinOnce();
}
close(connfd);
close(listenfd);
//recv_thread.join();
return 0;
}
客户端(Client):
#include
#include "ros/ros.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 4096
int sockfd;
struct sockaddr_in servaddr;
ros::Publisher robot_flag_publish ;
void init()
{
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(8989);
if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
{
printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
}
void receive()
{
while(1)
{
char buff[4096];
int n = recv(sockfd, buff, MAXLINE, 0);
if(n > 0)
{
printf("recv msg from client: %s\n", buff);
std_msgs::Bool data;
data.data = true;
robot_flag_publish.publish(data);
}
}
}
void flag_message(const std_msgs::Bool &flag_data)
{
if(flag_data.data==true)
{
char buff_send[] = "success";
send(sockfd, buff_send, strlen(buff_send),0);
printf("send msg\n");
}
}
int main(int argc, char** argv)
{
ros::init(argc,argv,"client");
ros::NodeHandle nh;
ros::NodeHandle n("~");
ros::Subscriber robot_flag_subScribe = n.subscribe("/robot2_flag",1,&flag_message);
robot_flag_publish = nh.advertise<std_msgs::Bool>("robot1_flag", 1);
init();
//std::thread recv_thread(receive);
while(ros::ok())
{
char buff[4096];
int n = recv(sockfd, buff, MAXLINE, 0);
if(n > 0)
{
printf("recv msg from client: %s\n", buff);
std_msgs::Bool data;
data.data = true;
robot_flag_publish.publish(data);
}
ros::spinOnce();
}
printf("end\n");
close(sockfd);
//recv_thread.join();
return 0;
}
关于CMakeLista.txt与package.xml的设置
关于CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(my_socket)
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall ${CMAKE_CXX_FLAGS}")
find_package(catkin REQUIRED COMPONENTS
std_msgs
roscpp
)
catkin_package(
)
include_directories(
${catkin_INCLUDE_DIRS}
)
add_executable(server
src/server.cpp
)
target_link_libraries(server
${catkin_LIBRARIES}
)
add_executable(client
src/client.cpp
)
target_link_libraries(client
${catkin_LIBRARIES}
)
关于package.xml
<package>
<name>my_socket</name>
<version>0.0.0</version>
<description>The socket package</description>
<maintainer email="[email protected]">flight</maintainer>
<license>TODO</license>
<author email="[email protected]">flight</author>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>std_msgs</build_depend>
<build_depend>roscpp</build_depend>
<run_depend>std_msgs</run_depend>
<run_depend>roscpp</run_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>