Exemple #1
0
// The remote Connections whant to start the transfer. 
void MyIOCP::PackageStartFileTransfer(CIOCPBuffer *pOverlapBuff,int nSize,ClientContext *pContext)
{
	#ifdef TRANSFERFILEFUNCTIONALITY
	StartSendFile(pContext->m_Socket);
	#endif 
}
//函数:接收文件套接字
//功能:在客户端向服务器请求下载或上传之后,服务器会主动与客户机连接,该函数即处理该连接的建立
//
void CPRJ3_0613Dlg::AcceptDataSocket(SOCKET s)
{
	int i;
	int retval;
	int addrlen;
	sockaddr_in remote;
	SOCKET new_sock;
	char* recv_buf;

	addrlen = sizeof(remote);
	new_sock = accept(s, (sockaddr*)&remote, &addrlen);
	if (new_sock == SOCKET_ERROR){
		MessageBox(TEXT("连接意外关闭!"), TEXT("接收连接错误"), MB_OK);
		return;
	}
	//接收到一个新的数据连接
	MessageBox(TEXT("接收到一个新的数据连接!"), TEXT("服务器"), MB_OK);

	recv_buf = (char *)malloc(BUF_SIZE);
	retval = recv(new_sock, recv_buf, BUF_SIZE, 0);
	if (retval <= 0){
		//接收错误
		MessageBox(TEXT("数据连接意外关闭!"), TEXT("数据连接错误"), MB_OK);
		closesocket(new_sock);
		return;
	}
	recv_buf[retval] = 0;

	FilePDU *file_pdu;
	file_pdu = (FilePDU*)recv_buf;
	if (file_pdu->type != FILE_PDU_TYPE){
		//数据连接收到错误PDU
		MessageBox(TEXT("数据连接收到错误PDU!"), TEXT("数据连接连接错误"), MB_OK);
		closesocket(new_sock);
		return;
	}

	if (file_pdu->No == GET_FILELIST_START){
		retval = StartRecvFileList(new_sock, file_pdu);
		return;
	}


	for (i = 0; i < SOCK_LIST_SIZE; i++){
		if (FileName[i] == file_pdu->buf){
			break;
		}
	}
	if (i == SOCK_LIST_SIZE){
		//报错,未找到
		MessageBox(TEXT("数据连接收到PDU中的文件名错误!"), TEXT("数据连接连接错误"), MB_OK);
		closesocket(new_sock);
		return;
	}

	sock_list[i] = new_sock;

	switch (file_pdu->No){
	case UP_FILE_START:
		StartSendFile(new_sock, i);
		break;
	case DOWN_FILE_START:
		StartRecvFile(new_sock, i);
		break;
	}
	free(recv_buf);	
}