winsock当前状态的表示。。。(100分)

  • 主题发起人 主题发起人 skylove
  • 开始时间 开始时间
S

skylove

Unregistered / Unconfirmed
GUEST, unregistred user!
在vb中,winsock的当前状态是可以查询,得到一个返回字符的。
例如,未连接,正在连接中,已连接成功。。。这几个状态都可以返回一个定值。。。
不知道delphi的winsock中有类似的么??
或者说,我该怎么在winsock中查询当前winsock执行时候的状态呢???
感谢各位,务必详尽一点!
 
你可以是个全局的字符串:
在以下一些事件中赋值就可以啦:
OnAccept
OnClientConnect
OnClientDisconnect
OnClientRead
OnClientWrite
OnListen
OnThreadEnd
OnThreadStart
.
 
可是我要的是知道当前的状态啊。。。
例如,我做了个网络小游戏,首先当然是两边要对连,然后,等到对联成功后开始下一步。
如果出现错误,当然就要返回。。(错误的情形很多,没连接,连接了,但是对方不理会等)
所以想知道当前连接的状态啊。。。。至于收到信息那些事件,很简单
我想要的是随时获取winsock控件的状态,如,开始和xxxx连接。。。连接中。。。连接
结果(成功,失败),有什么办法可以知道这个么?
 
你用的Winsock是VCL控件还是ActiveX控件?
 
我打算用delphi自己的vcl控件。。。不过如果实在找不到资料,我用vb的那个控件。。至少
那个我知道怎么得到winsocket的当前运行状态
 
我也想知道Delphi的TClientSocket是怎么实现的?
 
不知道对你是否有帮助!

winsock状态的判断、启动与停止

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
uses
WinSvc, WinTypes;
function ServiceGetKeyName(
sMachine,
sServiceDispName : string ) : string;
var
//
// service control
// manager handle
schm : SC_Handle;

//
// max key name len
nMaxNameLen : dWORD;
//
// temp. string
psServiceName : PChar;
begin
Result := '';

// expect a service key
// name shorter than 255
// characters
nMaxNameLen := 255;

// connect to the service
// control manager
schm := OpenSCManager(
PChar(sMachine),
Nil,
SC_MANAGER_CONNECT);

// if successful...
if(schm > 0)then
begin
psServiceName :=
StrAlloc(nMaxNameLen+1);

if(nil <> psServiceName)then
begin
if( GetServiceKeyName(
schm,
PChar(sServiceDispName),
psServiceName,
nMaxNameLen))then
begin
psServiceName
[nMaxNameLen] := #0;

Result :=
StrPas( psServiceName );
end;

StrDispose(psServiceName);
end;

// close service control
// manager handle
CloseServiceHandle(schm);
end;
end;

function ServiceStart(
sMachine,
sService : string ) : boolean;
var
//
// service control
// manager handle
schm,
//
// service handle
schs : SC_Handle;
//
// service status
ss : TServiceStatus;
//
// temp char pointer
psTemp : PChar;
//
// check point
dwChkP : DWord;
begin
//ss.dwCurrentState := -1;

// connect to the service
// control manager
schm := OpenSCManager(PChar(sMachine), Nil, SC_MANAGER_CONNECT);

// if successful...
if(schm > 0)then
begin
// open a handle to
// the specified service
schs := OpenService(schm,PChar(sService),SERVICE_START or SERVICE_QUERY_STATUS);
// we want to
// start the service and
// query service status


// if successful...
if(schs > 0)then
begin
psTemp := Nil;
if(StartService(schs, 0, psTemp)) then
begin
// check status
if(QueryServiceStatus(schs,ss)) then
begin
while(SERVICE_RUNNING<> ss.dwCurrentState)do
begin
//
// dwCheckPoint contains a
// value that the service
// increments periodically
// to report its progress
// during a lengthy
// operation.
//
// save current value
//
dwChkP := ss.dwCheckPoint;

//
// wait a bit before
// checking status again
//
// dwWaitHint is the
// estimated amount of time
// the calling program
// should wait before calling
// QueryServiceStatus() again
//
// idle events should be
// handled here...
//
Sleep(ss.dwWaitHint);
application.ProcessMessages;

if(not QueryServiceStatus(
schs,
ss))then
begin
// couldn't check status
// break from the loop
break;
end;

if(ss.dwCheckPoint <
dwChkP)then
begin
// QueryServiceStatus
// didn't increment
// dwCheckPoint as it
// should have.
// avoid an infinite
// loop by breaking
break;
end;
end;
end;
end;

// close service handle
CloseServiceHandle(schs);
end;

// close service control
// manager handle
CloseServiceHandle(schm);
end;

// return TRUE if
// the service status is running
Result :=SERVICE_RUNNING = ss.dwCurrentState;
end;
function ServiceStop(
sMachine,
sService : string ) : boolean;
var
//
// service control
// manager handle
schm,
//
// service handle
schs : SC_Handle;
//
// service status
ss : TServiceStatus;
//
// check point
dwChkP : DWord;
begin
// connect to the service
// control manager
schm := OpenSCManager(
PChar(sMachine),
Nil,
SC_MANAGER_CONNECT);

// if successful...
if(schm > 0)then
begin
// open a handle to
// the specified service
schs := OpenService(
schm,
PChar(sService),
// we want to
// stop the service and
SERVICE_STOP or
// query service status
SERVICE_QUERY_STATUS);

// if successful...
if(schs > 0)then
begin
if(ControlService(
schs,
SERVICE_CONTROL_STOP,
ss))then
begin
// check status
if(QueryServiceStatus(
schs,
ss))then
begin
while(SERVICE_STOPPED
<> ss.dwCurrentState)do
begin
//
// dwCheckPoint contains a
// value that the service
// increments periodically
// to report its progress
// during a lengthy
// operation.
//
// save current value
//
dwChkP := ss.dwCheckPoint;

//
// wait a bit before
// checking status again
//
// dwWaitHint is the
// estimated amount of time
// the calling program
// should wait before calling
// QueryServiceStatus() again
//
// idle events should be
// handled here...
//
Sleep(ss.dwWaitHint);
application.ProcessMessages;

if(not QueryServiceStatus(
schs,
ss))then
begin
// couldn't check status
// break from the loop
break;
end;

if(ss.dwCheckPoint <
dwChkP)then
begin
// QueryServiceStatus
// didn't increment
// dwCheckPoint as it
// should have.
// avoid an infinite
// loop by breaking
break;
end;
end;
end;
end;

// close service handle
CloseServiceHandle(schs);
end;

// close service control
// manager handle
CloseServiceHandle(schm);
end;

// return TRUE if
// the service status is stopped
Result :=
SERVICE_STOPPED =
ss.dwCurrentState;
end;



procedure TForm1.Button1Click(Sender: TObject);//启动服务
begin
if ServiceStart('',ServiceGetKeyName( '',edit1.Text) ) then
showmessage('成功')
else
showmessage('不成功');


end;

procedure TForm1.Button2Click(Sender: TObject);//停止服务
begin
if ServiceStop( '', ServiceGetKeyName('',edit1.Text) ) then
showmessage('成功')
else
showmessage('不成功');


end;
function ServiceGetStrCode(
nID : integer ) : string;
var
s : string;
begin
case nID of
SERVICE_STOPPED:
s := 'STOPPED';
SERVICE_RUNNING:
s := 'RUNNING';
SERVICE_PAUSED:
s := 'PAUSED';
SERVICE_START_PENDING:
s := 'START/PENDING';
SERVICE_STOP_PENDING:
s := 'STOP/PENDING';
SERVICE_CONTINUE_PENDING:
s := 'CONTINUE/PENDING';
SERVICE_PAUSE_PENDING:
s := 'PAUSE/PENDING';
else
s := 'UNKNOWN';
end;

Result := s;
end;

function ServiceGetStatus(
sMachine,
sService : string ) : DWord;
var
//
// service control
// manager handle
schm,
//
// service handle
schs : SC_Handle;
//
// service status
ss : TServiceStatus;
//
// current service status
dwState : integer;
begin
dwState:= -1;

// connect to the service
// control manager
schm := OpenSCManager(
PChar(sMachine),
Nil,
SC_MANAGER_CONNECT);

// if successful...
if(schm > 0)then
begin
// open a handle to
// the specified service
schs := OpenService(
schm,
PChar(sService),
// we want to
// query service status
SERVICE_QUERY_STATUS);

// if successful...
if(schs > 0)then
begin
// retrieve the current status
// of the specified service
if(QueryServiceStatus(
schs,
ss))then
begin
dwState := ss.dwCurrentState;
end;

// close service handle
CloseServiceHandle(schs);
end;

// close service control
// manager handle
CloseServiceHandle(schm);
end;

Result := dwState;
end;



procedure TForm1.Button3Click(Sender: TObject);//检查服务状态
var
n : integer;
sKeyName,
s : string;
begin
sKeyName :=
ServiceGetKeyName(
'',
edit1.Text);//'Fax Service'



n := ServiceGetStatus(
'',
sKeyName );

s := ServiceGetStrCode( n );


if(SERVICE_STOPPED = n)then
begin
showmessage('停止');
end;
if(SERVICE_RUNNING = n)then
begin
showmessage('运行');
end;
end;
end.
 
接受答案了.
 
后退
顶部