TCP送信ファイル

12367 ワード



//      //       ,         

UINT threadSendFile(LPVOID pvar)

{

    CDlgSendMessage *pDlg = (CDlgSendMessage *) pvar;



CFile m_fSendfile;

m_fSendfile.Close();



if (!m_fSendfile.Open(pDlg->m_sendfilepath, CFile::modeRead | CFile::typeBinary))

{

   AfxMessageBox("      !");

   return false;

}





SOCKET sSendMsg;//      

SOCKADDR_IN inetAddr;

sSendMsg = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);



if (INVALID_SOCKET == sSendMsg)

{

   AfxMessageBox( "            !");

   m_fSendfile.Close();

   return 0;

}



inetAddr.sin_family = AF_INET;

inetAddr.sin_port = htons(PUB_MSG_PORT);

inetAddr.sin_addr.s_addr= inet_addr(pDlg->m_receiveip);//      IP







  

if (SOCKET_ERROR == connect(sSendMsg, (SOCKADDR *)&inetAddr, sizeof(SOCKADDR_IN)))//      

{



         

     AfxMessageBox("     ,          !");

   closesocket(sSendMsg);

   m_fSendfile.Close();

   return 0;

}



char buff[MAX_BUF_SIZE] = "";

CString strMsgSend("1001/");

CString strSize;



//      

if (pDlg->m_fiSendFile.nFileSizeLow / (1024 * 1024) != 0)

{

   strSize.Format("%.2fMB", pDlg->m_fiSendFile.nFileSizeLow / (1024.0 * 1024));

}

else

{

   strSize.Format("%.2fKB", pDlg->m_fiSendFile.nFileSizeLow / (1024.0));

}



memcpy(buff, pDlg->m_fiSendFile.szFileTitle, sizeof(pDlg->m_fiSendFile.szFileTitle));

strMsgSend += buff;

strMsgSend += "/";

strMsgSend += strSize;

strMsgSend += "/";

//      ,    //              





if (SOCKET_ERROR == send(sSendMsg, strMsgSend.GetBuffer(0), strMsgSend.GetLength(), 0))

{

   AfxMessageBox( "      !: threadSendFile");

   closesocket(sSendMsg);

   m_fSendfile.Close();

   return 0;

}



memset(buff, 0, MAX_BUF_SIZE);





if (SOCKET_ERROR == recv(sSendMsg, buff, MAX_BUF_SIZE, 0))//        //        

{

   AfxMessageBox( "      !: threadSendFile");

   closesocket(sSendMsg);

   m_fSendfile.Close(); 

   return 0;

}





//         

CString strCmd;

strCmd += buff;

strCmd = strCmd.Left(4);

int iCmd = -1;

iCmd = atoi(strCmd);

if (MSG_ACCEPT != iCmd)

{

   AfxMessageBox( "        !: threadSendFile");

   closesocket(sSendMsg);

   m_fSendfile.Close();



   return false;//

}



//        ,    





//          socket,   FILE_PORT

SOCKET sSendFile;//     

SOCKET sAccept;//       

SOCKADDR_IN inetAddrSendFile;

SOCKADDR_IN inetAddrAccept;



sSendFile = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);

if (INVALID_SOCKET == sSendFile)

{

   AfxMessageBox( "       !: threadSendFile");

   closesocket(sSendMsg);

     m_fSendfile.Close();

   return 0;

}



inetAddrSendFile.sin_addr.s_addr = htonl(INADDR_ANY);

inetAddrSendFile.sin_port    = htons(PUB_FILE_PORT);

inetAddrSendFile.sin_family    = AF_INET;



if (SOCKET_ERROR == bind(sSendFile, (SOCKADDR *)&inetAddrSendFile, sizeof(SOCKADDR)))

{

   AfxMessageBox( "       !: threadSendFile");

   closesocket(sSendMsg);

   closesocket(sSendFile);

   m_fSendfile.Close();

   return 0;

}



if (SOCKET_ERROR == listen(sSendFile, 5))

{

   AfxMessageBox( "    !: threadSendFile");

   closesocket(sSendMsg);

   closesocket(sSendFile);

   m_fSendfile.Close();

   return 0;

}



int iLen = sizeof(SOCKADDR);

sAccept = accept(sSendFile, (SOCKADDR *)&inetAddrAccept, &iLen);



if (INVALID_SOCKET == sAccept)

{

   AfxMessageBox( "accept socket error occurred!: threadSendFile");

   closesocket(sSendMsg);

   closesocket(sSendFile);

   m_fSendfile.Close();

   return 0;

}



//         

char buffInfo[MAX_BUF_SIZE] = "";

memcpy(buffInfo, &pDlg->m_fiSendFile, sizeof(pDlg->m_fiSendFile));

send(sAccept, buffInfo, sizeof(pDlg->m_fiSendFile), 0);

