如何偵測某一端口是否被占用? |
答題得分者是:pcboy2
|
Py
一般會員 ![]() ![]() 發表:3 回覆:7 積分:2 註冊:2006-07-08 發送簡訊給我 |
|
pcplayer99
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:146 回覆:790 積分:632 註冊:2003-01-21 發送簡訊給我 |
|
Py
一般會員 ![]() ![]() 發表:3 回覆:7 積分:2 註冊:2006-07-08 發送簡訊給我 |
|
pcboy
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
unit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; Edit1: TEdit; Edit2: TEdit; btnCheck: TButton; procedure btnCheckClick(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses winsock; {$R *.dfm} procedure TForm1.btnCheckClick(Sender: TObject); procedure PortScan(AIP: string; APort: integer); var data: TWSAData; sock: TSocket; addr: TSockAddrIn; iaddr: TInAddr; begin try if (WSAStartup($0101, data) = 0) then begin sock := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock <> INVALID_SOCKET) then begin // zero out the addr struct. FillChar(addr, SizeOf(TSockAddrIn), 0); FillChar(iaddr, SizeOf(TInAddr), 0); //fill in the internet address iaddr.S_addr := inet_addr(PChar(AIP)); //fill in the rest of the address. addr.sin_family := PF_INET; addr.sin_port := htons(APort); addr.sin_addr := iaddr; // try to connect, if it failes then the port is closed if (connect(sock, addr, SizeOf(TSockAddrIn)) = 0) then begin ShowMessage('Port ' IntToStr(APort) ' IS open on ' AIP); end else begin ShowMessage('Port NOT open ' IntToStr(APort) ' on ' AIP); end; end else begin ShowMessage('There was an error creating the socket.'); end; end else begin ShowMessage('Failed to initialize winsock.'); end; finally WSACleanup(); end; end; begin btnCheck.Enabled := False; PortScan(Edit1.Text, StrToInt(Edit2.Text)); btnCheck.Enabled := True; end; procedure TForm1.FormCreate(Sender: TObject); begin Edit1.Text:='127.0.0.1'; Edit2.Text:='80'; end; end.
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |