为何我用Delphi7的indy TCPServer做 nt 的服务运行不了?(200分)

  • 主题发起人 主题发起人 peta
  • 开始时间 开始时间
P

peta

Unregistered / Unconfirmed
GUEST, unregistred user!
我如果直接做成一般 exe tcpserver 就没问题, 但如果做成 service , 就连不上。找遍google找不到答案。[:(]...那位高手指点指点,有示范源码更好咯,200分感谢![8D]

================
dmDataBackup.dfm
================
object DataBackup: TDataBackup
OldCreateOrder = False
DisplayName = 'DataBackup'
Password = ''
ServiceStartName = '/administrator'
OnExecute = ServiceExecute
OnStart = ServiceStart
OnStop = ServiceStop
Left = 336
Top = 248
Height = 150
Width = 215
object SocketServer: TIdTCPServer
Bindings = <>
CommandHandlers = <>
DefaultPort = 23
Greeting.NumericCode = 0
MaxConnectionReply.NumericCode = 0
OnConnect = SocketServerConnect
OnExecute = SocketServerExecute
ReplyExceptionCode = 0
ReplyTexts = <>
ReplyUnknownCommand.NumericCode = 0
Left = 64
Top = 32
end
end

================
dmDataBackup.pas
================
unit dmDataBackup;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
IdBaseComponent, IdComponent, IdTCPServer;

type
TDataBackup = class(TService)
SocketServer: TIdTCPServer;
procedure SocketServerExecute(AThread: TIdPeerThread);
procedure ServiceExecute(Sender: TService);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure SocketServerConnect(AThread: TIdPeerThread);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;

var
DataBackup: TDataBackup;

implementation

{$R *.DFM}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
DataBackup.Controller(CtrlCode);
end;

function TDataBackup.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;

procedure TDataBackup.SocketServerExecute(AThread: TIdPeerThread);
var
sCmd: string;
begin
with AThread.Connection do
begin
sCmd := ReadLn;

if SameText(Copy(sCmd, 0, 4), 'QUIT') then
begin
Disconnect();
end;

end;
end;


procedure TDataBackup.ServiceExecute(Sender: TService);
begin
while not Terminated do
begin
ServiceThread.ProcessRequests(false);
end;
end;

procedure TDataBackup.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
self.SocketServer.Active := false;
Stopped := true;
end;

procedure TDataBackup.ServiceStart(Sender: TService; var Started: Boolean);
begin
self.SocketServer.Active := true;
Started := true;
end;

procedure TDataBackup.SocketServerConnect(AThread: TIdPeerThread);
begin
AThread.Connection.WriteLn('connected...');
end;

end.
 
是不是IP地址,port变了
 
在Delphi6/7中利用UDP协议传送文件的例子.
http://www.aidelphi.com/6to23/docu/MyUDP(Delphi6_7).rar
你看看这个合不合你用??
 
to weichao9999
IP,port都没变
to app2001:
谢谢,但我想在服务中实现tcp server。

 
建议不要设置Password和ServiceStartName属性,再试一试。
 
试过了,还是不行。[:(]
 
连不上总有个错误信息的吧。

在服务器端多加异常捕获,看看什么地方的问题

一般服务程序调试的时候都是采用将异常信息记日志的方式

建议你首先查一下服务的启动是否正常
 
23端口是TCP的端口,你的程序当然不能用了(你和TCP冲突了,所以TCP/IP不会让你的Socket绑定23端口的,所以你会发现连接不了),1000以上的端口是自定义端口,你可以将端口改为1000以上就可以连接了,比如改为1500。
 
不是端口问题,我帮定 23 只是为了测试方便。如果不用service 是没问题的。
解决不了,只能用 atyourservice 那些将exe 变为service 的软件了。
 
后退
顶部