TCP机械臂测试
通过w(红色臂角度增大)s(红色臂角度减小)d(蓝色臂角度增大)a(蓝色臂角度减小)按键控制机械臂
#include<myhead.h>
#define SER_PORT 8888 //与服务器保持一致
#define SER_IP "192.168.0.149" //服务器ip地址
#define CLI_PORT 6666 //客户端端口号
#define CLI_IP "192.168.0.150" //客户端ip地址
int main(int argc, const char *argv[])
{
//1、创建用于通信的套接字文件描述符
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd == -1)
{
perror("socket error");
return -1;
}
printf("cfd = %d\n", cfd);
//3、连接到服务器
//3.1 填充服务器地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET; //通信域
sin.sin_port = htons(SER_PORT); //服务器端口号
sin.sin_addr.s_addr = inet_addr(SER_IP); //服务器ip地址
//3.2 连接服务器
if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) == -1)
{
perror("connect error");
return -1;
}
printf("连接服务器成功\n");
//准备数据
char rbuf[5] = {0xff, 0x02, 0x00, 0x00, 0xff};
char bbuf[5] = {0xff, 0x02, 0x01, 0x00, 0xff};
//发送给服务器,以初始化机械臂
send(cfd, rbuf,sizeof(rbuf), 0);
sleep(1);
send(cfd, bbuf,sizeof(bbuf), 0);
char n ;
while(1)
{
scanf("%c",&n);
switch(n)
{
case 'a':
{
bbuf[3]=bbuf[3]-1;
send(cfd, bbuf,sizeof(bbuf), 0);
}
break;
case 'd':
{
bbuf[3]=bbuf[3]+1;
send(cfd, bbuf,sizeof(bbuf), 0);
}
break;
case 's':
{
rbuf[3]=rbuf[3]-1;
send(cfd, rbuf,sizeof(rbuf), 0);
}
break;
case 'w':
{
rbuf[3]=rbuf[3]+1;
send(cfd, rbuf,sizeof(rbuf), 0);
}
break;
default :
break;
}
}
//5、关闭套接字
close(cfd);
return 0;
}
广播的发送端流程
#include <myhead.h>
int main(int argc,const char *argv[])
{
//1.创建用于通信的套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd==-1)
{
perror("socket error");
return -1;
}
printf("sfd = %d\n",sfd);
//2.绑定可选
//3.设置允许消息广播
int broad = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,&broad,sizeof(broad))==-1)
{
perror("setsockopt error");
return -1;
}
printf("设置广播成功\n");
//4.向广播地址发送消息
struct sockaddr_in bin;
bin.sin_family = AF_INET;
bin.sin_port = htons(5555);
bin.sin_addr.s_addr = inet_addr("192.168.0.255");
char wbuf[128]="";
while(1)
{
printf("请输入>>>");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1] = 0;
sendto(sfd,wbuf,strlen(wbuf),0,(struct sockaddr *)&bin,sizeof(bin));
printf("发送成功\n");
}
//5.关闭套接字
close(sfd);
return 0;
}
广播的接收端流程
#include <myhead.h>
int main(int argc,const char *argv[])
{
//1.创建用于通信的套接字文件描述符
int rfd = socket(AF_INET,SOCK_DGRAM,0);
if(rfd==-1)
{
perror("socket error");
return -1;
}
printf("rfd = %d\n",rfd);
//2.绑定广播地址和端口号
struct sockaddr_in rin;
rin.sin_family = AF_INET;
rin.sin_port = htons(5555);
rin.sin_addr.s_addr = inet_addr("192.168.0.255");
if(bind(rfd,(struct sockaddr *)&rin,sizeof(rin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
//3.接收数据
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
recv(rfd,rbuf,sizeof(rbuf),0);
printf("收到广播消息为:%s\n",rbuf);
}
//4.关闭套接字
close(rfd);
return 0;
}
组播发送端流程
#include <myhead.h>
int main(int argc,const char *argv[])
{
//1.创建用于通信的套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd==-1)
{
perror("socket error");
return -1;
}
printf("sfd = %d\n",sfd);
//2.绑定可选
//3.向多播组地址发送消息
struct sockaddr_in min;
min.sin_family = AF_INET;
min.sin_port = htons(9999);
min.sin_addr.s_addr = inet_addr("224.1.2.3");
char wbuf[128]="";
while(1)
{
printf("请输入>>>");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1] = 0;
sendto(sfd,wbuf,strlen(wbuf),0,(struct sockaddr *)&min,sizeof(min));
printf("发送成功\n");
}
//4.关闭套接字
close(sfd);
return 0;
}
组播的接收端流程
#include <myhead.h>
int main(int argc,const char *argv[])
{
//1.创建用于通信的套接字文件描述符
int rfd = socket(AF_INET,SOCK_DGRAM,0);
if(rfd==-1)
{
perror("socket error");
return -1;
}
printf("rfd = %d\n",rfd);
//2.绑定组播地址和端口号
struct sockaddr_in rin;
rin.sin_family = AF_INET;
rin.sin_port = htons(9999);
rin.sin_addr.s_addr = inet_addr("224.1.2.3");
if(bind(rfd,(struct sockaddr *)&rin,sizeof(rin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
//3.加入多播组
struct ip_mreqn imr;
imr.imr_multiaddr.s_addr = inet_addr("224.1.2.3");
imr.imr_address.s_addr = inet_addr("192.168.0.150");
imr.imr_ifindex = 2;
if(setsockopt(rfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&imr,sizeof(imr))==-1)
{
perror("setsockopt error");
return -1;
}
printf("加入多播组成功\n");
//4.接收数据
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
recv(rfd,rbuf,sizeof(rbuf),0);
printf("收到组播消息为:%s\n",rbuf);
}
//5.关闭套接字
close(rfd);
return 0;
}