计算机网络(五): ros下socket编程示例

ros下socket编程示例

服务器端
#include 
#include 
#include 
#include 
#include 
#include  
#include 
#include 
#include 
#define PORT 8080    //端口号
#define LOG  1       //请求队列中最大连接数量

using namespace std;


int main (int argc, char** argv)
{
	ros::init(argc, argv, "server_node");
	ros::NodeHandle nh;
	
	/*
	 *@fuc: 监听套节字描述符和连接套节字描述符
	 *@fuc; 服务器端和客户端IP4地址信息,struct关键字可不要
	 */
	int listenfd, connectfd;
	struct sockaddr_in sever;
	struct sockaddr_in client;
	socklen_t addrlen;
	
	/*
	 *@fuc: 使用socket()函数产生套节字描述符
	 */
	listenfd = socket(AF_INET, SOCK_STREAM, 0);
	if(listenfd == -1)
	{
		printf("socket() error\n");
		return -1;
	}
	
	/*
	 *@fuc: 初始化server套节字地址信息 
	 */
	memset((void *)&sever,0,sizeof(sever));
	sever.sin_family = AF_INET;
	sever.sin_addr.s_addr = htonl(INADDR_ANY);
	sever.sin_port = htons(PORT);
 
 	/*
	 *@fuc: 用bind()函数,将套接字与指定的协议地址绑定 
	 */
	if(bind(listenfd,(struct sockaddr *)&sever,sizeof(sever)) < 0)
	{
		printf("bind() error\n");
		return -1;
	}
	
 	/*
	 *@fuc: 使用listen()函数,等待客户端的连接 
	 */
    if(listen(listenfd, LOG) < 0)
    {
        printf("listen() error.\n");
        return -1;
    }
    
    addrlen = sizeof(client);
    
    //不断监听客户端请求
	while(ros::ok)
	{
		connectfd = accept(listenfd,(struct sockaddr *)&client,&addrlen);
		if(connectfd < 0)
		{
			printf("connect() error \n");
			return -1;
		}
		printf("You got a connection from client's IP is %s, port is %d\n",
				inet_ntoa(client.sin_addr), ntohs(client.sin_port));
				
		send(connectfd, "hello!",8,0);
		close(connectfd);
	}
	close(listenfd);
	return 0;
}
客户端
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define    MYPORT     8080   //端口号
#define    BUF_SIZE   1024   //数据缓冲区最大长度

char* SERVER_IP = "10.203.206.10";
int result = 0;

using namespace std;

int main(int argc, char **argv)
{
	ros::init(argc, argv, "client_node");
	ros::NodeHandle n;
	
	char recvbuf[BUF_SIZE];
	
	/*
	 *@fuc: socket()创建套节字
	 *
	 */
	int socket_cli = socket(AF_INET, SOCK_STREAM, 0);
	if(socket_cli < 0)
	{
		std::cout << "socket() error\n";
		return -1;
	}
	
	/*
	 *@fuc: 服务器端IP4地址信息,struct关键字可不写
	 *@fuc: 初始化sever地址信息   
	 */
	struct sockaddr_in sev_addr;  
	memset(&sev_addr, 0, sizeof(sev_addr));
	sev_addr.sin_family      = AF_INET;
	sev_addr.sin_port        = htons(MYPORT);
	sev_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
	std::cout << "connecting..." << std::endl;
	/*
	 *@fuc: 使用connect()函数来配置套节字,建立一个与TCP服务器的连接
	 */
	if(connect(socket_cli, (struct sockaddr*) &sev_addr, sizeof(sev_addr)) < 0)
	{ 
		std::cout << "connect error" << std::endl;
		return -1;
	}
	else
		std::cout << "connected successfullly!" << std::endl;
		
	while(ros::ok)
	{
		/*
		 *@fuc: 使用recv()函数来接收服务器发送的消息
		 */
		recv(socket_cli, recvbuf, sizeof(recvbuf), 0);
		printf("server message: %s\n", recvbuf);
	}
	/*
	 *@fuc: 关闭连接
	 */
	close(socket_cli);
	return 0;
}
启动说明
  • 在ROS工作空间下建立名字为tcp功能包,并将服务器代码以及客户端代码复制到对应位置
  • 先启动服务器节点,再启动客户端节点

你可能感兴趣的:(网络通讯,笔记,计算机网络,socket,c++)