-
На клиентеprocedure TForm1.OnClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
asd: TMemoryStream;
len: Integer;
buf: Pointer;
begin
Len:=Socket.ReceiveLength;
GetMem(Buf, Len+1);
asd := TMemoryStream.Create;
asd.Position:=0;
Client.Socket.ReceiveBuf(Buf^, Len);
asd.WriteBuffer(buf^, Len);
asd.Position:=0;
asd.SaveToFile(PChar('c:\123.bmp'));
asd.Free;
FreeMem(Buf);
end;
На сервереprocedure TForm1.BitBtn1Click(Sender: Tobject);
var
asd: TMemoryStream;
begin
asd := TMemoryStream.Create;
asd.LoadFromFile('c:\1.bmp');
asd.Position:=0;
if (server.Socket.Connections[0].Connected) then
server.Socket.Connections[0].SendStream(asd);
asd.free;
end;
Почему-то когда я пытаюсь передать файл больше 8КБ, то программа выдает ошибку Asynchronous socket error. Подскажите пожалуйста как это устранить? -
foreverDelphi © (14.10.07 20:43) [1]я б сделал бы примерно такprocedure TForm2.ClientSocket24Connect(Sender: TObject;
Socket: TCustomWinSocket);
var
ssFile:PssFile;
begin
new(ssFile);
ssFile^.fileName:='';
ssFile^.sizeFile:=0;
ssFile^.sizeWrite:=0;
Socket.Data:=ssFile;
end;
procedure TForm2.ServerSocket2ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
temp:string;
f1:file;
ssFile:PssFile;
begin
new(ssFile);
ssFile^.fileName:='';
ssFile^.sizeFile:=0;
ssFile^.sizeWrite:=0;
Socket.Data:=ssFile;
if OpenDialog1.Execute then begin
AssignFile(f1,filename);
Reset(f1);
ssFile^.sizeFile:=FileSize(f1);
ssFile^.fileName:=fileName;
socket.SendText(IntToStr(FileSize(f1))+'#'+ExtractFileName(fileName)+'#');
closeFile(f1);
end;
end;
procedure TForm2.ServerSocket2ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
f1:file;
buf:array[0..5000]of Char;
temp,temp2:string;
reads:integer;
begin
if TssFile(Socket.Data^).sizeFile=0 then begin
temp:=socket.ReceiveText;
TssFile(Socket.Data^).sizeFile:=StrToInt(Copy(temp,0,Pos('#',temp)-1));
delete(temp,1,Pos('#',temp));
TssFile(Socket.Data^).fileName:=Copy(temp,0,Pos('#',temp)-1);
AssignFile(f1,TssFile(Socket.Data^).fileName);
Rewrite(f1,1);
CloseFile(f1);
socket.SendText('next');
end else
begin
reads:=Socket.ReceiveBuf(bufArray,5001);
assignFile(f1,TssFile(Socket.Data^).fileName);
Reset(f1,1);
Seek(f1,FileSize(f1));
blockWrite(f1,buf,reads);
if FileSize(f1)=TssFile(Socket.Data^).sizeFile then begin
CloseFile(f1);
socket.Close;
end else begin
if FileSize(f1) mod 5001 = 0 then
socket.SendText('next');
CloseFile(f1);
end;
end;
procedure TForm2.ClientSocket24Read(Sender: TObject;
Socket: TCustomWinSocket);
var
f1:file;
buf:array[0..5000]of Char;
reads:integer;
begin
AssignFile(f1,TssFile(Socket.Data^).FileName);
Reset(f1)
Seek(f1,TssFile(Socket.Data^).sizeWrite);
blockRead(f1,buf,5001,reads);
TssFile(Socket.Data^).sizeWrite:=TssFile(Socket.Data^).sizeWrite+reads;
socket.SendBuf(buf,reads);
CloseFile(f1);
end; -
Hero © (15.10.07 03:26) [2]по мне так лучше через FileStream
-
program Client;
{$APPTYPE CONSOLE}
uses ScktComp,Classes, SysUtils,WinSock;
var
Socket:TClientWinSocket;
Stream:TFileStream;
buf:array[byte] of byte;
begin
Socket:=TClientWinSocket.Create(INVALID_SOCKET);
try
Socket.ClientType:=ctBlocking;
Socket.Open('','127.0.0.1','',80);
Stream:=TFileStream.Create('c:\123.bmp', fmCreate);
try
while Socket.Connected do
begin
Stream.WriteBuffer(buf,Socket.ReceiveBuf(buf,SizeOf(buf)));
end;
finally
Stream.Free;
end;
finally
Socket.Free;
end;
end.program Server;
{$APPTYPE CONSOLE}
uses
SysUtils,classes,ScktComp,WinSock;
type
TServerClientThreadEx=class(TServerClientThread)
protected
procedure ClientExecute; override;
end;
procedure TServerClientThreadEx.ClientExecute;
var
Stream:TFileStream;
buf:array[byte] of byte;
ToSend:integer;
begin
try
Stream := TFileStream.Create('c:\1.bmp', fmOpenRead or fmShareDenyWrite);
try
while (not Terminated) and ClientSocket.Connected do
begin
ToSend:=Stream.Read(buf,SizeOf(buf));
if ToSend=0 then
exit;
ClientSocket.SendBuf(buf,ToSend);
end;
finally
Stream.Free;
end;
except
Terminate;
HandleException;
end;
end;
procedure GetThread(Self:TObject;Sender: TObject;ClientSocket: TServerClientWinSocket; var SocketThread: TServerClientThread);
begin
writeln('GetThreadEvent ',ClientSocket.RemoteAddress,':',ClientSocket.RemotePort);
SocketThread:=TServerClientThreadEx.Create(false,ClientSocket);
end;
function Proc2Method(Code, Data: Pointer):TMethod;
begin
result.Code:=Code;
result.Data:=Data;
end;
var Socket:TServerWinSocket;
begin
Socket:=TServerWinSocket.Create(INVALID_SOCKET);
try
Socket.ThreadCacheSize := 10;
Socket.OnGetThread:=TGetThreadEvent(Proc2Method(@GetThread,Socket));
Socket.Listen('', '', '', StrToIntDef(ParamStr(1),19), SOMAXCONN);
while Socket.Connected do
Sleep(100);
finally
Socket.Free;
end;
end. -
Сергей М. © (15.10.07 08:15) [4]
> JanMihail © (14.10.07 19:51)
Читай внимательно справку к методу SendStream