网络 / day03 作业

一、TCP机械臂测试

代码

#include 

#define PORTSVC 8888
#define IPSVC "192.168.125.135"

#define PORTCLI 6666
#define IPCLI "192.168.250.100"

#define REDARM 0x00
#define BLUEARM 0x01


int main(int argc, const char *argv[])
{
	//1. create socket
	int cfd = -1;

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

	printf("cfd = %d\n", cfd);


	//端口号快速重用
	int reuse = -1;
	if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
	{
		perror("setsockopt error");
		return -1;
	}

	//2. binding
	struct sockaddr_in cin;
	cin.sin_family = AF_INET;
	cin.sin_port = htons(PORTCLI);
	cin.sin_addr.s_addr = inet_addr(IPCLI);

	if(bind(cfd, (struct sockaddr *)&cin, sizeof(cin)) == -1)
	{
		perror("bind error");
		return -1;

	}

	printf("bind success\n");


	//3. connect server
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORTSVC);
	sin.sin_addr.s_addr = inet_addr(IPSVC);

	if( connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) == -1)
	{
		perror("connect error");
		return -1;

	}

	//4. data send/recv
	char buf[5] = {0xff, 0x02, 0x00, 0x00, 0xff};
	/* 	0xffxff: 	起始结束协议,固定的;
		0x02: 		控制机械手臂协议,固定的;
		x: 		指定要操作的机械臂
    					0x00 红色摆臂
   			 			0x01 蓝色摆臂
		y: 		指定角度*/
	char value = 0;
	char *parm = buf + 2;
	char *panglered = buf + 3;
	unsigned char *pangleblue = buf + 3;

	char anglered = 0x00;
	unsigned char angleblue = 0x5a;
	

	while(1)
	{

		value = getchar();
		getchar();
		switch(value)
		{
		case 'w':
		case 'W': //-90, 90
			{

				*parm = REDARM;
				anglered += 0x2;
				*panglered = anglered;
				if(*panglered > 0x5a)
				{
					anglered = 0x5a;
					*panglered = anglered;
				}				 

			}
			break;
		case 's':
		case 'S':
			{
				*parm = REDARM;
				anglered -= 0x2;
				*panglered = anglered;
				if(*panglered < -0x5a)
				{
					anglered = 0x5a;
					*panglered = anglered;
				}	
			
			}
			break;
		case 'd':
		case 'D':
			{
				*parm = BLUEARM;
				angleblue += 0x02;
				*pangleblue = angleblue;
				if(*pangleblue > 0xb4)
				{
					angleblue = 0xb4;
					*pangleblue = angleblue;
				}	
			
			}
			break;

		case 'a':
		case 'A':
			{
				*parm = BLUEARM;
				angleblue -= 0x02; 
				*pangleblue = angleblue;
				if(*pangleblue < 0x0)
				{
					angleblue = 0x0;
					*pangleblue = angleblue;
				}	
			
			}
			break;

		}

		//send data to svc
		send(cfd, buf, sizeof(buf), 0);

		/*/data recv
		//bzero(wbuf, sizeof(wbuf));

		//int res = recv(cfd, wbuf, sizeof(wbuf), 0);
		/if(res == 0)
		{
			printf("server is off line\n");
			break;

		}

		printf("[%s:%d]: %s\n", IPSVC, PORTSVC, wbuf);*/

	}

	close(cfd);


	return 0;
}

二、基于UDP的TFTP文件传输

代码

#include

//实现下载功能
int do_download(int cfd, struct sockaddr_in sin)
{
	//定义变量存储下载请求包
	char buf[516] = "";
	//定义变量存储文件名
	char fileName[40] = "";

	int wfd = -1;



	printf("请输入文件名:");
	scanf("%s", fileName);
	getchar();

	if((wfd=open(fileName, O_WRONLY | O_CREAT | O_TRUNC , 0664 )) == -1)
	{
		perror("open error");
		return -1;

	}

	//组装请求包
	short *p1 = (short *)buf;
	*p1 = htons(1);                 //表明要下载

	char *p2 = buf+2;           //文件名段
	strcpy(p2, fileName);

	char *p4 = p2+strlen(p2)+1;    //模式段
	strcpy(p4, "octet");

	int size = 4 + strlen(p2) + strlen(p4);      //要发送数据的大小


	//向服务器发送下载请求
	if(sendto(cfd, buf, size, 0, (struct sockaddr*)&sin, sizeof(sin)) == -1)
	{
		perror("sendto error");
		return -1;
	}

	printf("请求成功\n");

	//循环接收回复服务器发来的消息

	while(1)
	{
		short *popcode = (short *)buf;
		short *pbnum =(short *)(buf + 2);
		char *pdata = (char *)(buf + 4);

		recvfrom(cfd, buf, sizeof(buf), 0,NULL, NULL);

		//判断操作码
		if( ntohs(*popcode) == 3 )
		{
			int datalen =strlen(pdata);
			printf("datalen=%d, data=%s\n", datalen, pdata );
			write(wfd, pdata, datalen );

			//发送确认
			*popcode == 4;
			sendto(cfd, buf, sizeof(short), 0, (struct sockaddr*)&sin, sizeof(sin));



			if(datalen<512)
			{
				break;
			}


		}

	}
	close(wfd);
	close(cfd);

}




int main(int argc, const char *argv[])
{
	if(argc != 2)
	{
		printf("input error\n");
		printf("usage:./a.out ip\n");
		return -1;
	}

	//1、创建套接字
	int cfd = socket(AF_INET, SOCK_DGRAM, 0);
	if(cfd == -1)
	{
		perror("socket error");
		return -1;
	}

	//2、填充服务器地址信息结构体
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(69);
	sin.sin_addr.s_addr = inet_addr(argv[1]);

	int mune = -1;


	while(1)
	{
		system("clear");                     //清屏
		printf("\t\t======1、下载=======\n");
		printf("\t\t======2、上传=======\n");
		printf("\t\t======0、退出=======\n");

		printf("请输入功能:");
		scanf("%d", &mune);
		getchar();

		//多分支选择
		switch(mune)
		{
		case 1:
			{
				do_download(cfd, sin);
			}
			break;

		case 2:
			{
				//do_upload();
			}
			break;
		case 0:
			goto POS;
		default:printf("输入功能有误,请重新输入\n");
		}

		//阻塞
		printf("输入任意键,按回车清空:");
		while(getchar() != '\n');

	}

POS:
	//关闭套接字
	close(cfd);


	return 0;
}

运行结果

		======1、下载=======
		======2、上传=======
		======0、退出=======
请输入功能:1
请输入文件名:test.txt
请求成功
datalen=40, data=hello


hello


world


world

输入任意键,按回车清空:

你可能感兴趣的:(网络,c语言,linux)