水
水刃
Unregistered / Unconfirmed
GUEST, unregistred user!
程序虽然能运行,但是访问本地192.168.101.2:2000连接失败,请帮忙修改,多谢!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
memo1: TMemo;
Button3: TButton;
Button4: TButton;
Memo2: TMemo;
Edit2: TEdit;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
aWSAData:TWSAData;
wVersion:word;
SockRaw:Tsocket;
addr:u_long;
saDest:Tsockaddr;
remote:Tsockaddr;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
wVersion:=makeword(2,2);
memo1.Text:='';
memo2.Text:='';
if WSAStartup(wVersion,aWSAData)<>0 then
begin
memo1.Lines.Add('无法建立WINSOCK动态链接库');
exit;
end
else
begin
if WSAStartup(wVersion,aWSAData)=0 then
memo1.Lines.Add('初始化成功');
exit;
end;
end;
//创建套接字,绑定端口并连接
procedure TForm1.Button1Click(Sender: TObject);
begin
SockRaw:=socket(PF_INET,SOCK_RAW,IPPROTO_iP);
SaDest.sin_family :=PF_INET;
if (SockRaw=INVALID_SOCKET) then
begin
memo1.Lines.Add(' 套接字创建失败') ;
end
else memo1.Lines.Add(' 套接字创建成功') ;
begin
SaDest.sin_port:=2000;//SaDest.sin_port:=htons(strtoint(trim(edit2.Text)));
//addr:=inet_addr(pchar(trim(edit1.Text)));
SaDest.sin_addr.S_addr:=inaddr_any;
end;
if( bind(SockRaw,SaDest, sizeof(SaDest))<>0) and (listen(SockRaw,somaxconn)<>0)then
begin
memo1.Lines.Add('地址绑定失败');
end
else
begin memo1.Lines.Add('地址绑定成功,建立连接') ;
remote.sin_port:=htons(strtoint(trim(edit2.Text)));
addr:=inet_addr(pchar(trim(edit1.Text)));
remote.sin_addr.S_addr:=addr;
end;
if connect(SockRaw,remote, sizeof(remote))<>0 then
begin
memo1.Lines.Add('建立连接失败');
closesocket(SockRaw);
end
else begin memo1.Lines.Add('建立连接成功') ;
end;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
memo1: TMemo;
Button3: TButton;
Button4: TButton;
Memo2: TMemo;
Edit2: TEdit;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
aWSAData:TWSAData;
wVersion:word;
SockRaw:Tsocket;
addr:u_long;
saDest:Tsockaddr;
remote:Tsockaddr;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
wVersion:=makeword(2,2);
memo1.Text:='';
memo2.Text:='';
if WSAStartup(wVersion,aWSAData)<>0 then
begin
memo1.Lines.Add('无法建立WINSOCK动态链接库');
exit;
end
else
begin
if WSAStartup(wVersion,aWSAData)=0 then
memo1.Lines.Add('初始化成功');
exit;
end;
end;
//创建套接字,绑定端口并连接
procedure TForm1.Button1Click(Sender: TObject);
begin
SockRaw:=socket(PF_INET,SOCK_RAW,IPPROTO_iP);
SaDest.sin_family :=PF_INET;
if (SockRaw=INVALID_SOCKET) then
begin
memo1.Lines.Add(' 套接字创建失败') ;
end
else memo1.Lines.Add(' 套接字创建成功') ;
begin
SaDest.sin_port:=2000;//SaDest.sin_port:=htons(strtoint(trim(edit2.Text)));
//addr:=inet_addr(pchar(trim(edit1.Text)));
SaDest.sin_addr.S_addr:=inaddr_any;
end;
if( bind(SockRaw,SaDest, sizeof(SaDest))<>0) and (listen(SockRaw,somaxconn)<>0)then
begin
memo1.Lines.Add('地址绑定失败');
end
else
begin memo1.Lines.Add('地址绑定成功,建立连接') ;
remote.sin_port:=htons(strtoint(trim(edit2.Text)));
addr:=inet_addr(pchar(trim(edit1.Text)));
remote.sin_addr.S_addr:=addr;
end;
if connect(SockRaw,remote, sizeof(remote))<>0 then
begin
memo1.Lines.Add('建立连接失败');
closesocket(SockRaw);
end
else begin memo1.Lines.Add('建立连接成功') ;
end;
end;
end.