开放源码啰,将究着看,别管这程序规不规范,反正实现了。我可没有做模块的好习惯呀!
用了winshoe的一套东西,本来准备是自己用,没考虑过要给大家看的~~~~~~
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Winshoes, serverwinshoe, StdCtrls, ServerWinshoeIRC, NMUDP,
ipwatchwinshoe, ComCtrls, Menus, ScktComp;
type
TForm1 = class(TForm)
Memo1: TMemo;
WinshoeListener1: TWinshowIRCListener;
NMUDP1: TNMUDP;
WinshoeIPWatch1: TWinshoeIPWatch;
ListView1: TListView;
PopupMenu1: TPopupMenu;
show1: TMenuItem;
ClientSocket1: TClientSocket;
Button1: TButton;
procedure WinshoeListener1Execute(Thread: TWinshoeServerThread);
procedure WinshoeListener1Disconnect(Thread: TWinshoeServerThread);
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
procedure show1Click(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Write(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
private
rr,s:integer;
thr:TWinshoeServerThread;
qqq:string; { Private declarations }
public
{ Public declarations }
end;
function iptoword(address:string):string;
var
Form1: TForm1;
procedure clopen;
implementation
{$R *.DFM}
procedure TForm1.WinshoeListener1Execute(Thread: TWinshoeServerThread);
var
ttt,qq:string; i:integer;
begin
//if thread.Connection.PeerAddress<>WinshoeIPWatch1.CurrentIP then thread.Terminate ;
ttt:=thread.Connection.ReadBuffer ;
if ttt=#5#1#0 then
begin
thread.Connection.Write(#5#0);
s:=2;
exit;
end;
if (s=3) and (thr=thread) then
begin
clientsocket1.Socket.SendText(ttt);
//ttt:=copy(ttt,11,length(ttt)-10);
for i:=1 to length(ttt) do
begin
qq:=qq+inttohex(ord(ttt),2)+' ';
end;
memo1.Lines.Add('sending: '+qq);
exit;
end;
if s<2 then exit;
thread.Connection.Write(#5#0#0#1#192#168#0#1#4#0);
s:=1 ;
if ttt[2]=#1 then
begin
clientsocket1.host:=inttostr(ord(ttt[5]))+'.'+inttostr(ord(ttt[6]))+'.'+inttostr(ord(ttt[7]))+'.'+inttostr(ord(ttt[8]));
clientsocket1.Port:= ord(ttt[9])*256+ord(ttt[10]);
ClientSocket1.Active := False;
clopen;
memo1.Lines.add( clientsocket1.host+':'+inttostr(clientsocket1.Port));
thr:=thread;
s:=3
end;
end;
procedure TForm1.WinshoeListener1Disconnect(Thread: TWinshoeServerThread);
begin
thread.Terminate;
end;
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream,mmm,www: TMemoryStream;
TmpStr,qq: String;i:integer;
begin
MyStream := TMemoryStream.Create;
try
NMUDP1.ReadStream(MyStream);
SetLength(TmpStr,NumberBytes);
MyStream.Read(TmpStr[1],NumberBytes);
if fromip='192.168.0.1' then
begin
rr:=port;
if tmpstr[4]=#3 then
begin
nmudp1.RemoteHost :=copy(tmpstr,6,ord(tmpstr[5]));
qqq:=copy(tmpstr,1,7+ord(tmpstr[5]));
qq:=copy(tmpstr,8+ord(tmpstr[5]),NumberBytes-8-ord(tmpstr[5])+1);
mmm:=Tmemorystream.Create;
mmm.Write(tmpstr[8+ord(tmpstr[5])],NumberBytes-8-ord(tmpstr[5])+1);
nmudp1.RemotePort :=ord(tmpstr[6+ord(tmpstr[5])])*256+ord(tmpstr[7+ord(tmpstr[5])]);
nmudp1.SendStream (mmm);
tmpstr:=qq;
qq:='';
for i:=1 to NumberBytes do
begin
qq:=qq+inttohex(ord(tmpstr),2)+' ';
end;
listview1.Items.insert(0);
listview1.TopItem.Caption:=fromip;
listview1.TopItem.SubItems.add(inttostr(port));
listview1.TopItem.SubItems.add(nmudp1.RemoteHost);
listview1.TopItem.SubItems.add(inttostr(nmudp1.RemotePort));
listview1.TopItem.SubItems.add (qq);
listview1.TopItem.SubItems.add(tmpstr);
end
else
begin
nmudp1.RemoteHost :=inttostr(ord(tmpstr[5]))+'.'+inttostr(ord(tmpstr[6]))+'.'+inttostr(ord(tmpstr[7]))+'.'+inttostr(ord(tmpstr[8]));
qqq:=copy(tmpstr,1,10);
mmm:=Tmemorystream.Create;
mmm.Write(tmpstr[11],NumberBytes-10);
nmudp1.RemotePort :=ord(tmpstr[9])*256+ord(tmpstr[10]);
tmpstr:=copy(tmpstr,11,NumberBytes-10);
nmudp1.SendStream (mmm);
qq:='';
for i:=1 to length(tmpstr) do
begin
qq:=qq+inttohex(ord(tmpstr),2)+' ';
end;
listview1.Items.insert(0);
listview1.TopItem.Caption:=fromip;
listview1.TopItem.SubItems.add(inttostr(port));
listview1.TopItem.SubItems.add(nmudp1.RemoteHost);
listview1.TopItem.SubItems.add(inttostr(nmudp1.RemotePort));
listview1.TopItem.SubItems.add (qq);
listview1.TopItem.SubItems.add(tmpstr);
end;
end
else
begin
nmudp1.RemoteHost :='127.0.0.1';
nmudp1.RemotePort :=rr;
for i:=1 to NumberBytes do
begin
qq:=qq+inttohex(ord(tmpstr),2)+' ';
end;
listview1.Items.insert(0);
listview1.TopItem.Caption:=fromip;
listview1.TopItem.SubItems.add(inttostr(port));
listview1.TopItem.SubItems.add(nmudp1.RemoteHost);
listview1.TopItem.SubItems.add(inttostr(nmudp1.RemotePort));
listview1.TopItem.SubItems.add (qq);
listview1.TopItem.SubItems.add(tmpstr);
www:=Tmemorystream.Create;
tmpstr:=#0#0#0#1+iptoword(fromip)+chr(port div 256)+chr(port mod 256)+tmpstr;
www.Write(tmpstr[1],length(tmpstr));
nmudp1.SendStream (www);
qq:='';
end;
finally
MyStream.Free;mmm.free;
end;
end;
function iptoword(address:string):string;
var s1:array [0..3] of word;
s2,s3:string;i,j:integer; s4:longword;
begin
s2:=address;
j:=0;
s3:='';
for i:=1 to length(s2) do
begin
if s2<>'.' then
begin
s3:=s3+s2;
end
else
begin
s1[j]:=strtoint(s3);
s3:='';
j:=j+1;
end;
end;
s1[j]:=strtoint(s3);
s4:=((s1[0]*256+s1[1])*256+s1[2])*256+s1[3];
iptoword:=chr(s1[0])+chr(s1[1])+chr(s1[2])+chr(s1[3]);
end;
procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
var tmpstr,qq:string;i:integer;
begin
tmpstr:=item.SubItems.Strings[4];
qq:='';
for i:=1 to length(tmpstr) do
begin
qq:=qq+inttohex(ord(tmpstr),2)+' ';
end;
//memo1.Lines.Add(qq+'333333333');
end;
procedure TForm1.show1Click(Sender: TObject);
var tmpstr,qq:string;i:integer;
begin
tmpstr:= listview1.Selected.SubItems.Strings[4];
qq:='';
for i:=1 to length(tmpstr) do
begin
qq:=qq+inttohex(ord(tmpstr),2)+' ';
end;
memo1.Lines.Add(qq);
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
thr.Connection.Write (socket.ReceiveText);
memo1.Lines.Add('accept');
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add('connect');
end;
procedure TForm1.ClientSocket1Write(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add('send');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
clientsocket1.Active :=true;
end;
procedure clopen;
begin
form1.ClientSocket1.Open;
end;
end.