當前位置:編程學習大全網 - 編程語言 - VC SOCKET 文件傳輸程序問題

VC SOCKET 文件傳輸程序問題

用VC6.0建立兩個WIndows32 Console Application程序,並且選擇壹個支持MFC的程序,第壹個程序中輸入:

// server.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "server.h"

#include<afxsock.h>

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// The one and only application object

CWinApp theApp;

using namespace std;

typedef struct _SOCKET_STREAM_FILE_INFO {

TCHAR szFileTitle[128]; //文件的標題名

DWORD dwFileAttributes; //文件的屬性

FILETIME ftCreationTime; //文件的創建時間

FILETIME ftLastAccessTime; //文件的最後訪問時間

FILETIME ftLastWriteTime; //文件的最後修改時間

DWORD nFileSizeHigh; //文件大小的高位雙字

DWORD nFileSizeLow; //文件大小的低位雙字

DWORD dwReserved0; //保留,為0

DWORD dwReserved1; //保留,為0

} SOCKET_STREAM_FILE_INFO, * PSOCKET_STREAM_FILE_INFO;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])

{

int nRetCode = 0;

// initialize MFC and print and error on failure

if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))

{

// TODO: change error code to suit your needs

cerr << _T("Fatal Error: MFC initialization failed") << endl;

nRetCode = 1;

}

else

{

// TODO: code your application's behavior here.

CString strHello;

strHello.LoadString(IDS_HELLO);

cout << (LPCTSTR)strHello << endl;

}

CSocket sockClient;

sockClient.Create();

if(!sockClient.Connect("127.0.0.1", 800))

{

AfxMessageBox("連接到對方機器失敗!");

return 0;

}

SOCKET_STREAM_FILE_INFO StreamFileInfo;

sockClient.Receive(&StreamFileInfo,sizeof(SOCKET_STREAM_FILE_INFO));

CFile destFile(StreamFileInfo.szFileTitle, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);

UINT dwRead = 0;

while(dwRead)

{

byte* data = new byte[1024];

memset(data,0,1024);

UINT dw=sockClient.Receive(data, 1024);

destFile.Write(data, dw);

dwRead+=dw;

}

SetFileTime((HANDLE)destFile.m_hFile,&StreamFileInfo.ftCreationTime,

&StreamFileInfo.ftLastAccessTime,&StreamFileInfo.ftLastWriteTime);

destFile.Close();

SetFileAttributes(StreamFileInfo.szFileTitle,StreamFileInfo.dwFileAttributes);

sockClient.Close();

AfxMessageBox("接收完畢!");

return nRetCode;

}

另外壹個程序輸入:

// client.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "client.h"

#include <afxsock.h>

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// The one and only application object

CWinApp theApp;

using namespace std;

typedef struct _SOCKET_STREAM_FILE_INFO {

TCHAR szFileTitle[128]; //文件的標題名

DWORD dwFileAttributes; //文件的屬性

FILETIME ftCreationTime; //文件的創建時間

FILETIME ftLastAccessTime; //文件的最後訪問時間

FILETIME ftLastWriteTime; //文件的最後修改時間

DWORD nFileSizeHigh; //文件大小的高位雙字

DWORD nFileSizeLow; //文件大小的低位雙字

DWORD dwReserved0; //保留,為0

DWORD dwReserved1; //保留,為0

} SOCKET_STREAM_FILE_INFO, * PSOCKET_STREAM_FILE_INFO;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])

{

int nRetCode = 0;

// initialize MFC and print and error on failure

if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))

{

// TODO: change error code to suit your needs

cerr << _T("Fatal Error: MFC initialization failed") << endl;

nRetCode = 1;

}

else

{

// TODO: code your application's behavior here.

CString strHello;

strHello.LoadString(IDS_HELLO);

cout << (LPCTSTR)strHello << endl;

}

CFile myFile;

CString filename="123.txt";

if(!myFile.Open(filename, CFile::modeRead | CFile::typeBinary))

{

AfxMessageBox("文件不存在!",MB_OK|MB_ICONERROR);

return 0;

}

CSocket sockSrvr;

sockSrvr.Create(800);

sockSrvr.Listen();

CSocket sockRecv;

sockSrvr.Accept(sockRecv);

SOCKET_STREAM_FILE_INFO StreamFileInfo;

WIN32_FIND_DATA FindFileData;

FindClose(FindFirstFile(filename,&FindFileData));

memset(&StreamFileInfo,0,sizeof(SOCKET_STREAM_FILE_INFO));

strcpy(StreamFileInfo.szFileTitle,myFile.GetFileTitle());

StreamFileInfo.dwFileAttributes = FindFileData.dwFileAttributes;

StreamFileInfo.ftCreationTime = FindFileData.ftCreationTime;

StreamFileInfo.ftLastAccessTime = FindFileData.ftLastAccessTime;

StreamFileInfo.ftLastWriteTime = FindFileData.ftLastWriteTime;

StreamFileInfo.nFileSizeHigh = FindFileData.nFileSizeHigh;

StreamFileInfo.nFileSizeLow = FindFileData.nFileSizeLow;

sockRecv.Send(&StreamFileInfo,sizeof(SOCKET_STREAM_FILE_INFO));

UINT dwRead=0;

while(dwRead)

{

byte* data = new byte[1024];

UINT dw=myFile.Read(data, 1024);

sockRecv.Send(data, dw);

dwRead+=dw;

}

myFile.Close();

sockRecv.Close();

AfxMessageBox("發送完畢!");

return nRetCode;

}

現在應該可以運行了,然後根據妳的需要修改

  • 上一篇:python編程語言的集成編譯環境哪個好用
  • 下一篇:舞臺音響師介紹
  • copyright 2024編程學習大全網