就是因为网络不通,那它总得提示返回说网络不通,
问题是如何返回这个值。
我使用的是delphi6.
问题是如何返回这个值。
我使用的是delphi6.
解决方案 »
- windows按'详细资料'查看文件夹时上边有一排可以排序,也可以拖动的头中排序是用什么做的?怎么做?
- 有没有在深圳混的兄弟,帮忙找份delphi的工作!我现在重庆,因为感情的困扰希望换个环境!谢了!qq:233018
- 是delphi烂还是如何?高手请进
- canvas显示怎么同时显示单词和指定行距显示
- delphi ado连SQL SERVER,在win98系统下运行问题
- 那位大虾有写过图像识别(OCR)的?帮我写一个条形码识别软件好吗?
- 如何实现在listview的列标题上右键单击后弹出一个快捷菜单
- 如何取出当天是星期几,请各位高手指点
- 大家好!我是Jera,我的贴子被删掉了。。呜呜,账号被封掉了。我想给分也错啊??。。。。。
- 我想在DBGrid中设计按鼠标右键弹出菜单,但
- 哪里可以下载Acrobat Reader 5.0 Works(可以制作及转换生成PDF文档)?
- 有没有从 DOS 过来的 PASCAL 或 C 高手?
try 前加:data1.SocketConnection1.Connected:=false;
后加:exit;
如果是那样连接就会变慢
如何立即传回连接失败。
summernightrain(夏夜雨):我是想在程序出错时,屏蔽这个出错信息。
lanren_me(阿波) :我就是不能立即得到连接失败的信息,至少要等到10多分钟才会回传信息这
样的等待是无法忍受的。
snjun(取个啥名儿好呢?!):就是因为连接速度慢,我要如何加快这个速度。
if E is EoleException then begin
DoSomeThing;
end
等十多分钟太夸张了吧,二三分钟差不多。
你所说的参数E的错误具体如何定义,能讲得详细一点吗?谢谢!
分数多少不成问题。
if E is EoleSysError then
ShowMessage(Inttostr((E as EOleSysError).ErrorCode));
可得出错误码,记下该错误码然后可以用如下语句:
if E is EoleSysError then
if (E as EOleSysError).ErrorCode = 错误码 then
DoSomeThing;
我是这样写的可是还是不行。
socketconnection1.host:='shd01ffff';
socketconnection1.Connected:=true;
if E is EoleSysError then .......
可是运行结果还是一样,不会返回结果还要设置什么吗?
procedure TForm1.Button2Click(Sender: TObject);
begin
socketconnection1.host:='shd01ffff';
socketconnection1.Connected:=true;
end;procedure TForm1.ApplicationEvents1Exception(Sender: TObject;
E: Exception);
begin
showmessage('11');
if E is EoleSysError then
ShowMessage(Inttostr((E as EOleSysError).ErrorCode));
end;
我在程序中调试,一会儿就会提示出错(socketconnection1.Connected:=true;)
可就是无法继续执行下一条,还有也不会执行到这里(if E is EoleSysError then)
这是为什么?
那你就把做Socket异常处理。
uses
ScktComp;procdure MySocketError(ErrorCode: Integer);
begin
ShowMessage(Format('Error: %s', SysErrorMessage(ErrorCode));
end;buttononclick:
begin
ScktComp.SetErrorProc(MySocketError);
try
SocketConnection1.Connected := True;
finally
ScktComp.SetErrorProc(nil);
end;
end;
这句无法通过如何改。
忘打[]
我用断点调试,当这句出错时,提示'sindows socket error:(10049),on api 'connect''
然后程序不会往下运行,就一直停留在这句上。
不信你编绎后不要在delphi下运行,然后在
procedure MySocketError(ErrorCode: Integer);
begin
ShowMessage('dana, Socket出错了,错误代码是:%d,可以用SysErrorMessage函数看一下错误代码的意思');
end;如果SocketConnection的异常是在MySocketError中处理的,那么就是Socket的错,
如果不是,那就是你的服务端程序的问题了
》》没错,我不在调试状态下同样也是不行
什么意思?
主机不存在,连接肯定会出错,所以在MySocketError作你的错误处理,
还会不会连接10分钟啦。
var
Form1: TForm1;
procedure MySocketError(ErrorCode: Integer);
begin
Form1.ErrorProc(ErrorCode);
end;procedure TForm1.ErrorProc(ErrorCode: Integer);
begin
MessageBox(Handle, '连接服务器主机出错,请确定选择的是服务器。', 'Error', MB_OK + MB_ICONERROR);
//do other thing
end;不然你用TClientSocket试一下连接主机,连接成功后再试其它原因。
Handling Winsock errors - by Borland Developer Support Staff Technical Information DatabaseTI4524D.txt - Handling Winsock errorsCategory :Windows API
Platform :All-32Bit
Product :All32Bit, Description:
For any of the following exception handling methods to work,
the VCL must in some way become aware that an error condition
exists. If a call to the Winsock does not return, or does
not provide information to the TCustomWinSocket descendant
that called it, then there is no mechanism to handle the condition.
OnError exception handler One method for trapping exception conditions in a descendant of
TCustomWinSocket is to use an OnError exception handler. This
method will only handle a limited set of conditions because the
mechanism provided by the Winsock for event notification only
reacts to a limited list of conditions. To be notified of an
exception condition within the Winsock, TCustomWinSocket registers
user message CM_SocketMessage to be sent to the component, and the
CMSocketMessage message handler raises an exception. The message
is registered with the Winsock by an API call to WSASyncSelect.
WSASyncSelect is a request for event notification of socket read,
writes, connect, close, and accept events. If the exception
condition is not read, write, connect, close or accept, or if the
CM_SocketMessage is not sent by the Winsock for any reason, the
error handler will not fire.Usage: procedure TChatForm.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
const
ErrorEvents: array[eeGeneral..eeAccept] of string = (
'eeGeneral',
'eeSend',
'eeReceive',
'eeConnect',
'eeDisconnect',
'eeAccept'
);
begin
ListBox1.Items.Add('ClientSocketError. TErrorEvent: ' +
ErrorEvents[ErrorEvent] + ' ErrorCode: ' + IntToStr(ErrorCode));
ErrorCode := 0; // don't raise an exception
end;
Definition: procedure TCustomWinSocket.CMSocketMessage(var Message: TCMSocketMessage); function CheckError: Boolean;
var
ErrorEvent: TErrorEvent;
ErrorCode: Integer;
begin
if Message.SelectError <> 0 then
begin
Result := False;
ErrorCode := Message.SelectError;
case Message.SelectEvent of
FD_CONNECT: ErrorEvent := eeConnect;
FD_CLOSE: ErrorEvent := eeDisconnect;
FD_READ: ErrorEvent := eeReceive;
FD_WRITE: ErrorEvent := eeSend;
FD_ACCEPT: ErrorEvent := eeAccept;
else
ErrorEvent := eeGeneral;
end;
Error(Self, ErrorEvent, ErrorCode);
if ErrorCode <> 0 then
raise ESocketError.CreateFmt(sASyncSocketError, [ErrorCode]);
end else Result := True;
end;begin
with Message do
if CheckError then
case SelectEvent of
FD_CONNECT: Connect(Socket);
FD_CLOSE: Disconnect(Socket);
FD_READ: Read(Socket);
FD_WRITE: Write(Socket);
FD_ACCEPT: Accept(Socket);
end;
end;
Object Pascal Exception Handling
You can also wrap a specific call in a try..except block or
setting an application level exception handler. For this to
work, the component must in some way become aware of the
exception condition and an exception must be raised for the
exception to be trapped here.Example of Application Exception Handler: TChatForm = class(TForm)
.
.
public
procedure AppException(Sender: TObject; E: Exception);
end;
.
.
implementation
.
.
procedure TChatForm.AppException(Sender: TObject; E: Exception);
begin
ListBox1.Items.Add('AppException: ' + E.Message);
end;procedure TChatForm.FormCreate(Sender: TObject);
begin
Application.OnException := AppException;
end;
Example of Try..Except block:with ClientSocket do
try
Active := True;
except
on E: Exception do
ListBox1.Items.Add('Try..except during open: ' + E.Message);
end;
end;SocketErrorProc
For calls that use the CheckSocketResult function to check the
result returned by WSAGetLastError, errors can be handled in a
programmer defined function by setting the SocketErrorProc.Usage: Interface
.
.
procedure MySocketError(ErrorCode: Integer);implementation
.
.
procedure MySocketError(ErrorCode: Integer);
begin
ShowMessage('MySocketError: ' + IntToStr(ErrorCode));
end;procedure TChatForm.FormCreate(Sender: TObject);
begin
SocketErrorProc := MySocketError;
end;
Defined: function CheckSocketResult(ResultCode: Integer; const Op: string):
Integer;
begin
if ResultCode <> 0 then
begin
Result := WSAGetLastError;
if Result <> WSAEWOULDBLOCK then
if Assigned(SocketErrorProc) then
SocketErrorProc(Result)
else raise ESocketError.CreateFmt(sWindowsSocketError,
[SysErrorMessage(Result), Result, Op]);
end else Result := 0;
end;
Help Text for SocketErrorProc: Unit ScktCompSocketErrorProc handles error messages that are received from a Windows socket
connection.threadvar SocketErrorProc: procedure (ErrorCode: Integer);Assign a value to SocketErrorProc to handle error messages
from Windows socket API calls before the socket component raises an
exception. Setting SocketErrorProc prevents the socket component from
raising an exception. SocketErrorProc is a thread-local variable. It
only handles errors that arise from the Windows socket API calls made within
a single execution thread.