udp_client

udp 的简单使用。

https://download.csdn.net/download/v7xyy/10870832

//xx  2018-10-04
//实现laserscan数据读取并在udp下以结构体的形式发送出去

#include "ros/ros.h"

#include <iostream>
#include <stdlib.h>
#include <string.h>

#include <udp_get.h>

void *udp_get( void *arg )
{
   // udp 
   int client_fd;
   struct sockaddr_in ser_addr;
    client_fd = socket(AF_INET, SOCK_DGRAM, 0);
    if(client_fd < 0)
    {
        printf("create socket fail!\n");
      //  return -1;
    }
    memset(&ser_addr, 0, sizeof(ser_addr));
    ser_addr.sin_family = AF_INET;
    ser_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//    ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);  //注意网络序转换
    ser_addr.sin_port = htons(SERVER_PORT);  //注意网络序转换

  while(ros::ok())
    {     
      udp_data_buf.flag = 10;
      udp_data_buf.ch = "oooooookk";
      std::cout<<udp_data_buf.ch<<"  "<<std::endl;
      std::cout<<udp_data_buf.flag<<std::endl; 
      sendto(client_fd, (char *)&udp_data_buf, sizeof(udp_data_buf)+1,0,(struct sockaddr*)&ser_addr,sizeof(ser_addr));
    }
}
void create_all_thread(void)
{
	pthread_t thread_udp_get;
	if( (pthread_create( &thread_udp_get , NULL , udp_get, NULL )) != 0 )
	{
		perror("Create the thread_joy fail");
		exit( 1 );
	}
}



int main(int argc, char **argv)
{
   ros::init(argc, argv, "scan_data_get");
 //  ros::NodeHandle n;
   create_all_thread();
    ros::spin();


  return 0;
}

添加一个头文件#include <udp_get.h>

#ifndef __UDP_GET_H
#define __UDP_GET_H
#include <iostream>
#include <stdlib.h>
#include <string.h>
//udp
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define SERVER_PORT 8888


 struct udp_data
{
  std::string ch;
 //   char  ch[10];
  int flag;       

};     
 
struct udp_data udp_data_buf;

#endif

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值