1.UDP通信:
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char** argv)
{
ros::init(argc, argv, "udp_port");
//创建句柄(虽然后面没用到这个句柄,但如果不创建,运行时进程会出错)
ros::NodeHandle n;
//1 创建socket套接字
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd == -1)
{
perror("socket error");
exit(1);
}
// fd绑定本地的IP和端口
struct sockaddr_in serv;
memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_port = htons(1024);//设置服务器端口号
serv.sin_addr.s_addr = htonl(INADDR_ANY);//获取本机ip
//3.bind()绑定
//参数一:0的返回值(fd)
//参数二:(struct sockaddr*)&addr 前面结构体,即地址
//参数三: addr结构体的长度
int ret = bind(fd, (struct sockaddr*)&serv, sizeof(serv));
if(ret == -1)
{
perror("bind error");
exit(1);
}
struct sockaddr_in client;
socklen_t cli_len = sizeof(client);
// 通信
char buf[1024] = {0}; //创建接收数据的数组
ros::Rate loop_rate(50);//while以50Hz进行循环
while(ros::ok())
{
int recvlen = recvfrom(fd, buf, sizeof(buf), 0,
(struct sockaddr*)&client, &cli_len);//接收数据保存到buf,并返回数据长度。
if(recvlen == -1)
{
perror("recvform error");
exit(1);
}
printf("received data: %s\n", buf);
char ip[64] = {0};
printf(" Client IP: %s, Port: %d\n",
inet_ntop(AF_INET, &client.sin_addr.s_addr, ip, sizeof(ip)),
ntohs(client.sin_port));
// 给客户端发送数据
sendto(fd, buf, strlen(buf)+1, 0, (struct sockaddr*)&client, sizeof(client));
ros::spinOnce();
loop_rate.sleep();
}
close(fd);
return 0;
}
2.1 TCP通信Server:
#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;
}
2.2TCP通信Client:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 8080 //端口号
#define BUF_SIZE 1024 //数据缓冲区最大长度
char* SERVER_IP = "192.168.1.98";
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;
}