1.Asynchronous socket error 10061
Asynchronous socket error 10061
æå¡å¨é误ï¼
端å£æ«æçé®é¢
å¨å端å£æ«ææ¶ ,源码首页图片的源码å¦æä¸æ主æºç¹å®ç«¯å£æ æ³éä¿¡ ,
å°±æ¤ä¸»æºèè¨ ,ææ³åºè¯¥æ以ä¸ä¸¤ç§æ åµ :
1 ãæ¤å°åä¸æ ä»»ä½ä¸»æºåå¨
2 ãæ主æºä½è¢«æ«æçç¹å®ç«¯å£ä¸åå¨ ( ä¹å¯è½æ¯è¢« firewall è¿æ»¤äº )
å¦ä½å¾ç¥æ端å£ä¸æå¼
ç»ä½ æ¥ä¸ªç®åçå§ï¼
procedure TForm1.Timer1Timer(Sender: TObject);
var
I : integer;
begin
Memo1.Clear;
for I := 0 to do begin
ServerSocket1.Close;
ServerSocket1.Port := I;
try
ServerSocket1.Open;
except
Memo1.Lines.Add(IntToStr(I) + ' 端å£è¢«æå¼ !');
end;
end;
end;
对ä¸èµ· ,ææçæ¯å«äººæºå¨ä¸ç PORT
ä½ æ¯è¯´ PORT åªè½è¢«ä¸ä¸ªç¨åºæå¼ä¹ ?
å¯æ¯ ,æç¨ OICQ æ¶å¨æå¼ æ²¡é®é¢å
ææä¸é¢çç¨åºæ¹äºä¸ä¸ ,ä¹å¯ä»¥ç¨çãä½ å°±å»è¯å¾è¿æ¥å¯¹æ¹ ,å¦æéäº ,说ææ¤ç«¯å£è¢«æ
å¼ã
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(' ç«¯å£ '+IntToStr(Socket.RemotePort)+' 被æå¼ï¼ ');
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
ClientSocket1.Close;
ClientSocket1.Port := PortID;
try
ClientSocket1.Open;
except
end;
Inc(PortID);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
PortID := 1;
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
try
ClientSocket1.Close;
except
end;
Memo2.Lines.add(IntToStr(Socket.remotePort));
end;
åéªæ¾ä½ çæ¹æ³æè¯è¿äºå¯æ¯æ±é :asynchronous socket error
--------------------------------------------------------------------------------
æ¥èª :xueminliu æ¶é´ :-3-3 :: ID:
è¦åºå 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> 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(); //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.
ç¸ä¿¡åºè¯¥å¯ä»¥æ»¡è¶³ä½ çè¦æ±
请é®å¦ä½ç¼ç¨åºåè¿ä¸¤ç§æ åµ
æ好详ç»ä¸ç¹å ( æå¾ç¬¨ç )
å¦æå¨æ¤å°åä¸æ 主æºåå¨ ,åååºçæ°æ®å å¾ä¸å°ååº ,åºç¨ç¨åºä¼çå¾ è¶ æ¶æ
认为è¿æ¥å¤±è´¥ ( 被 firewall è¿æ»¤æ¶æ åµä¸æ · ),è¥æ主æºä½è¢«æ«æçç¹å®ç«¯å£ä¸åå¨æ¶ ,
该主æºä¼ååºç®ç端å£ä¸åå¨çåºç
è³äºå¦ä½ç¼ç¨å®ç° ,åºè¯¥å¯ä»¥ç±é误ç æ¥å¤æ ,å¨ OnError äºæ ä¸å¤å® ErrorCode æ¯å¤
å° ,ååå«å¤ç ,ErrorCode ç详æ åè§ Help
è¿ä¹é«æ·±çé®é¢æ å ,å°äº
端å£æ«æä¸æ¯è¿ä¹ç®å ,å¦å大家é½å
é¦å ä½ æ«æ人家ç端å£ä¼çä¸èªå·±çç迹 ,ç³»ç»ææ¥å¿å¯ä»¥å¯ç
å æ¤æ们å端å£æ«æççæ¶åç»å¯¹ä¸ä¼ç´æ¥è¿æ¥å«äºº ,èæ¯éè¿å°å±çæ¥å£ç¼ç¨
ä¾å¦å¨ TCP ä¸æ¬¡æ¡æç第ä¸æ¬¡æ¾å¼ ,对æ¹å°±ä¸ä¼ææ¥å¿ ,è¿ç§°ä¸º sys æ«æ
ç»å¯¹æ¹ç«¯å£åæå¼è¿æ¥ç请æ±ç§°ä¸º fin æ«æ .
éè¿è¿ä¸¤ç§æ«ææ¹å¼é½å¯ä»¥å¾ç¥å¯¹æ¹ç端å£æ¯å¦å¼ ,èä¸ä¸ä¼çä¸ç迹 .
æ»ä¹ç«¯å£æ«æéé¢æå¾å¤å¦é® ,ä¸æ¯è¿éå¯ä»¥è¯´æ¸ é¤ç
æä¹åå ,å¦ä½ æ说çè¯å¥½è±¡è¦ç´æ¥è°ç¨ socket api?
æç°å¨é¦å å ³å¿çæ¯ææåºçé®é¢ ,å¦ä½ç¼ç¨åºåè¿ä¸¤ç§æ åµ :
1 ãæ¤å°åä¸æ ä»»ä½ä¸»æºåå¨
2 ãæ主æºä½è¢«æ«æçç¹å®ç«¯å£ä¸åå¨ ( ä¹å¯è½æ¯è¢« firewall è¿æ»¤äº )
è¿æ ,为ä»ä¹ææ clientsocket ç onread éç errorcode 设为 0 äº ,
è¿æ¯å¸¸å¸¸ä¼åºç° delphi èªå·±çé误æ¶æ¯æ示 ,象 , ä»ä¹ç ,
è¿å¥½è±¡æ¯å¦å¤ä¸ç§ error code,å¦è½æå®å±è½ææ³å°±ä¸ä¼åºç°æç¤ºäº .
æ¯åï¼å¦ææ¯ ,该æä¹åå¢ã
æå : å¦æè½ç»æä¸ä¸ªå¤çº¿ç¨ç端å£æ«ææºç ,æåç» å ( ççå¾ç©·å )
ææ¾å°äº help éçæå ³è¯´æ ( æ¯å¨ç´¢å¼ä¸ Error TCP Event éæ¾å°ç )
WinSock Error Codes
The following error codes apply to the WinSock ActiveX Controls.
Error Code Error Message
The operation is canceled.
The requested address is a broadcast address, but flag is not set.
Invalid argument.
Socket not bound, invalid address or listen is not invoked prior to accept.
No more file descriptors are available, accept queue is empty.
Socket is non-blocking and the specified operation will block.
A blocking Winsock operation is in progress.
The operation is completed. No blocking operation is in progress.
The descriptor is not a socket.
Destination address is required.
The datagram is too large to fit into the buffer and is truncated.
The specified port is the wrong type for this socket.
Option unknown, or unsupported.
The specified port is not supported.
Socket type not supported in this address family.
Socket is not a type that supports connection oriented service.
Address Family is not supported.
Address in use.
Address is not available from the local machine.
Network subsystem failed.
The network cannot be reached from this host at this time.
Connection has timed out when SO_KEEPALIVE is set.
Connection is aborted due to timeout or other failure.
The connection is reset by remote side.
No buffer space is available.
Socket is already connected.
Socket is not connected.
Socket has been shut down.
The attempt to connect timed out.
Connection is forcefully rejected.
Socket already created for this object.
Socket has not been created for this object.
Authoritative answer: Host not found.
Non-Authoritative answer: Host not found.
Non-recoverable errors.
Valid name, no data record of requested type.
ææ³åªè¦å¯¹å®è¿è¡æå ³æä½å°±è½å®å ¨å±è½ winsocket é误æ¶æ¯ ( è³å°
è½å±è½å¾å¤ onerror éç errorcode åæ°æ æ³å±è½çæ¶æ¯ )
æç»äºæ¾å°åå æå¨äº
å¨æå¼ Socket æ¶ä¹è¦æè·å¼å¸¸
try
ClientSocket.Open;
except
MessageBox(MainForm.Handle,'Error connecting to this address','Connect',MB_ICONEXCLAMATION);
end;
å¨ OnError ä¸æåè¦å° ErrorCode 置为 0
if ErrorEvent=eeConnect then
begin
Socket.Close;
MessageBox(MainForm.Handle,'Error connecting to this address','Connect',MB_ICONEXCLAMATION);
end
else if ErrorEvent=eeSend then
Socket.Close;
ErrorCode:=0;
ä½ å¯è½æ å第ä¸æ¥
èè¿æ ·ä¹å¯ä»¥åºåä½ æ说ç两ç§æ åµ
1 ã第äºæ¥ OnError å°±æ¯æ¤å°åä¸æ ä»»ä½ä¸»æºåå¨ ,å°è¶ æ¶å°±è§¦å OnError äºä»¶
2 ã第ä¸æ¥ææå°å¼å¸¸å°±æ¯æ主æºä½è¢«æ«æçç¹å®ç«¯å£ä¸åå¨