1、TCP客户端控制机械臂代码实现:
#include <myhead.h>
#define IP "192.168.126.58"
#define PORT 8888#define CLI_IP "192.168.126.91" //客户端IP
#define CLI_PORT 6666 //客户端端口号int main(int argc, const char *argv[])
{//1.创建套接字文件描述符int cfd=-1;if((cfd=socket(AF_INET,SOCK_STREAM,0))==-1){perror("socket error");return -1;}printf("cfd=%d\n",cfd);//绑定struct sockaddr_in cin;cin.sin_family=AF_INET;cin.sin_port=htons(CLI_PORT);cin.sin_addr.s_addr=inet_addr(CLI_IP);if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1){perror("bind error");return -1;}//连接服务器//填充要连接的服务器地址信息结构体struct sockaddr_in sin;sin.sin_family=AF_INET;sin.sin_port=htons(PORT);sin.sin_addr.s_addr=inet_addr(IP);if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1){perror("connect error");return -1;}printf("connect success\n");char rbuf[5]={0xff,0x02,0x00,0x00,0xff}; //红色臂unsigned char bbuf[5]={0xff,0x02,0x01,0x00,0xff};//蓝色臂send(cfd,rbuf,sizeof(rbuf),0);sleep(1);send(cfd,bbuf,sizeof(bbuf),0);char key=0;//收发数据while(1){printf("请输入key的值>>>");scanf("%c",&key);getchar();switch(key){case 'w':case 'W':rbuf[3]+=3;if(rbuf[3]>=90){rbuf[3]=90;}send(cfd,rbuf,sizeof(rbuf),0);break;case 's':case 'S':rbuf[3]-=3;if(rbuf[3]<=-90){rbuf[3]=-90;}send(cfd,rbuf,sizeof(rbuf),0);break;case 'd':case 'D':bbuf[3]+=3;if(bbuf[3]>=180){bbuf[3]=180;}send(cfd,bbuf,sizeof(bbuf),0);break;case 'A':case 'a':bbuf[3]-=3;if(bbuf[3]<=0){bbuf[3]=0;}send(cfd,bbuf,sizeof(bbuf),0);break;default:printf("输入错误,请重新输入!!!\n");}}//关闭套接字close(cfd);return 0;
}
2、tftp实现文件的上传和下载
#include <myhead.h>
#define IP "192.168.126.47"
#define PORT 69//定义下载函数
int do_download(int sfd,struct sockaddr_in sin)
{char pack[516]=""; //组件协议包:下载请求short *p1=pack;*p1=htons(1); //设置操作码char *p2=pack+2;char filename[40]="";printf("请输入要下载的文件名>>>");fgets(filename,sizeof(filename),stdin);filename[strlen(filename)-1]=0;strcpy(p2,filename);char *p4=p2+strlen(p2)+1;strcpy(p4,"octet"); //模式位int packlen=4+strlen(p2)+strlen(p4); //请求包的大小//向服务器发送请求包if(sendto(sfd,pack,packlen,0,(struct sockaddr*)&sin,sizeof(sin))==-1){perror("sendto error");return -1;}printf("请求成功\n");//收取服务器发来的数据包char rbuf[516]="";short *r1=rbuf;//创建一个文件int fd=-1;if((fd=open(filename,O_RDWR|O_CREAT|O_TRUNC,0664))==-1){perror("open error");return -1;}printf("fd=%d\n",fd);socklen_t socklen=sizeof(sin);while(1){// printf("111\n");bzero(rbuf,sizeof(rbuf));int res=recvfrom(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&sin,&socklen);if(*r1==ntohs(3)){if(write(fd,rbuf+4,res-4)<0){perror("write error");break;}*r1=htons(4);if(sendto(sfd,rbuf,4,0,(struct sockaddr*)&sin,sizeof(sin))==-1){perror("sendto error");return -1;}if(res<516){printf("下载完毕\n");break;}else if(rbuf[1]==5){printf("download error:%s\n",rbuf+4);}}}close(fd);return 0;//如果为512,则读取后,回复一个应打包,继续接收下一个//如果小于512,则读取数据后,回复一个应打包,结束接收数据 }//定义上传函数
int do_upload(int sfd,struct sockaddr_in sin)
{char pack[516]=""; //组件协议包:上传请求short *p1=pack;*p1=htons(2); //设置操作码char *p2=pack+2;char filename[40]="";printf("请输入要上传的文件名>>>");fgets(filename,sizeof(filename),stdin);filename[strlen(filename)-1]=0;strcpy(p2,filename);char *p4=p2+strlen(p2)+1;strcpy(p4,"octet"); //模式位int packlen=4+strlen(p2)+strlen(p4); //请求包的大小//向服务器发送请求包if(sendto(sfd,pack,packlen,0,(struct sockaddr*)&sin,sizeof(sin))==-1){perror("sendto error");return -1;}printf("请求成功\n");//从linux系统中读取文件,然后发送给服务器//以只读的形式打开上传的文件int fd1=-1;if((fd1=open(filename,O_RDWR))==-1){perror("open error");return -1;}printf("fd1=%d\n",fd1);socklen_t socklen=sizeof(sin);//用来读取文件的容器char rbuf[516]="";short *r1=rbuf; //操作码short *r2=rbuf+2; //块编号int i=1;int len=0; //读取文件的大小while(1){bzero(rbuf,sizeof(rbuf));int res=recvfrom(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&sin,&socklen);if(*r1==ntohs(4)){bzero(rbuf,sizeof(rbuf));len=read(fd1,rbuf+4,512); *r2=htons(i); //给块编号赋值//把读取文件的内容发送给服务器*r1=htons(3); //给操作码赋值if(sendto(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&sin,socklen)==-1){perror("sendto error");return -1;}i++;if(len<512){printf("上传完毕\n");break;}else if(rbuf[1]==5){printf("upload error:%s\n",rbuf+4);}}}close(fd1);return 0;}int main(int argc, const char *argv[])
{int sfd=-1;if((sfd=socket(AF_INET,SOCK_DGRAM,0))==-1){perror("socket error");return -1;}printf("sfd=%d\n",sfd);struct sockaddr_in sin;sin.sin_family=AF_INET;sin.sin_port=htons(PORT);sin.sin_addr.s_addr=inet_addr(IP);socklen_t socklen=sizeof(sin);int menu=0;while(1){ system("clear");printf("****************\n");printf("*****1.下载*****\n");printf("*****2.上传*****\n");printf("*****3.退出*****\n");printf("****************\n");printf("请输入选项:");scanf("%d",&menu);getchar();//对菜单多分支选择switch(menu){case 1:{//下载功能do_download(sfd,sin);}break;case 2:{//上传功能do_upload(sfd,sin);}break;case 3:goto END;break;default:printf("输入错误,请重新输入\n");}printf("请输入任意键按回车结束\n");while(getchar()!='\n');}END: close(sfd);return 0;
}
运行结果: