1.通信协议
实例:文件传输
A发送一个文件给B
(1)把一个文件分块传送,每块最大1024字节;
(2)数据包格式:前16个字节表示指令
(3)01:开始传送 02:数据包 F1:结束传输
发送方:
#include <stdio.h>
#include <string.h>
#include "Endian.h"
#include "osapi/osapi.h"
int main()
{
printf("发送方: port=9000 ...\n");
OS_UdpSocket sock;
sock.Open();
// 回车后,开始传送
printf("press enter to send ...");
getchar();
FILE* fp = fopen("123.rar", "rb");
unsigned char buf[1024 + 16];// unsigned char
OS_SockAddr peer("127.0.0.1", 9001); // 对方地址
// 指令:开始传送
buf[0] = 0x01;//01代表开始传输
sock.SendTo(buf, 16, peer);
int count = 0;
while(1)
{
int n = fread(buf + 16, 1, 1024, fp);
if(n <= 0) break;
// 数据包
buf[0] = 0x02;//十六个字节存放状态
sock.SendTo(buf, n + 16, peer);