请教DELPHI高手,ServerSocket,clientSocket 怎么用,谢谢,急! ( 积分: 10 )

  • 主题发起人 主题发起人 tigeryt
  • 开始时间 开始时间
T

tigeryt

Unregistered / Unconfirmed
GUEST, unregistred user!
我用ServerSocket做一个服务器端软件,想直接向客户端发信息,请问该怎么实现?有哪几种方法?
 
我用ServerSocket做一个服务器端软件,想直接向客户端发信息,请问该怎么实现?有哪几种方法?
 
我可以告诉你,你的邮箱是什么?
客户端:
unit Unwl_kh;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, IdBaseComponent, IdComponent, IdIPWatch;

type
Tfrmkhd = class(TForm)
ClientSocket1: TClientSocket;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Button2: TButton;
Edit3: TEdit;
IdIPWatch1: TIdIPWatch;
Label3: TLabel;
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
ConnectFlag:Boolean;

public
{ Public declarations }
end;

var
frmkhd: Tfrmkhd;

implementation

{$R *.dfm}

procedure Tfrmkhd.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
ConnectFlag:=True;
end;

procedure Tfrmkhd.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Application.MessageBox(Pchar('不能连接到IP地址 :'+ Edit2.Text+'。'+ #13+'可能是监听程序没有运行或网络故障!'),'连接错误',MB_OK+MB_ICONSTOP);
ErrorCode := 0;
end;

procedure Tfrmkhd.Button1Click(Sender: TObject);
begin
with ClientSocket1 do
begin
if Active then
begin
Active := False;//在连接之前首先关闭
ConnectFlag:=False; //置连接标志为假
end;
if Length(Edit2.Text) > 0 then
begin
Host := Edit2.Text; //指定IP地址或主机名
Active := True;
end;
end;
end;

procedure Tfrmkhd.Button2Click(Sender: TObject);
begin
if ConnectFlag then
ClientSocket1.Socket.SendText(Edit1.Text) //如果连接成功则发送字符串
else
Application.MessageBox(PChar('没有连接到地址: '+ Edit2.Text+'。'+#13+'检查IP地址是否存在或是否已经连接!'),'提示信息',MB_OK+MB_ICONSTOP);
end;

procedure Tfrmkhd.FormCreate(Sender: TObject);
begin
EDIT3.Text:=IdIPWatch1.LocalIP;
end;

end.
服务器端:
unit Unfwqd;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp;

type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
Str:PChar;
Code,V:Integer;
begin
Str:=PChar(Socket.ReceiveText); //接收字符串
Val(Str,V,Code); //处理接收的字符串,首先转换字符串为整数
if Code<>0 then
Application.MessageBox(Str,'提示信息',MB_OK+MB_ICONINFORMATION) //如果转换后的数据不是一个整型数就通过消息框来显示字符串
else
if (V=0)or(V=1)or(V=2) then
ExitWindowsEx(V,0) //调用Windows的API函数(V=0表示重新登录、V=1表示关机、V=2表示重启动)
else
Application.MessageBox(Str,'提示信息',MB_OK+MB_ICONINFORMATION); //转换后数据不为0、1、2时在消息框内显示字符串
end;

end.
 
后退
顶部