要区分tcp和udp
oicq用udp协议,connect没有用,但是tcp可以这样
另外,如果你写扫描程序可千万不要这样,应该使用别的链接方法,否则你的踪迹会被别人
发现.例如使用sys扫描或者fin扫描:
我给你异步socket的api代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,WInSock, ExtCtrls;
const WM_SOCKET=WM_USER+1; //socket消息
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Panel1: TPanel;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
Sockhd : integer; //socket句柄
Serv_Addr : Tsockaddr;//目标地址
procedure SockEvent(var msg: Tmessage);message WM_SOCKET; //处理cocket消息
procedure DspMsg(msg : string); //显示信息
{ Private declarations }
public
{ Public declarations }
end;
Form1: TForm1;
implementation
{$R *.DFM}
function lookup_hostname(const hostname:string):longint; //把域名转化成IP地址
var
RemoteHost : PHostEnt; (* no, don't free it! *)
ip_address: longint;
begin
ip_address:=-1;
try
if hostname='' then
begin (* no host given! *)
lookup_hostname:=ip_address;
EXIT;
end
else
begin
ip_address:=Winsock.Inet_Addr(PChar(hostname)); (* try a xxx.xxx.xxx.xx first *)
if ip_address=SOCKET_ERROR then begin
RemoteHost:=Winsock.GetHostByName(PChar(hostname));
if (RemoteHost=NIL) or (RemoteHost^.h_length<=0) then
begin
lookup_hostname:=ip_address;
EXIT; (* host not found *)
end
else
ip_address:=longint(pointer(RemoteHost^.h_addr_list^)^);
end;
end;
except
ip_address:=-1;
end;
lookup_hostname:=ip_address;
end;
procedure TFOrm1.DspMsg(msg: string);
begin
memo1.Lines.Add(msg+'...');
if Memo1.Lines.Count>200 then Memo1.Lines.Delete(0);
end;
procedure TForm1.SockEvent(var msg : tmessage); //处理socket消息
begin
case msg.LParam of
FD_READ: begin //标识可以读数据,当然肯定已经链接上了
dspmsg('可以读取数据');
//do what you want do
end;
FD_WRITE: begin
dspmsg('可以发送数据');
//do what you want do
end;
FD_ERROR: begin
dspmsg('发生错误');
//如果你是客户端,则应该是连接不上,即端口没有开
end;
FD_CLOSE: Begin
dspmsg('服务器断开连接');
//对方关闭连接
end;
FD_CONNECT: begin
dspmsg('连结上服务器');
//表示对方端口开放
end;
FD_ACCEPT: begin
dspmsg('接收一个请求');
//这个消息只有服务端可能出现
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var wsaData:TwsaData;
begin //启动winsock动态链接库
if WSAStartup (makeword(2,2), wsaData)<>0 then begin
messagebox(application.handle,'无法启动winsock动态连接库!','警告',MB_OK or MB_APPLMODAL or MB_ICONWARNING);
Application.Terminate;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin //关闭dll
WSACleanup;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Sockhd := socket(AF_INET,SOCK_STREAM,0); //创建socket句柄
if Sockhd<0 then begin
messagebox(application.handle,'无法创建句柄!','警告',MB_OK or MB_APPLMODAL or MB_ICONWARNING);
exit;
end;
Serv_addr.sin_addr.s_addr:= lookup_hostname(edit1.Text); //主机名
Serv_addr.sin_family := PF_INET;
Serv_addr.sin_port := htons(23); //any port you want to connect
if WSAAsyncSelect(Sockhd,Form1.handle,WM_SOCKET,FD_ACCEPT or FD_CONNECT or FD_CLOSE or FD_READ or FD_WRITE)=SOCKET_ERROR
then begin
messagebox(application.handle,'无法创建句柄!','警告',MB_OK or MB_APPLMODAL or MB_ICONWARNING);
exit;
end; //异步socket
connect(sockhd,serv_addr,sizeof(serv_addr)); //连接,结果会在前面的处理函数处理
end;
end.
相信应该可以满足你的要求