200分,求点对点文件传送源代码!货到发分!(100分)

  • 主题发起人 主题发起人 jierxrh
  • 开始时间 开始时间
J

jierxrh

Unregistered / Unconfirmed
GUEST, unregistred user!
用MODEM连接两PC机,可互相传送文件(RAR、TXT、ZIP都行),
既可以从CLIENT上传至SERVER,又可以从SERVER下传至CLIENT的源代码!
那位DFW可以贡献一下。
不要告诉我从DFW上找,我知道有很多,但我更愿意送分 [:D][:D][:D]

发到油箱:jierxrh@sohu.com,马上给分
另为100分在:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1997755
 
我有CB的你要吗?
 
我给你贴出来:
传输大型文件实列 (SOCKET API,98/NT/2000调试通过)

作C/S均建立读/写线程,一但连接,C/S便都可发送/接收文件,发送文件方式使 用字节流传送。将传送文件进行分割,每次传送1K,CLIENT或SERVER方收到后 进行重组。接收文件方式使用异步SOCKET,根据实际读入数据写文件。在LAN 上测试,C/S多次相互传输大型文件如:IIS,SQL SP,和几个上百M的文件, 效果良好。


Server.cpp

//---------------------------------------------------------------------------
// SERVER端
//
// 作者:贾佳,jiasys@21cn.com
//---------------------------------------------------------------------------

#include <vcl.h>
#include <winsock.h>
#define BUFFSIZE 1024
#pragma hdrstop

#include "Server.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

WSADATA wsaData;
SOCKET sck,sc;
SOCKADDR_IN to,client;
BOOL flag=TRUE;
int iAddrSize,ret,ret_no,i=0;
TCHAR szBuf[BUFFSIZE];
fd_set FdRead;

//---------------------------------------------------------------------------
// ReadClient
//
// 接收文件数据线程,判断建立文件关键字,每次将实际读到的数据写入文件。
//
//---------------------------------------------------------------------------

DWORD WINAPI ReadClient(LPVOID lPort)
{
HANDLE hFile=NULL;
DWORD dwWrite,dwFileSize;
TCHAR szFileName[MAX_PATH];

FD_ZERO(&FdRead);
FD_SET(sck,&FdRead);

while(TRUE)
{
ret_no=select(0,&FdRead,NULL,NULL,NULL);
if(ret_no==SOCKET_ERROR)
{
closesocket(sck);
return FALSE;
}

if(FD_ISSET(sck,&FdRead))
{
iAddrSize=sizeof(client);
sc=accept(sck,(SOCKADDR *)&client,&iAddrSize);
if(sc==INVALID_SOCKET)
{
MessageBox(NULL,"accept error",NULL,MB_OK);
closesocket(sc);
WSACleanup();
}
getpeername(sck,(SOCKADDR *)&client,&iAddrSize);
ShowMessage(inet_ntoa(client.sin_addr));
FD_SET(sc,&FdRead);
}

if(FD_ISSET(sc,&FdRead))
{
ZeroMemory(szBuf,sizeof(szBuf));
if((dwFileSize=recv(sc,szBuf,BUFFSIZE,0))==SOCKET_ERROR)
{
closesocket(sc);
CloseHandle(hFile);
return FALSE;
}
else if((strncmp(szBuf,"UPFILE_",lstrlen("UPFILE_")))==0)
{
wsprintf(szFileName,"c://%s",szBuf);
hFile=CreateFile(szFileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,
NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL|
FILE_ATTRIBUTE_ARCHIVE,(HANDLE)NULL);

if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Server Open File Error",NULL,MB_OK);
return FALSE;
}
ZeroMemory(szBuf,sizeof(szBuf));
}
else
WriteFile(hFile,szBuf,dwFileSize,&dwWrite,NULL);
}
}
CloseHandle(hFile);
return TRUE;
}

//---------------------------------------------------------------------------
// WriteClient
//
// 发送文件数据线程,每次预读1K数据,根据实际读取发送,直到读取数据小于1K
//
//---------------------------------------------------------------------------

DWORD WINAPI WriteClient(LPVOID szFileName)
{

HANDLE hFile;
DWORD dwRead,dwNdx;
BOOL bRet;
TCHAR szFileBuff[BUFFSIZE],szSend[MAX_PATH];

wsprintf(szSend,"DOWNFILE_%s",ExtractFileName((LPCTSTR)szFileName).c_str());
send(sc,szSend,lstrlen(szSend),0);
hFile=CreateFile((LPCTSTR)szFileName,GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL|
FILE_ATTRIBUTE_ARCHIVE,(HANDLE)NULL);

if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Open File Error",NULL,MB_OK);
ExitProcess(0);
}

do
{
bRet=ReadFile(hFile,szFileBuff,BUFFSIZE,&dwRead,NULL);
if(bRet==FALSE)
{
MessageBox(NULL,"Read Buf ERROR!",NULL,MB_OK);
break;
}
else if(dwRead==0)
{
MessageBox(NULL,"File EOF!",NULL,MB_OK);
break;
}
else
{
send(sc,szFileBuff,dwRead,0);
}
}while(dwRead==BUFFSIZE);

CloseHandle(hFile);
return TRUE;
}

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{

HANDLE hThread;
DWORD dwTid;

if(WSAStartup(MAKEWORD(1,1),&wsaData)!=NULL)
{
ShowMessage("初始化WINSOCK错误");
WSACleanup();
}

if((sck=socket(AF_INET,SOCK_STREAM,0))==SOCKET_ERROR)
{
ShowMessage("SOCKET错误");
closesocket(sck);
WSACleanup();
}

to.sin_family=AF_INET;
to.sin_port=htons(926);
to.sin_addr.s_addr=htonl(INADDR_ANY);

if(setsockopt(sck,SOL_SOCKET,SO_REUSEADDR,(LPSTR)&flag,sizeof(flag))==SOCKET_E
RROR)
{
ShowMessage("setsockopt error!");
closesocket(sck);
}


if(bind(sck,(struct sockaddr *)&to,sizeof(to))==SOCKET_ERROR)
{
ShowMessage("Could not bind");
closesocket(sck);
}

else
{
listen(sck,1);
hThread=CreateThread(NULL,0,ReadClient,(LPVOID)0,0,&dwTid);
CloseHandle(hThread);
}

}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)
{
HANDLE hThread;
DWORD dwTid;

if(OpenDialog1->Execute())
{
hThread=CreateThread(NULL,0,WriteClient,(LPVOID)OpenDialog1->FileName.c_str()
,0,&dwTid);
CloseHandle(hThread);
}


}
//---------------------------------------------------------------------------

Client.cpp

//---------------------------------------------------------------------------
// CLIENT端
//
// 作者:贾佳,jiasys@21cn.com
//---------------------------------------------------------------------------

#include <vcl.h>
#include <winsock.h>
#define BUFFSIZE 1024
#pragma hdrstop

#include "Client.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

WSADATA wsa;
SOCKET sck;
SOCKADDR_IN tto;
hostent *host;
fd_set FdRead;
int port=926,ret,i;
DWORD dwRead;


//---------------------------------------------------------------------------
// ReadClient
//
// 接收文件数据线程,判断建立文件关键字,每次将实际读到的数据写入文件。
//
//---------------------------------------------------------------------------

DWORD WINAPI ReadClient(LPVOID lParam)
{
HANDLE hFile;
DWORD dwWrite,dwFileSize;
TCHAR szFileName[MAX_PATH];
TCHAR szBuff[BUFFSIZE];

FD_ZERO(&FdRead);
FD_SET(sck,&FdRead);

while(TRUE)
{
ret=select(0,&FdRead,NULL,NULL,NULL);
if(ret==SOCKET_ERROR)
{
closesocket(sck);
return FALSE;
}
else if(FD_ISSET(sck,&FdRead))
{
ZeroMemory(szBuff,sizeof(szBuff));
if((dwFileSize=recv(sck,szBuff,BUFFSIZE,0))==SOCKET_ERROR)
{
closesocket(sck);
CloseHandle(hFile);
return FALSE;
}
else if((strncmp(szBuff,"DOWNFILE_",lstrlen("DOWNFILE_")))==0)
{
wsprintf(szFileName,"c://%s",szBuff);
hFile=CreateFile(szFileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,
NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL|
FILE_ATTRIBUTE_ARCHIVE,(HANDLE)NULL);

if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Cli Open File Error",NULL,MB_OK);
return FALSE;
}
ZeroMemory(szBuff,sizeof(szBuff));
}
else
WriteFile(hFile,szBuff,dwFileSize,&dwWrite,NULL);
}
}
return TRUE;
}

