改装了rov,rov原有树莓派和加装的树莓派通过UDP通信,加装的树莓派上的udp程序收不到rov发送的数据,但我单独在rov树莓派上令写了个测试程序,向外发送数据,加装树莓派上相同的程序可以接收,前后的差别就只有改个端口号。
之前rov树莓派直接和win10电脑连,可以正常接收udp数据。
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<ctype.h>
int main(){
struct sockaddr_in servaddr,cliaddr;
socklen_t cliaddr_len;
int sockfd;
char buf[300];
char str[INET_ADDRSTRLEN];
int i,n;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
inet_pton(AF_INET,"192.168.2.1",&servaddr.sin_addr);
servaddr.sin_port=htons(14550);
bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
printf("accepting connections ...\n");
cliaddr_len=sizeof(cliaddr);
n=recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&cliaddr,&cliaddr_len);
printf("connection already..\n");
while(1){
cliaddr_len=sizeof(cliaddr);
n=recvfrom(sockfd,buf,sizeof(512),0,(struct sockaddr*)&cliaddr,&cliaddr_len);
if(n==-1){
perror("recvfrom error");
}
printf("recvfrom from %s at PORT %d\n",inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),ntohs(cliaddr.sin_port));
printf("recvive: %s length: %d\n",buf,n);
//printf("receice: %d\n",(int)buf[0]);
}
close(sockfd);
return 0;
}