⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netreceivers.cpp

📁 用UDP写的可靠传输程序源代码,非常有借鉴意义,适合互连网通讯
💻 CPP
字号:
// CCTPNetReceiver - CTP reciever
// CTCPNetReceiver - TCP reciever
// CUDPNetReceiver - UDP reciever
// They partly duplicate each other to allow to customize support of every
// protocol easily
// Implementation file
//
// (c) Lev Naumov, CAMEL Laboratory
// E-mail: camellab@mail.ru
// For more information see http://camel.ifmo.ru or
// http://www.codeproject.com/internet/ctp.asp
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "NetBasic.h"
#include "NetReceivers.h"
#include "CTPNet.h"
#include "TCPNet.h"
#include "UDPNet.h"
#include "CTPDemo.h"
#include "CTPStatusDlg.h"
#include "CTPDemoDlg.h"

void CCTPNetReceiver::OnReceive(void* data)
{
    // Typecast variables
    CCTPReceivedData* rd=(CCTPReceivedData*)data;
    CCTPDemoDlg* dlg=(CCTPDemoDlg*)m_pMainWnd;
    // Other necessary variables
    CString s="";
    char ips[16];
    char timestamp[22];

    if (rd->command==NET_TEXT) {
        // Handle NET_TEXT command

        rd->from.GetString(ips);
        CCTPErrorInfo::GetTimeStamp(timestamp);
        s.Format("%s CTP> Recieve text from %s: size %d\n",timestamp,ips,rd->size);
        dlg->GetDlgItem(IDC_TEXTRECV)->SetWindowText(rd->pBuf);
    } else if (rd->command==NET_FILE) {
        // Handle NET_FILE command

        rd->from.GetString(ips);
        CCTPErrorInfo::GetTimeStamp(timestamp);
        s.Format("%s CTP> Recieve file from %s: size %d\n",timestamp,ips,rd->size);
        
        // Ask for filename
        CFileDialog fd(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_PATHMUSTEXIST,"All files (*.*)|*.*||");
        if (fd.DoModal()==IDOK) {
            // Copy recieved data to file
            CStdioFile file;
            file.Open(fd.GetPathName(),CFile::typeBinary|CFile::modeCreate|CFile::modeWrite);
            file.Write(rd->pBuf,(UINT)rd->size);
            file.Close();
        }
    }

    // Prepend text to log
    CString text;
    dlg->GetDlgItem(IDC_LOG)->GetWindowText(text);
    dlg->GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}

void CCTPNetReceiver::OnError(void* data)
{
    // Typecast variables
    CCTPErrorInfo* ei=(CCTPErrorInfo*)data;
    CCTPDemoDlg* dlg=(CCTPDemoDlg*)m_pMainWnd;

    // Generate error information text
    CString s;
    char ips[16];
    ei->addr.GetString(ips);
    s.Format("%s CTP> Error from %s: type %d, WinSocket error code %d, command %d\n",ei->timestamp,ips,ei->type,ei->code,ei->command);

    // Prepend text to log
    CString text;
    dlg->GetDlgItem(IDC_LOG)->GetWindowText(text);
    dlg->GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}

void CTCPNetReceiver::OnReceive(void* data)
{
    // Typecast variables
    CTCPReceivedData* rd=(CTCPReceivedData*)data;
    CCTPDemoDlg* dlg=(CCTPDemoDlg*)m_pMainWnd;
    // Other necessary variables
    CString s="";
    char ips[16];
    char timestamp[22];

    if (rd->pBuf[0]==0) {
        // Handle arrived text (first byte is zero)

        rd->from.GetString(ips);
        CCTPErrorInfo::GetTimeStamp(timestamp);
        s.Format("%s TCP> Recieve text from %s: size %d\n",timestamp,ips,rd->size);
        dlg->GetDlgItem(IDC_TEXTRECV)->SetWindowText(&(rd->pBuf[1]));
    } else {
        // Handle arrived file (first byte is not zero)

        rd->from.GetString(ips);
        CCTPErrorInfo::GetTimeStamp(timestamp);
        s.Format("%s TCP> Recieve file from %s: size %d\n",timestamp,ips,rd->size);
        
        // Ask for filename
        CFileDialog fd(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_PATHMUSTEXIST,"All files (*.*)|*.*||");
        if (fd.DoModal()==IDOK) {
            // Copy recieved data to file
            CStdioFile file;
            file.Open(fd.GetPathName(),CFile::typeBinary|CFile::modeCreate|CFile::modeWrite);
            file.Write(&(rd->pBuf[1]),(UINT)rd->size-1);
            file.Close();
        }
    }

    // Prepend text to log
    CString text;
    dlg->GetDlgItem(IDC_LOG)->GetWindowText(text);
    dlg->GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}

void CTCPNetReceiver::OnError(void* data)
{
    // Typecast variables
    CTCPErrorInfo* ei=(CTCPErrorInfo*)data;
    CCTPDemoDlg* dlg=(CCTPDemoDlg*)m_pMainWnd;

    // Generate error information text
    CString s;
    char ips[16];
    ei->addr.GetString(ips);
    s.Format("%s TCP> Error from %s: type %d, WinSocket error code %d\n",ei->timestamp,ips,ei->type,ei->code);

    // Prepend text to log
    CString text;
    dlg->GetDlgItem(IDC_LOG)->GetWindowText(text);
    dlg->GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}

void CUDPNetReceiver::OnReceive(void* data)
{
    // Typecast variables
    CUDPReceivedData* rd=(CUDPReceivedData*)data;
    CCTPDemoDlg* dlg=(CCTPDemoDlg*)m_pMainWnd;
    // Other necessary variables
    CString s="";
    char ips[16];
    char timestamp[22];

    if (rd->pBuf[0]==0) {
        // Handle arrived text (first byte is zero)

        rd->from.GetString(ips);
        CCTPErrorInfo::GetTimeStamp(timestamp);
        s.Format("%s UDP> Recieve text from %s: size %d\n",timestamp,ips,rd->size);
        dlg->GetDlgItem(IDC_TEXTRECV)->SetWindowText(&(rd->pBuf[1]));
    } else {
        // Handle arrived file (first byte is not zero)

        rd->from.GetString(ips);
        CCTPErrorInfo::GetTimeStamp(timestamp);
        s.Format("%s UDP> Recieve file from %s: size %d\n",timestamp,ips,rd->size);
        
        // Ask for filename
        CFileDialog fd(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_PATHMUSTEXIST,"All files (*.*)|*.*||");
        if (fd.DoModal()==IDOK) {
            // Copy recieved data to file
            CStdioFile file;
            file.Open(fd.GetPathName(),CFile::typeBinary|CFile::modeCreate|CFile::modeWrite);
            file.Write(&(rd->pBuf[1]),(UINT)rd->size-1);
            file.Close();
        }
    }

    // Prepend text to log
    CString text;
    dlg->GetDlgItem(IDC_LOG)->GetWindowText(text);
    dlg->GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}

void CUDPNetReceiver::OnError(void* data)
{
    // Typecast variables
    CUDPErrorInfo* ei=(CUDPErrorInfo*)data;
    CCTPDemoDlg* dlg=(CCTPDemoDlg*)m_pMainWnd;

    // Generate error information text
    CString s;
    char ips[16];
    ei->addr.GetString(ips);
    s.Format("%s UDP> Error from %s: type %d, WinSocket error code %d\n",ei->timestamp,ips,ei->type,ei->code);

    // Prepend text to log
    CString text;
    dlg->GetDlgItem(IDC_LOG)->GetWindowText(text);
    dlg->GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -