1>机械臂
#include <head.h>
#define SER_POTR 8899
#define SER_IP "192.168.125.223"
int main(int argc, const char *argv[])
{
//创建套接字
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd==-1)
{
perror("");
return -1;
}
//链接
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(SER_POTR);
sin.sin_addr.s_addr=inet_addr(SER_IP);
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("connect error:");
return -1;
}
char red[5]={0xff,0x02,0x00,0x00,0xff};
unsigned char blue[5]={0xff,0x02,0x01,0x8a,0xff};
while(1)
{
char cmd;
printf("请输入命令:");
scanf("%c",&cmd);
getchar();
switch(cmd)
{
case 'w':
{
red[3]+=10;
send(cfd,red,sizeof(red),0);
break;
}
case 's':
{
red[3]-=10;
send(cfd,red,sizeof(red),0);
break;
}
case 'a':
{
blue[3]-=10;
send(cfd,blue,sizeof(blue),0);
break;
}
case 'd':
{
blue[3]+=10;
send(cfd,blue,sizeof(blue),0);
break;
}
default:
{
printf("输入有误\n");
}
}
}
return 0;
}
2> 使用tftp实现简单文件的上传
#include <head.h>
#define SER_PORT 69
#define SER_IP "192.168.125.223"
int link_file()
{
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd==-1)
{
perror("socket error");
return -1;
}
return sfd;
}
int filedownload()
{
//创建文件描述符用于下载传输过来的数据包
int wfd=-1;
if((wfd=open("./copy_file",O_WRONLY|O_CREAT|O_TRUNC,0664))==-1)
{
perror("open error:");
return -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);
socklen_t socklen=sizeof(sin);
int sfd=-1;
if((sfd=link_file())==-1)
{
printf("链接失败\n");
return -1;
}
//封装请求下载数据包
//第一块
char buf[516]="";
short *p1=(short*)buf;
*p1=htons(1);
//第二模块
printf("请输入你要下载的文件:");
char file[50];
scanf("%s",file);
char *p2=buf+2;
strcpy(p2,file);
//第三模块
char *p3=p2+strlen(p2)+1;
strcpy(p3,"octet");
int len=4+strlen(p2)+strlen(p3);
sendto(sfd,buf,len,0,(struct sockaddr*)&sin,sizeof(sin));
while(1)
{
int res=0;
if((res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&socklen))<0)
{
perror("recvfrom error:");
return -1;
}
char *data_point=buf+4;
short *cmd_point=(short*)buf;
short *block_point=(short*)(buf+2);
if(res<516)
{
write(wfd,data_point,res-4);
printf("文件拷贝完成!\n");
break;
}
write(wfd,data_point,512);
//完成了一次拷贝要向客户端发送ACK包
char ack[4];
short *p1=(short*)ack;
short *p2=(short*)(ack+2);
*p1=htons(4);
*p2=*block_point;
sendto(sfd,ack,4,0,(struct sockaddr*)&sin,sizeof(sin));
}
}
int onload()
{
int sfd=link_file();
system("ls");
printf("请问你要上传的文件是:");
char file[50];
scanf("%s",file);
//未完成
}
int main(int argc, const char *argv[])
{
while(1)
{
printf("==========欢迎使用文件下载客户端==========\n");
printf("1、下载文件\t2、上传文件\t0、退出\n");
printf("请输入你要执行的操作:");
int cmd;
scanf("%d",&cmd);
switch(cmd)
{
case 1:
{
filedownload();
break;
}
case 2:
{
//fileonload();
break;
}
case 0:
{
system("clear");
return 0;
}
default:
{
printf("输入有误\n");
}
}
system("clear");
}
return 0;
}