memset(buffInfo, 0, MAX_BUF_SIZE);



//      huozebujieshou,    

   

    recv(sAccept, buffInfo, MAX_BUF_SIZE, 0);//          



//      

DWORD dwRead = 0;

DWORD dwCurrentRead = 0;

BYTE *bReadBuff = new BYTE[MAX_BUF_SIZE];



//      





while (dwRead < pDlg->m_fiSendFile.nFileSizeLow)

{ 

   dwCurrentRead = 0;

   memset(bReadBuff, 0, MAX_BUF_SIZE);

   dwCurrentRead = m_fSendfile.Read(bReadBuff, MAX_BUF_SIZE);//       

   if (SOCKET_ERROR == send(sAccept, (char *)bReadBuff, dwCurrentRead, 0))//    

   {

    AfxMessageBox( "      !: threadSendFile");//       ,          

    closesocket(sSendMsg);

    closesocket(sSendFile);

    m_fSendfile.Close();

    break;

   }



   dwRead += dwCurrentRead;//      

    CString   str;   

        str.Format("%d",   dwRead);

   AfxMessageBox("    "+str);



}

AfxMessageBox("    ");

delete bReadBuff; //     

//     

m_fSendfile.Close();//    

if (INVALID_SOCKET != sAccept)

{

   closesocket(sAccept);//       

}

if (INVALID_SOCKET != sSendFile)

{

   closesocket(sSendFile);//       

}





AfxEndThread(0);



return 1;

}



//         

UINT threadRecvFile(LPVOID pvar)

{



    char * m_sip=(char *)pvar;

SOCKET sFileRecv;

SOCKADDR_IN inetAddr;



//      socket,                

sFileRecv = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);

if (INVALID_SOCKET == sFileRecv)

{

   AfxMessageBox( "        : threadRecvFile");

   AfxEndThread(0);

   return 0;

}



inetAddr.sin_family    = AF_INET;

inetAddr.sin_port    = htons(PUB_FILE_PORT);

inetAddr.sin_addr.s_addr = inet_addr(m_sip);





if (SOCKET_ERROR == connect(sFileRecv, (SOCKADDR *)&inetAddr, sizeof(SOCKADDR)))//     IP

{

   AfxMessageBox("         : threadRecvFile");

   closesocket(sFileRecv);

   AfxEndThread(0);

   return 0;

}



//      

FileInfo fiRecvFile;



if (SOCKET_ERROR == recv(sFileRecv, (char *)&fiRecvFile, sizeof(FileInfo), 0))

{

   AfxMessageBox("         : threadRecvFile");

   closesocket(sFileRecv);

   AfxEndThread(0);

   return 0;

}



CString strFileInfo;

double nfileSize = 0.0;

if (fiRecvFile.nFileSizeLow / (1024 * 1024) != 0)

