S
shean8375
Unregistered / Unconfirmed
GUEST, unregistred user!
我自己做了一个端口监控的程序,一有连接在label1里就显示对方的IP。
我的程序是成功了,但是为什么只有别人在浏览器里打我的IP才成功,而我打开某个网页却得不到他的IP。我知道是我的概念有问题,别人用tcp和我连接和我打开网页的这种tcp连接有什么区别。我的本意是想得到这个端口上所以有数据的源IP!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock, StdCtrls;
const
WM_SOCK = WM_USER + 1; //自定义windows消息
TCP_PORT =80; //设定TCP端口号
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
procedure ReadData(var Message: TMessage); message WM_SOCK;
public
{ Public declarations }
end;
var
Form1: TForm1;
HasConnected, IsServer: boolean;
SvrSocket: integer;
SvrAddrIn, CliAddrIn:TSockAddrIn;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
wsadata: TWsadata;
begin
WSAStartup($0101,WSAData);
SvrSocket := socket(AF_INET, SOCK_STREAM,IPPROTO_IP);
if (SvrSocket = INVALID_SOCKET) then
begin
ShowMessage(inttostr(WSAGetLastError())+' Socket创建失败');
CloseSocket(SvrSocket);
exit;
end;
SvrAddrIn.sin_addr.s_addr:= INADDR_ANY;
SvrAddrIn.sin_family := AF_INET;
SvrAddrIn.sin_port :=htons(TCP_PORT);
Bind(SvrSocket, SvrAddrIn, sizeof(SvrAddrIn));
Listen(SvrSocket,5);
WSAAsyncSelect(SvrSocket, Handle , WM_SOCK, FD_READ or FD_ACCEPT or FD_CONNECT or FD_WRITE or FD_CLOSE);
end;
procedure tform1.ReadData(var message:Tmessage);
begin
label1.Caption:='成功';
end;
end.
我的程序是成功了,但是为什么只有别人在浏览器里打我的IP才成功,而我打开某个网页却得不到他的IP。我知道是我的概念有问题,别人用tcp和我连接和我打开网页的这种tcp连接有什么区别。我的本意是想得到这个端口上所以有数据的源IP!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock, StdCtrls;
const
WM_SOCK = WM_USER + 1; //自定义windows消息
TCP_PORT =80; //设定TCP端口号
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
procedure ReadData(var Message: TMessage); message WM_SOCK;
public
{ Public declarations }
end;
var
Form1: TForm1;
HasConnected, IsServer: boolean;
SvrSocket: integer;
SvrAddrIn, CliAddrIn:TSockAddrIn;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
wsadata: TWsadata;
begin
WSAStartup($0101,WSAData);
SvrSocket := socket(AF_INET, SOCK_STREAM,IPPROTO_IP);
if (SvrSocket = INVALID_SOCKET) then
begin
ShowMessage(inttostr(WSAGetLastError())+' Socket创建失败');
CloseSocket(SvrSocket);
exit;
end;
SvrAddrIn.sin_addr.s_addr:= INADDR_ANY;
SvrAddrIn.sin_family := AF_INET;
SvrAddrIn.sin_port :=htons(TCP_PORT);
Bind(SvrSocket, SvrAddrIn, sizeof(SvrAddrIn));
Listen(SvrSocket,5);
WSAAsyncSelect(SvrSocket, Handle , WM_SOCK, FD_READ or FD_ACCEPT or FD_CONNECT or FD_WRITE or FD_CLOSE);
end;
procedure tform1.ReadData(var message:Tmessage);
begin
label1.Caption:='成功';
end;
end.