作业--day32

发布时间:2023年12月18日

机械臂

#include <myhead.h>

#define PORT 8888
#define IP "192.168.125.59"

int main(int argc, const char *argv[])
{
	int sfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sfd == -1){
		perror("socket error");
		return -1;
	}

	int reuse = -1;
	if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1){
		perror("setsockopt 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(sfd, (struct sockaddr *)&sin, sizeof(sin)) == -1){
		perror("connect error");
		return -1;
	}
	puts("连接成功");

	char rbuf[5] = {0xff, 0x02, 0x00, 0x00, 0xff};
	unsigned char bbuf[5] = {0xff, 0x02, 0x01, 0x00, 0xff};
	while(1){

		char c = fgetc(stdin);
		switch(c){
			case 'W':
			case 'w':
			{
				rbuf[3] += 2;
				if(rbuf[3] > 90){
					rbuf[3] = 90;
				}
				send(sfd, rbuf, sizeof(rbuf), 0);
			}
				break;
			case 'S':
			case 's':
			{
				rbuf[3] -= 2;
				if(rbuf[3] < -90){
					rbuf[3] = -90;
				}
				send(sfd, rbuf, sizeof(rbuf), 0);

			}
				break;
			case 'A':
			case 'a':
			{
				bbuf[3] += 2;
				if(bbuf[3] > 180){
					bbuf[3] = 180;
				}
				send(sfd, bbuf, sizeof(bbuf), 0);

			}
				break;
			case 'D':
			case 'd':
			{
				if(bbuf[3] < 2){
					bbuf[3] = 0;
				}else{
					bbuf[3] -= 2;
				}
				send(sfd, bbuf, sizeof(bbuf), 0);

			}
		}
		if(c == 'e' || c =='E'){
			break;
		}

	}
	close(sfd);
	return 0;
}

基于UDP的tftp文件下载

#include <myhead.h>

#define PORT 69
#define IP "192.168.125.59"

int down(const char *);
int up(const char *);

int main(int argc, const char *argv[])
{

	while(1){
		system("clear");
		//打印菜单
		puts("***************功能**************");
		puts("*************1. 下载*************");
		puts("*************2. 上传*************");
		puts("*************3. 退出*************");

		int choose = -1;
		char filename[128] = "";

		scanf("%d", &choose);
		switch(choose){
		case 1:
			{
				printf("输入文件名>>");
				scanf(" %[^\n]", filename);
				down(filename);

			}
			break;
		case 2:
			{
				printf("输入文件名>>");
				scanf(" %[^\n]", filename);
				up(filename);
			}
			break;
		case 3:

			return 0;

		default:
			puts("输入错误");
		}
	}
	
	return 0;
}

int down(const char *filename){

	int cfd = socket(AF_INET, SOCK_DGRAM, 0);
	if(cfd == -1){
		perror("socket error");
		return -1;
	}

	char buf[516];
	bzero(buf, sizeof(buf));
	//设置为下载(读)操作
	buf[1] = 1;
	//将文件名加入协议包中
	strcpy(buf+2, filename);
	
	//模式
	char mod[6] = "octet";

	//将模式字符串加入协议包中
	strcpy(buf+2+strlen(filename)+1, mod);

	//统计协议包字节数总大小
	int size = 2+strlen(filename)+1+strlen(mod)+1;

	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);
	if(sendto(cfd, buf, size, 0, (struct sockaddr *)&sin, sizeof(sin)) == -1){
		perror("sendto error");
		return -1;
	}

	puts("请求成功");

	int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);

	while(1){
		
		bzero(buf, sizeof(buf));
		int res = -1;
		if((res = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, &socklen)) == -1){
			perror("recvfrom error");
			return -1;
		}

		if(buf[1] == 3){
			if(res < 516){

				write(fd, buf+4, res-4);
				buf[1] = 4;
				sendto(cfd, buf, 4, 0, (struct sockaddr *)&sin, sizeof(sin));
				break;
			
			}else{
				
				write(fd, buf+4, 512);
				buf[1] = 4;
				sendto(cfd, buf, 4, 0, (struct sockaddr *)&sin, sizeof(sin));

			}
		}else{
			puts("未知错误");
			close(fd);
			close(cfd);
			return -1;
		}
	}

	close(fd);
	close(cfd);
	return 0;
}
int up(const char *filename){
}

在这里插入图片描述

思维导图

在这里插入图片描述

文章来源:https://blog.csdn.net/qq_39831963/article/details/135071160
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。