//---------------------------------------------------------------------------
// WriteClient
//
// 发送文件数据线程,每次预读1K数据,根据实际读取发送,直到读取数据小于1K
//
//---------------------------------------------------------------------------


DWORD WINAPI WriteClient(LPVOID szFileName)
{
HANDLE hFile;
DWORD dwRead,dwNdx;
BOOL bRet;
TCHAR szFileBuff[BUFFSIZE],szSend[MAX_PATH];

wsprintf(szSend,"UPFILE_%s",ExtractFileName((LPCTSTR)szFileName).c_str());
send(sck,szSend,lstrlen(szSend),0);
hFile=CreateFile((LPCTSTR)szFileName,GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL|
FILE_ATTRIBUTE_ARCHIVE,(HANDLE)NULL);

if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Open File Error",NULL,MB_OK);
ExitProcess(0);
}

do
{
bRet=ReadFile(hFile,szFileBuff,BUFFSIZE,&dwRead,NULL);
if(bRet==FALSE)
{
MessageBox(NULL,"Read Buf ERROR!",NULL,MB_OK);
break;
}
else if(dwRead==0)
{
MessageBox(NULL,"File EOF!",NULL,MB_OK);
break;
}
else
{
send(sck,szFileBuff,dwRead,0);
}
}while(dwRead==BUFFSIZE);

CloseHandle(hFile);
return TRUE;
}

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HANDLE hThread;
DWORD dwTid;

WSAStartup(MAKEWORD(1,1),&wsa);
sck=socket(AF_INET,SOCK_STREAM,0);

if(sck==INVALID_SOCKET)
{
ShowMessage("Could not create a sock");
ExitProcess(0);
}

else
{
host=gethostbyname(Edit1->Text.c_str());
tto.sin_family=AF_INET;
tto.sin_port=htons(port);
CopyMemory(&tto.sin_addr,host->h_addr,host->h_length);
if((connect(sck,(struct sockaddr FAR *)&tto,sizeof(tto))==SOCKET_ERROR) )
{
hThread=CreateThread(NULL,0,WriteClient,(LPVOID)OpenDialog1- >FileName.c_str()
,0,&dwTid);
CloseHandle(hThread);
}


--------------------------------------------------------------------------------
 
:fxh7622大哥,虽然你没提供DELPHI代码,但我还是要谢谢你,并给你加分。
CB代码我看不太懂。:)
 
很简单的,不过在DELPHI下使用SOCKET编程的时候比较麻烦。你可以使用DELPHI中自带的控件在Internet也中的ClientSocket和ServerSocket来实现,不过读去文件到缓存的时候必须找我给你的例子那样就可以了的。
 
我写过一个delphi的,给你贴出来:
客户端:
procedure TForm1.CilentSend(Sender: TObject); //客户端发送文件
var AFile:String; AFS:TFileStream;
begin
if OpenDialog1.Execute then begin
AFile := openDialog1.FileName;
pFile:=AFile;
AFS := TFileStream.Create(AFile,fmShareDenyNone);
CS1.Socket.SendText('Send:'+GetFilename(AFile)+';'+inttostr(AFS.Size));
pb1.Max:= AFS.Size;
pb1.Position := 0;
AFS.Free;
end;
end;

