机械臂
#include<myhead.h>
#define SER_IP "172.20.10.5" //服务器ip
#define SER_PORT 8888 //服务器端口号
#define CLI_IP "192.168.111.154" //客户端IP
#define CLI_PORT 9999 //客户端端口号int main(int argc, const char *argv[])
{int cfd;//创建用于通信的套接字文件描述符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;}printf("bind success\n");//链接服务器//填充要链接的服务器地址信息结构体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地址//链接服务器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("请输入要控制的机械臂:\nW/w红色增大,S/s红色减小\nD/d蓝色增大,A/a蓝色减小\n");scanf("%c",&key);getchar();switch(key){case 'W':case'w':{rbuf[3]+=2;//每次操作的角度偏移2if(rbuf[3]>=90){rbuf[3]=90;}send(cfd,rbuf,sizeof(rbuf),0);}break;case 'S':case 's':{rbuf[3]-=2;//红色减小if(rbuf[3]>=90){rbuf[3]=90;}send(cfd,rbuf,sizeof(rbuf),0);}break;case 'D':case'd':{bbuf[3]+=2;//每次操作的角度偏移2if(bbuf[3]>=90){bbuf[3]=90;}send(cfd,bbuf,sizeof(bbuf),0);}break;case 'A':case 'a':{bbuf[3]-=2;if(bbuf[3]>=90){bbuf[3]=90;}send(cfd,bbuf,sizeof(bbuf),0);}break;default:goto A;}}close(cfd);A:;
return 0;
}
文件的上传和下载
#include<myhead.h>
#define SER_IP "172.20.10.5"
#define SER_PORT 69int main(int argc, const char *argv[])
{//创建套接字int cfd=socket(AF_INET,SOCK_DGRAM,0);if(cfd==-1){perror("socket error");return -1;}printf("cfd=%d\n",cfd);//收发数据char pack[516]="";//定义容器,记录服务器的地址信息结构体struct sockaddr_in sin;sin.sin_family=AF_INET;sin.sin_port=htons(SER_PORT);sin.sin_addr.s_addr=inet_addr(SER_IP);//下载short *p1=(short *)pack;*p1=htons(1);//设置操作码char *p2=(char *)p1+1;//输入文件名char name[50]="";puts("请输入文件名:");fgets(name,sizeof(name),stdin);strcpy(p2,name);//文件名char *p4=p2+strlen(p2)+1;strcpy(p4,"octet");int packlen=4+strlen(p2)+strlen(p4);//请求包的大小//向服务器发送请求包sendto(cfd,pack,packlen,0,(struct sockaddr*)&sin,sizeof(sin));while(1){//收取数据包,每次以512大小进行读取,直到读取结束puts("1.上传");puts("2.下载");puts("请输入你想要进行的操作:");int option;scanf("%d",&option);getchar();switch(option){case 1://上传{ struct sockaddr_in cin;socklen_t socklen = sizeof(cin); //计算结构体的大小//以只读的方式打开文件int rfd = -1;if((rfd = open(name,O_RDONLY|O_CREAT|O_TRUNC,0664)) == -1){perror("open error");return -1;}short num = 0;while(1){//清空协议包bzero(pack,sizeof(pack));//接受发来的应答包int res = recvfrom(cfd,pack,sizeof(pack),0,(struct sockaddr*)&cin,&socklen);if(*p1 == ntohs(4)){//发送一个数据包*p1 = htons(3);num++;*(p1 + 1) = htons(num);//读取数据res = read(rfd, p1+2,512);sendto(cfd, pack, sizeof(pack),0,(struct sockaddr*)&cin,sizeof(cin));if(res == 0){printf("upload success\n");break;}}}close(rfd);}break;case 2:{struct sockaddr_in cin;socklen_t socklen = sizeof(cin); //打开文件int wfd = -1;if((wfd = open(name,O_WRONLY|O_CREAT|O_TRUNC,0664)) == -1){perror("open error");return -1;}while(1){//清空协议包bzero(pack, sizeof(pack));int res = recvfrom(cfd, pack, sizeof(pack), 0, (struct sockaddr*)&cin,&socklen);//判断操作码是否为3 数据内容是否为512字节if(*p1 == ntohs(3)){//写入文件write(wfd, pack+4, res-4);//回复sendto(cfd, pack, 4, 0,(struct sockaddr*)&cin, sizeof(cin));//数据内容小于512字if(res-4 < 512){printf("download success\n");break;}}}close(wfd);}break;}}close(cfd);return 0;}