1.TCP机械臂测试
代码:
#include <myhead.h>
#define SER_IP "192.168.43.185" //服务器ip
#define SER_PORT 8888 //服务器端口号
#define CLI_IP "192.168.153.128" //客户端IP
#define CLI_PORT 9999 //客户端端口号
int main(int argc, const char *argv[])
{
//1、创建用于通信的套接字文件描述符
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if (cfd == -1)
{
perror("socket error");
return -1;
}
printf("cfd = %d\n", cfd); //3
//2、绑定(非必须)
//2.1 填充地址信息结构体
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(CLI_PORT);
cin.sin_addr.s_addr = inet_addr(CLI_IP);
//2.2 绑定
if (bind(cfd, (struct sockaddr *)&cin, sizeof(cin)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
//3、连接服务器
//3.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); //ip地址
//3.2 连接服务器
if (connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("connect error");
return -1;
}
printf("connect success\n");
//4、数据收发
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);
printf("请键入W、S、A、D进行控制:");
while (1)
{
char skb = Key();
if (skb == 27)
printf("\n请键入W、S、A、D进行控制(键入Q退出控制!):\n");
switch (skb)
{
case 'W':
case 'w':
{
int buf_size1 = sizeof(rbuf);
r_angleUp(cfd, rbuf, buf_size1, skb);
}
break;
case 'S':
case 's':
{
int buf_size1 = sizeof(rbuf);
r_angleDown(cfd, rbuf, buf_size1, skb);
}
break;
case 'D':
case 'd':
{
int buf_size2 = sizeof(bbuf);
b_angleUp(cfd, bbuf, buf_size2, skb);
}
break;
case 'A':
case 'a':
{
int buf_size2 = sizeof(rbuf);
b_angleDown(cfd, bbuf, buf_size2, skb);
}
break;
case 'Q':
{
printf("\n控制已结束\n");
return -1;
}
break;
}
}
//5、关闭套接字
close(cfd);
return 0;
}
char Key();
void r_angleUp(int fd, char buf[], int buf_size, char skb); //红色臂向右偏移
void r_angleDown(int fd, char buf[], int buf_size, char skb); //红色臂向左偏移
void b_angleUp(int fd, char buf[], int buf_size, char skb); //蓝色色臂向右偏移
void b_angleDown(int fd, char buf[], int buf_size, char skb); //蓝色臂向左偏移
char Key()
{
char in;
struct termios new_settings; //保存用于操作的终端属性
struct termios stored_settings; //保存当前的终端属性,以便之后恢复
tcgetattr(0, &stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
tcgetattr(0, &stored_settings);
new_settings.c_cc[VMIN] = 1; //非规范模式读取时的最小字符数--VMIN
tcsetattr(0, TCSANOW, &new_settings); //不等数据传输完毕就立即改变属性--TCSANOW
in = getchar();
tcsetattr(0, TCSANOW, &stored_settings);
return in;
}
void r_angleUp(int fd, char buf[], int buf_size, char skb)
{
if (skb != 27) //skb==ECS时退出控制
{
buf[3] += minStep; //每次操作的角度偏移minStep度
if (buf[3] >= 90)
{
buf[3] = 90;
}
send(fd, buf, buf_size, 0);
}
return;
}
void r_angleDown(int fd, char buf[], int buf_size, char skb)
{
if (skb != 27)
{
buf[3] -= minStep;
if (buf[3] <= -90)
{
buf[3] = -90;
}
send(fd, buf, buf_size, 0);
}
return;
}
void b_angleUp(int fd, char buf[], int buf_size, char skb)
{
if (skb != 27)
{
buf[3] += minStep;
if (buf[3] >= 180)
{
buf[3] = 180;
}
send(fd, buf, buf_size, 0);
}
return;
}
void b_angleDown(int fd, char buf[], int buf_size, char skb)
{
if (skb != 27)
{
buf[3] -= minStep;
if (buf[3] <= 0)
{
buf[3] = 0;
}
send(fd, buf, buf_size, 0);
}
return;
}
2.基于UDP的TFTP文件传输
代码:
#include<myhead.h>
#define SER_IP "192.168.43.185"
#define SER_PORT 69
#define IP "192.168.153.128"
#define PORT 9999
enum mode
{
TFTP_READ = 1,
TFTP_WRITE = 2,
TFTP_DATA = 3,
TFTP_ACK = 4,
TFTP_ERR = 5
};
void get_filename(char* filename, int size)
{
bzero(filename, size);
printf("请输入文件名:");
fgets(filename, size, stdin);
filename[strlen(filename) - 1] = 0;
}
void rw_request(char* pack, int pack_size, char* filename, int mode, int* packlen)
{
bzero(pack, pack_size);
short* p1 = (short*)pack;
*p1 = htons(mode);
char* p2 = (char*)(p1 + 1);
strcpy(p2, filename);
char* p4 = p2 + strlen(p2) + 1;
strcpy(p4, "octet");
*packlen = 4 + strlen(p2) + strlen(p4);
}
void pack_data(char* pack, int num, char* rbuf, int len, int* packlen)
{
bzero(pack, sizeof(pack));
short* p1 = (short*)pack;
*p1 = htons(TFTP_DATA);
short* p2 = p1 + 1;
*p2 = htons(num);
char* p4 = (char*)(p2 + 1);
for (int i = 0; i < len; i++)
{
*(p4 + i) = rbuf[i];
}
*packlen = 4 + len;
}
void pack_ack(char* ack, int num)
{
bzero(ack, 4);
short* a = (short*)ack;
*a = htons(TFTP_ACK);
*(a + 1) = htons(num);
}
void pack_errmsg(char* pack, char* msg, int* packlen)
{
bzero(pack, sizeof(pack));
short* p = (short*)pack;
*p = htons(TFTP_ERR);
*(p + 1) = htons(0);
char* errmsg = (char*)(p + 2);
strcpy(errmsg, msg);
*packlen = 4 + strlen(errmsg);
}
void client_recv(int sfd, struct sockaddr_in* sin, socklen_t* socklen)
{
char filename[128];
get_filename(filename, sizeof(filename));
char pack[516] = "";
int packlen = 0;
rw_request(pack, sizeof(pack), filename, TFTP_READ, &packlen);
sendto(sfd, pack, packlen, 0, (struct sockaddr*)sin, *socklen);
int fd = -1;
if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664)) == -1)
{
perror("create file error");
return;
}
char wbuf[512] = "";
int block_num = 0;
while (1)
{
bzero(pack, sizeof(pack));
packlen = recvfrom(sfd, pack, sizeof(pack), 0, (struct sockaddr*)sin, socklen);
short* p = (short*)pack;
short code = ntohs(*p);
short num = ntohs(*(p + 1));
if (3 == code && num == ++block_num)
{
write(fd, pack + 4, packlen - 4);
char ack[4];
pack_ack(ack, block_num);
sendto(sfd, ack, 4, 0, (struct sockaddr*)sin, *socklen);
if (packlen < 512)
{
printf("下载完成\n");
close(fd);
break;
}
}
}
}
void client_send(int sfd, struct sockaddr_in* sin, socklen_t* socklen)
{
char filename[128];
get_filename(filename, sizeof(filename));
char pack[516] = "";
int packlen = 0;
rw_request(pack, sizeof(pack), filename, TFTP_WRITE, &packlen);
sendto(sfd, pack, packlen, 0, (struct sockaddr*)sin, *socklen);
int fd = -1;
if ((fd = open(filename, O_RDONLY)) == -1)
{
perror("open error");
return;
}
char ack[4];
char rbuf[512] = "";
int len;
while (1)
{
recvfrom(sfd, ack, 4, 0, (struct sockaddr*)sin, socklen);
short* a = (short*)ack;
short code = ntohs(*a);
short num = ntohs(*(a + 1));
if (4 == code && (len = read(fd, rbuf, sizeof(rbuf))) > 0)
{
pack_data(pack, num + 1, rbuf, len, &packlen);
sendto(sfd, pack, packlen, 0, (struct sockaddr*)sin, *socklen);
bzero(rbuf, sizeof(rbuf));
}
else
{
printf("上传成功\n");
break;
}
}
}
int main(int argc, char const *argv[])
{
int sfd = -1;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sfd)
{
perror("socket error");
return -1;
}
printf("sfd = %d\n", sfd);
int reuse = 1;
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
{
perror("setsockopt error");
return -1;
}
printf("端口号快速重用成功\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);
socklen_t socklen = sizeof(sin);
printf("1.上传\n");
printf("2.下载\n");
printf("0.退出\n");
printf("请输入:");
int n;
scanf("%d", &n);
getchar();
if (1 == n)
{
client_send(sfd, &sin, &socklen);
}
else if (2 == n)
{
client_recv(sfd, &sin, &socklen);
}
else
{
printf("输入错误\n");
}
close(sfd);
return 0;
}
结果: