资源简介

摘要:VC/C++源码,网络相关,点对点,文件传输   简易的VC++点对点文件传输,暂时没有运行截图,文件传输的功能并不强大,但是实现过程既简单又经典,说明了文件传输的原理。

资源截图

代码片段和文件信息

// SimpleFileTrans.cpp : Defines the entry point for the console application.
// Download by http://www.codefans.net
#include “stdafx.h“
#include “SimpleFileTrans.h“

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include 
#include 
#include 
#include 
#include 
#include 

#define SERVER_LISTEN_PORT 4000
#define SERVER_IP “10.214.50.124“

void SendFile(char *filename);
void GetFile(char *filename=NULL);
BOOL SendMessage(SOCKET sock const char* buffer int bufferLen);
BOOL ReceiveMessage(SOCKET sock char* buffer int bufferLen);

//argv[0]  Module name
//argv[1]  Switch
//    s -send file
//         r -receive file
//argv[2]  File name
//         If user sets argv[1] as ‘s‘ file name is necessary.
//         If user sets argv[1] as ‘r‘ file name is optional.
//         Defaultly we set filename as its raw name from server.

int _tmain(int argc TCHAR* argv[] TCHAR* envp[])
{
if(argc==1)
return 1;

if(argv[1][0]==‘s‘)
{
if(argc!=3)
return 1;
SendFile(argv[2]);
}
else if(argv[1][0]==‘r‘)
{
if(argc<2||argc>3)
return 1;
if(argc==2)
GetFile();
else
GetFile(argv[2]);
}
else
{
cerr<<“s/r send or receive!“< return 1;
}
return 0;
}

void SendFile(char *filename)
{
WSADATA wsaData;
SOCKET sock;
sockaddr_in localAddrclientAddr;
SOCKET clientSock;
int sizeAddr=sizeof(sockaddr);
char *data;
ifstream file;
filebuf *fB;
int length;
BOOL bRet;
char sDrive[_MAX_DRIVE]; char sDir[_MAX_DIR];
char sFname[_MAX_FNAME]; char sExt[_MAX_EXT];

assert(filename);

_splitpath(filename sDrive sDir sFname sExt);
strcat(sFnamesExt);

if(WSAStartup(MAKEWORD( 2 2 ) &wsaData)!=0)
{
cerr<<“Socket init error“< return;
}

sock=socket(AF_INET SOCK_STREAM0);

localAddr.sin_addr.s_addr=htonl(INADDR_ANY);
localAddr.sin_family=AF_INET;
localAddr.sin_port=htons(SERVER_LISTEN_PORT);

if(bind(sock(const struct sockaddr*)& localAddrsizeAddr)==SOCKET_ERROR)
{
cerr<<“Bind error!“< goto EXIT;
}

cout<<“Listening at port “<
listen(sock1);// 峨家Α钡Μ硈钡
if((clientSock=accept(sock(struct sockaddr*)&clientAddr&sizeAddr))==INVALID_SOCKET)
{
cerr<<“Accept error“< goto EXIT;
}
cout<<“A connection has been focused!“<
//send file name‘s length and buffer content
length=strlen(sFname);
SendMessage(clientSock(const char*)&lengthsizeof(int));
SendMessage(clientSocksFnamelength);

//ゴ秨璶祇癳郎
file.open(filenameios::binary);
if(file.fail())
{
cerr<<“Fail opening file “< goto EXIT;
}

cout<<“Sending file now!“< fB=file.rdbuf();
length=fB->seekoff(0ios::endios::in)-fB->seekoff(0ios::begios::in);
fB->seekoff(0ios::begios::in);

// 祇癳郎へ倒钡Μよ
SendMessage(cl

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       1668  2006-08-09 22:26  Lelecode.com\SimpleFileTrans\ReadMe.txt

     文件        456  2006-08-09 22:37  Lelecode.com\SimpleFileTrans\Resource.h

     文件      16920  2012-03-20 09:41  Lelecode.com\SimpleFileTrans\SimpleFileTrans.aps

     文件        252  2005-11-08 19:09  Lelecode.com\SimpleFileTrans\SimpleFileTrans.clw

     文件       5464  2012-03-20 09:42  Lelecode.com\SimpleFileTrans\SimpleFileTrans.cpp

     文件       4406  2005-11-08 19:09  Lelecode.com\SimpleFileTrans\SimpleFileTrans.dsp

     文件        555  2005-11-08 19:09  Lelecode.com\SimpleFileTrans\SimpleFileTrans.dsw

     文件        353  2006-08-09 22:37  Lelecode.com\SimpleFileTrans\SimpleFileTrans.h

     文件       1458  2006-08-08 02:33  Lelecode.com\SimpleFileTrans\SimpleFileTrans.rc

     文件        302  2006-08-09 06:08  Lelecode.com\SimpleFileTrans\StdAfx.cpp

     文件       1083  2006-08-09 22:37  Lelecode.com\SimpleFileTrans\StdAfx.h

     目录          0  2012-03-20 09:43  Lelecode.com\SimpleFileTrans

     目录          0  2012-03-20 09:39  Lelecode.com

----------- ---------  ---------- -----  ----

                32917                    13


评论

共有 条评论