资源简介
停止等待协议,用来发送接受不同的文件。用C++语言写的。
代码片段和文件信息
// receiver.cpp : 定义控制台应用程序的入口点。
//
#include “stdafx.h“
#include
#include
#include “../header/ARQ.h“
#include “../header/Exception.h“
// 服务器端口
#define SERVER_PORT 2280
#pragma comment(lib“ws2_32.lib“)//设置link时的lib库
using namespace std;
SOCKET PrimaryUDP;
char FileSavePath[MAX_PATH];
void InitWinSock()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2 2) &wsaData) != 0)
{
throw Exception(“Windows sockets 2.2 startup unsuccessful“);
}
else{
printf(“Using %s (Status: %s)\n“
wsaData.szDescription wsaData.szSystemStatus);
printf(“with API versions %d.%d to %d.%d\n\n“
LOBYTE(wsaData.wVersion) HIBYTE(wsaData.wVersion)
LOBYTE(wsaData.wHighVersion) HIBYTE(wsaData.wHighVersion));
}
}
void mksock(int type)
{
PrimaryUDP = socket(AF_INET type 0);
if (PrimaryUDP < 0)
{
throw Exception(“create socket error“);
}
}
void BindSock()
{
sockaddr_in sin;
sin.sin_addr.S_un.S_addr = INADDR_ANY;
sin.sin_family = AF_INET;
sin.sin_port = htons(SERVER_PORT);
if (bind(PrimaryUDP (struct sockaddr*)&sin sizeof(sin)) < 0)
throw Exception(“bind error“);
}
DWORD WINAPI ARecv(LPVOID lpParam)
{
FILE * file = NULL;
sockaddr_in remote;
int sinlen = sizeof(remote);
BSC bufferbsc;
bsc.header = STX;bsc.tail = ETX;
memset(bsc.data 0 MAXBSCLENGTH);
int iread = 0;
unsigned long dwReceived = 0;
bool number = true;//发送方的数据开始发送时的序号设为0为了判断是不是第一次一段数据所以这里标为1
while (true)
{
iread = recvfrom(PrimaryUDP(char*)&buffersizeof(buffer)0(sockaddr*)&remote&sinlen);
if (SOCKET_ERROR == iread || buffer.header != STX || buffer.tail != ETX)
{
// 数据错误发送负应答
cout<<“received a error data“< bsc.bcc = NAK;bsc.number=false;//number这时没有实际的意义
sendto(PrimaryUDP(char*)&bscsizeof(bsc)0(sockaddr*)&remotesinlen);
continue;
}
if (buffer.number == number)
{
// 重复收到数据发送应答消息
cout<<“received a repeat data“< bsc.bcc = ACK;
bsc.number = buffer.number;
sendto(PrimaryUDP(char*)&bscsizeof(bsc)0(sockaddr*)&remotesinlen);
continue;
}
switch(buffer.bcc)
{
case ENQ:
{
number = !number;//准备接收下一段数据
// 文件请求
cout<<“received a file request messagefilename:“< // 打开文件
if(strcmp((FileSavePath+strlen(FileSavePath)-1)“\\“)!=0)
strcat(FileSavePath“\\“);
strcat(FileSavePathbuffer.data);
if((file = fopen(FileSavePath “ab“)) == NULL)
{
cout<<“file open failed“< return -1;
}
break;
}
case SYN:
{
number = !number;
// 文字发送中
int i =0;
if((i=fwrite(buffer.data sizeof(char) sizeof(buffer.data) file)) <= 0)
{
cout << “write failed“ << endl;
return -1;
}
dwReceived += i;
cout<<“write:“< break;
}
case EOT:
{
number = !number;
// 文件发送完毕
cout<<“file receive
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 52224 2008-02-29 11:31 ARQDemo\ARQDemo.ncb
文件 1418 2008-02-28 12:33 ARQDemo\ARQDemo.sln
..A..H. 12288 2008-02-29 11:31 ARQDemo\ARQDemo.suo
文件 667 2009-11-05 14:10 ARQDemo\header\ARQ.h
文件 403 2008-02-14 09:25 ARQDemo\header\Exception.h
文件 2171904 2010-04-11 21:27 ARQDemo\receiver\Debug\receiver.bsc
文件 548951 2010-04-11 21:27 ARQDemo\receiver\Debug\receiver.exe
文件 800252 2010-04-11 21:27 ARQDemo\receiver\Debug\receiver.ilk
文件 204147 2010-04-11 21:27 ARQDemo\receiver\Debug\receiver.obj
文件 6487648 2010-04-11 21:27 ARQDemo\receiver\Debug\receiver.pch
文件 1123328 2010-04-11 21:27 ARQDemo\receiver\Debug\receiver.pdb
文件 0 2010-04-11 21:27 ARQDemo\receiver\Debug\receiver.sbr
文件 205824 2010-04-11 21:27 ARQDemo\receiver\Debug\vc60.idb
文件 143360 2010-04-11 21:27 ARQDemo\receiver\Debug\vc60.pdb
文件 997 2008-02-28 12:33 ARQDemo\receiver\ReadMe.txt
文件 3912 2010-04-11 21:27 ARQDemo\receiver\receiver.cpp
文件 3429 2009-11-04 18:56 ARQDemo\receiver\receiver.dsp
文件 541 2009-11-04 18:56 ARQDemo\receiver\receiver.dsw
文件 41984 2010-04-11 21:28 ARQDemo\receiver\receiver.ncb
文件 49664 2010-04-11 21:28 ARQDemo\receiver\receiver.opt
文件 1335 2010-04-11 21:27 ARQDemo\receiver\receiver.plg
文件 3958 2008-02-28 12:51 ARQDemo\receiver\receiver.vcproj
文件 212 2008-02-28 12:33 ARQDemo\receiver\stdafx.cpp
文件 195 2008-02-28 12:33 ARQDemo\receiver\stdafx.h
文件 2171904 2009-11-14 20:12 ARQDemo\sender\Debug\sender.bsc
文件 548957 2009-11-14 20:12 ARQDemo\sender\Debug\sender.exe
文件 807356 2009-11-14 20:12 ARQDemo\sender\Debug\sender.ilk
文件 206112 2009-11-14 20:12 ARQDemo\sender\Debug\sender.obj
文件 6475360 2009-11-05 14:20 ARQDemo\sender\Debug\sender.pch
文件 1123328 2009-11-14 20:12 ARQDemo\sender\Debug\sender.pdb
............此处省略22个文件信息
评论
共有 条评论