{

   nfileSize = fiRecvFile.nFileSizeLow / (1024 * 1024);

   strFileInfo.Format("      ...
[%s],
[%s] :[%.2f]MB", inet_ntoa(inetAddr.sin_addr), fiRecvFile.szFileTitle, nfileSize); } else { nfileSize = fiRecvFile.nFileSizeLow / (1024); strFileInfo.Format(" ...
[%s],
[%s] :[%.2f]KB", inet_ntoa(inetAddr.sin_addr), fiRecvFile.szFileTitle, nfileSize); } CFileDialog fdlgSave( FALSE,NULL,fiRecvFile.szFileTitle, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T(" (*.*)|*.*|")); char buff[MAX_BUF_SIZE] = ""; CAcModuleResourceOverride thisResource; if (fdlgSave.DoModal() != IDOK) { sprintf(buff, "%d", MSG_REJECT); send(sFileRecv, buff, sizeof(buff), 0); closesocket(sFileRecv);// AfxEndThread(0); } CString strFilePath; strFilePath = fdlgSave.GetPathName(); CFile fRecvedFile(strFilePath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary); DWORD dwTotalRead; DWORD dwCurrentRead; dwTotalRead = 0; // dwCurrentRead = 0; // BYTE *dataBuff = new BYTE[MAX_BUF_SIZE]; // // memset(buff, 0, sizeof(buff)); sprintf(buff, "%d", MSG_BEGIN); send(sFileRecv, buff, sizeof(buff), 0); while (dwTotalRead < fiRecvFile.nFileSizeLow) { dwCurrentRead = 0; memset(dataBuff, 0, MAX_BUF_SIZE); dwCurrentRead = recv(sFileRecv, (char *)dataBuff, MAX_BUF_SIZE, 0); if (0 == dwCurrentRead || SOCKET_ERROR == dwCurrentRead) { CString strFileRecvInfo; strFileRecvInfo.Format(" :%s !", fiRecvFile.szFileTitle); break; } fRecvedFile.Write(dataBuff, dwCurrentRead); dwTotalRead += dwCurrentRead; // CString str; // str.Format("%d",dwTotalRead); // AfxMessageBox(" "+str); double iCompleted = 0.0; iCompleted = (dwTotalRead * 1.0) / fiRecvFile.nFileSizeLow; iCompleted *= 10.0; CString strSavedInfo; strSavedInfo.Format("%d", iCompleted); } delete dataBuff; // CString strFileRecvInfo; strFileRecvInfo.Format(" :%s !", fiRecvFile.szFileTitle); AfxMessageBox(strFileRecvInfo); if (sFileRecv != INVALID_SOCKET) { closesocket(sFileRecv); } // fRecvedFile.Close(); // // memset(&pDlg->m_fiSendFile, 0, sizeof(pDlg->m_fiSendFile)); // AfxEndThread(0); return 1; } // // UINT threadServer(LPVOID pvar) { SOCKADDR_IN inetAddr; SOCKADDR_IN inetAccept; SOCKET sAccept; SOCKET MySock; // MYSOCK; MySock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (INVALID_SOCKET == MySock) { AfxMessageBox( " !"); return FALSE; } inetAddr.sin_family = AF_INET; inetAddr.sin_port = htons(PUB_MSG_PORT); // //inetAddr.sin_port = 0; inetAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); // if (INVALID_SOCKET == bind(MySock, (SOCKADDR *)&inetAddr, sizeof(SOCKADDR))) { AfxMessageBox( " : threadServer!"); closesocket(MySock); return 0; } // if (0 != listen(MySock, 5)) { AfxMessageBox(" : threadServer!"); return 0; } // AfxMessageBox(" "); //accept int ilen = sizeof(SOCKADDR_IN); while(TRUE) {// sAccept = accept(MySock, (SOCKADDR *)&inetAccept, &ilen); if (INVALID_SOCKET == sAccept) { AfxMessageBox(" : threadServer!"); return 0; } // // , socket SocketInfo *psInfo = new SocketInfo; memset(psInfo, 0, sizeof(SocketInfo)); psInfo->sNow = sAccept; psInfo->inetAddr = inetAccept;// AfxBeginThread(threadRecvMsg, (LPVOID)psInfo); }//while return 1; } // , , UINT threadRecvMsg(LPVOID pvar) { SocketInfo *psockInfo = (SocketInfo *)pvar; SocketInfo sockInfo = *psockInfo; delete psockInfo; // // , , int iRecv = -1; char buff[MAX_BUF_SIZE] = ""; char szMsg[256] = ""; // iRecv = recv(sockInfo.sNow, buff, sizeof(buff), 0);// if (SOCKET_ERROR == iRecv) { closesocket(sockInfo.sNow); AfxMessageBox( " !: threadRecvMsg"); AfxEndThread(0); } //1001/ 2007.exe/32MB/ strcpy(szMsg, buff); int itype = 0; CString strTemp; CString strInMsg; strTemp += szMsg; strInMsg += szMsg; strTemp = strTemp.Left(4); itype = atoi(strTemp); // if (MSG_NEW_FILE == itype) { CString strMsgInfo; CString strHost; CString strFileName; CString strSize; int i, j; i = 0; j = 0; i = strInMsg.Find("/"); j = strInMsg.Find("/", i + 1); // strFileName = strInMsg.Mid(i + 1, j - i - 1); strInMsg.TrimRight("/"); // strSize = strInMsg.Right(strInMsg.GetLength() - strInMsg.ReverseFind('/') - 1); strMsgInfo.Format("[ :%s]
[ :%s]
[ :%s]", inet_ntoa(sockInfo.inetAddr.sin_addr), strFileName, strSize); strMsgInfo += "
?"; CAcModuleResourceOverride thisResource; if (IDYES == MessageBox(NULL, strMsgInfo, " ", MB_YESNO)) { char buffSend[MAX_BUF_SIZE] = ""; char sztemp[20] = ""; itoa(MSG_ACCEPT, sztemp, 10); strcpy(buffSend, sztemp); // if (SOCKET_ERROR == send(sockInfo.sNow, buffSend, sizeof(buffSend), 0)) { AfxMessageBox( " : threadRecvMsg"); closesocket(sockInfo.sNow); AfxEndThread(0); }//if char * senderip; senderip=inet_ntoa(sockInfo.inetAddr.sin_addr); // AfxBeginThread(threadRecvFile, (LPVOID)senderip); }//if else { char buffSend[MAX_BUF_SIZE] = ""; char sztemp[20] = ""; itoa(MSG_REJECT, sztemp, 10); strcpy(buffSend, sztemp); // if (SOCKET_ERROR == send(sockInfo.sNow, buffSend, sizeof(buffSend), 0)) { AfxMessageBox( " : threadRecvMsg"); closesocket(sockInfo.sNow); AfxEndThread(0); }//if }//else }//if else { AfxMessageBox(" "); }//else return 0; }