day36 1218

作业1:tftp

  1 #include
  2 
  3 //实现下载功能
  4 int do_download(int cfd, struct sockaddr_in sin)
  5 {
  6     //定义变量存储下载请求包
  7     char buf[516] = "";
  8     //定义变量存储文件名
  9     char fileName[40] = "";
 10 
 11     printf("请输入文件名:");
 12     scanf("%s", fileName);
 13     getchar();
 14 
 15     //组装请求包
 16     short *p1 = (short *)buf;
 17     *p1 = htons(1);                 //表明要下载
 18 
 19     char *p2 = buf+2;           //文件名段
 20     strcpy(p2, fileName);
 21 
 22     char *p4 = p2+strlen(p2)+1;    //模式段
 23     strcpy(p4, "octet");
 24 
 25     int size = 4 + strlen(p2) + strlen(p4);      //要发送数据的大小
 26 
 27     //向服务器发送下载请求
 28     if(sendto(cfd, buf, size, 0, (struct sockaddr*)&sin, sizeof(sin)) == -1)
 29     {
 30         perror("sendto error");
 31         return -1;
 32     }
 33 
 34     printf("请求成功\n");
 35 
 36     struct sockaddr_in newsin;
 37     socklen_t socklen = sizeof(newsin);
 38 
 39     int res = 0;
 40     int fd = -1;
 41     fd = open(fileName,O_WRONLY|O_CREAT|O_TRUNC,0664);
 42     PERROR_INFO(fd);
 43 
 44     //循环接收回复服务器发来的消息
 45     while (1)
 46     {
 47         bzero(buf,sizeof(buf));
 48         res = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr *)&newsin,&socklen);
 49 
 50         //printf("*p1 = %d\n",ntohs(*p1));
 51 
 52         if (ntohs(*p1) == 3)
 53         {
 54             write(fd,buf+4,res-4);
 55 
 56             buf[1] = 4;
 57 
 58             if(sendto(cfd,buf,4,0,(struct sockaddr*)&newsin,sizeof(newsin)) == -1)
 59             {
 60                 perror("sendto error");
 61                 return -1;
 62             }
 63         }
 64         else if (buf[1] == 5)
 65         {
 66             printf("5 error\n");
 67             return -1;
 68         }
 69 
 70         if (res < 516)
 71         {
 72             puts("file is clone over!!!");
 73             close(fd);
 74             break;
 75         }
 76 
 77     }
 78 
 79 }
 80 
 81 int main(int argc, const char *argv[])
 82 {
 83     if(argc != 2)
 84     {
 85         printf("input error\n");
 86         printf("usage:./a.out ip\n");
 87         return -1;
 88     }
 89 
 90     //1、创建套接字
 91     int cfd = socket(AF_INET, SOCK_DGRAM, 0);
 92     if(cfd == -1)
 93     {
 94         perror("socket error");
 95         return -1;
 96     }
 97 
 98     //2、填充服务器地址信息结构体
 99     struct sockaddr_in sin;
100     sin.sin_family = AF_INET;
101     sin.sin_port = htons(69);
102     sin.sin_addr.s_addr = inet_addr(argv[1]);
103 
104     int mune = -1;
105     while(1)
106     {
107         system("clear");                     //清屏
108         printf("\t\t======1、下载=======\n");
109         printf("\t\t======2、上传=======\n");
110         printf("\t\t======0、退出=======\n");
111 
112         printf("请输入功能:");
113         scanf("%d", &mune);
114         getchar();
115 
116         //多分支选择
117         switch(mune)
118         {
119         case 1:
120             {
121                 do_download(cfd, sin);
122             }
123             break;
124 
125         case 2:
126             {
127                 //do_upload();
128             }
129             break;
130         case 0:
131             goto POS;
132         default:printf("输入功能有误,请重新输入\n");
133         }
134 
135         //阻塞
136         printf("输入任意键,按回车清空:");
137         while(getchar() != '\n');
138 
139     }
140 
141 POS:
142     //关闭套接字
143     close(cfd);
144 
145 
146     return 0;
147 }                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
148 
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
~                            

效果
day36 1218_第1张图片

作业2: 控制机械臂

  3 #define SERIP "192.168.125.32"
  4 
  5 #define CLIPORT 6666
  6 #define CLIIP "192.168.125.65"
  7 
  8 int main(int argc, const char *argv[])
  9 {
 10     int ret = -1;
 11     int cfd = -1;
 12     cfd = socket(AF_INET,SOCK_STREAM,0);
 13     PERROR_INFO(cfd);
 14 
 15     printf("cfd = %d\n",cfd);
 16 
 17 
 18     int reuse = 1;
 19     ret = setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));
 20     PERROR_INFO(ret);
 21 
 22     /*
 23     struct sockaddr_in cin;
 24     cin.sin_family = AF_INET;
 25     cin.sin_port = htons(CLIPORT);
 26     cin.sin_addr.s_addr = inet_addr(CLIIP);
 27 
 28     ret = bind(cfd,(struct sockaddr *)&cin,sizeof(cin));
 29     PERROR_INFO(ret);
 30     */
 31 
 32     struct sockaddr_in sin;
 33     sin.sin_family = AF_INET;
 34     sin.sin_port = htons(SERPORT);
 35     sin.sin_addr.s_addr = inet_addr(SERIP);
 36 
 37     ret = connect(cfd,(struct sockaddr *)&sin,sizeof(sin));
 38     PERROR_INFO(ret);
 39 
 40 
 41     unsigned char blue_buf[5] = {0xff,0x02,0x01,90,0xff};
 42     char red_buf[5] = {0xff,0x02,0x00,0,0xff};
 43 
 44     while (1)
 45     {
 46         //bzero(wbuf,sizeof(wbuf));
 47         char order = fgetc(stdin);
 48         if (order == 'w'||order == 'W')
 49         {
 50             red_buf[3] += 5;
 51             if (red_buf[3] > 90)
 52             {
 53                 red_buf[3] = 90;
 54             }
 55             send(cfd,red_buf,sizeof(red_buf),0);
 56         }
 57         else if(order == 's' || order == 'S')
 58         {
 59             red_buf[3] -= 5;
 60             if (red_buf[3] < -90)
 61             {
 62                 red_buf[3] = -90;
 63             }
 64             send(cfd,red_buf,sizeof(red_buf),0);
 65         }
 66         else if (order == 'a' || order == 'A')
 67         {
 68             blue_buf[3] += 5;
 69             if (blue_buf[3] > 180)
 70             {
 71                 blue_buf[3] = 180;
 72             }
 73             send(cfd,blue_buf,sizeof(blue_buf),0);                                                                                                                                                                                                                                                            
 74         }
 75         else if (order == 'd' || order == 'D')
 76         {
 77 
 78             if (blue_buf[3] < 5)
 79             {
 80                 blue_buf[3] = 0;
 81             }
 82             else
 83             {
 84                 blue_buf[3] -= 5;
 85             }
 86             send(cfd,blue_buf,sizeof(blue_buf),0);
 87         }
 88 
 89 
 90 
 91 
 92 
 93     }
 94     close(cfd);
 95 
 96     return 0;
 97 }
~            

思维导图

你可能感兴趣的:(算法)