procedure TForm1.cs1Read(Sender: TObject; Socket: TCustomWinSocket);//客户端接收
var fS,AStream,AFS:TFileStream; AFileName,s,s1:String; i,nRetr,bf:Integer; Buffer:Pointer;
begin
case TCon(socket.Data).FState of
0: begin
S:= Socket.ReceiveText;
if S='Ready' then begin
AFS := TFileStream.Create(pFile,fmOpenRead);
CS1.Socket.SendStream(AFS);
pb1.Position:=AFS.Position;
TCon(Socket.Data).FState := 0;
end
else if pos('Send',s)>0 then begin
S1 := Copy(S,pos(':',s)+1,pos(';',s)-pos(':',s)-1);
TCon(Socket.Data).FName := Trim(s1);
TCon(Socket.Data).FState := 1;
pb1.Max := StrtoInt(Copy(S,pos(';',s)+1,length(s)));
Memo1.Lines.Add(Socket.RemoteAddress+' Ask Send '+s1);
pb1.Position:=0;
cs1.Socket.SendText('ok');
end;
end;
1: begin
bf := Socket.ReceiveLength;
AFileName := ExtractFilePath(Application.ExeName)+'/'+TCon(Socket.Data).FName;
GetMem(Buffer,bf);
nRetr := Socket.ReceiveBuf(Buffer^,bf);
if not FIleExists(AFileName) then begin
fs :=TFileStream.Create(AFileName,fmCreate or fmShareDenyNone);
fs.Seek(0,soFromBeginning);
end
else begin
fs :=TFileStream.Create(AFileName,fmOpenWrite or fmShareDenyNone);
fs.Seek(0,soFromEnd);
end;
fs.WriteBuffer(Buffer^,bf);
pb1.Position := fs.Position;
fs.Destroy;
FreeMem(Buffer);
if pb1.Position=pb1.Max then begin
memo1.Lines.Add('over');
TCon(Socket.Data).FName := '';
TCon(Socket.Data).FState := 0;
end;
end;
end;
// memo1.Lines.Add(socket.ReceiveText);
end;

//以下是服务端接收,发送文件
procedure TForm1.ss1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var i,nRetr,bf:Integer; Buffer:Pointer; AFS,fS,AStream:TFileStream; AFileName,s,s1 :String;
begin
case TCon(socket.Data).FState of
0: begin
S := Socket.ReceiveText;
memo1.Lines.Add(s);
if pos('Send',s)>0 then begin //如果客户端传‘send’,表明客户端要发文件
S1 := Copy(S,pos(':',s)+1,pos(';',s)-pos(':',s)-1);
TCon(Socket.Data).FName := Trim(s1);
TCon(Socket.Data).FState := 1;
pb1.Max := StrtoInt(Copy(S,pos(';',s)+1,length(s)));
Memo1.Lines.Add(Socket.RemoteAddress+' Ask Send!');
socket.SendText('Ready'); //通知客户端自己准备好接收
pb1.Position:=0;
end
else
begin
if s='download' then begin //如果客户端发‘download’,表明要向客户端传文件
AFS := TFileStream.Create(pFile,fmShareDenyNone);
pb1.Max:= AFS.Size;
pb1.Position := 0;
for i:=0 to ss1.Socket.ActiveConnections-1 do
if socket=SS1.Socket.Connections then
socket.SendText('Send:'+GetFilename(pFile)+';'+inttostr(AFS.Size));
AFS.Free;
end
else if s='ok' then begin //客户端准备好接收,自己发送
FS := TFileStream.Create(pFile,fmShareDenyNone);
for i:=0 to ss1.Socket.ActiveConnections-1 do begin
if socket=ss1.Socket.Connections then begin
SS1.Socket.Connections[0].SendStream(FS);
pb1.Position:=FS.Position;
end;
end;
TCon(Socket.Data).FState := 0;
end;
end;
end;
1: begin //服务端接收文件
bf := Socket.ReceiveLength;
AFileName := ExtractFilePath(Application.ExeName)+'/'+TCon(Socket.Data).FName;
GetMem(Buffer,bf);
nRetr := Socket.ReceiveBuf(Buffer^,bf);
if not FIleExists(AFileName) then begin
fs :=TFileStream.Create(AFileName,fmCreate or fmShareDenyNone);
fs.Seek(0,soFromBeginning);
end
else begin
fs :=TFileStream.Create(AFileName,fmOpenWrite or fmShareDenyNone);
fs.Seek(0,soFromEnd);
end;
fs.WriteBuffer(Buffer^,bf);
pb1.Position := fs.Position;
fs.Destroy;
FreeMem(Buffer);
if pb1.Position=pb1.Max then begin
memo1.Lines.Add('over');
TCon(Socket.Data).FName := '';
TCon(Socket.Data).FState := 0;
end;
end;
end;
end;
 
在Delphi6/7中利用UDP协议传送文件的例子
http://www.aidelphi.com/6to23/docu/MyUDP(Delphi6_7).rar
一个利用WinSocket控件传送文件的Demo
http://www.aidelphi.com/6to23/docu/transfile.zip

这两个例你看有没有用吧
 
已发送!查收。

全是SOCKET API实现看看吧有点乱!
 
哈哈哈,这么多,我会调试一下,明天散分。OK!

在此谢谢各位FW了;
 
多人接受答案了。
 
后退
顶部