ServerSocket能否主动向ClientSocket发信息?(50分)

  • 主题发起人 主题发起人 qj_chen
  • 开始时间 开始时间
Q

qj_chen

Unregistered / Unconfirmed
GUEST, unregistred user!
ClientSocket能正常向ServerSocket发送Text或Stream。这都是由Client端先向Server端发出请求而实现的。能否由ServerSocket向ClientSocket发出请求触发Client端的某个事件?
我在Server端发出Button1.Click事件:ServerSocket1.Socket.SendText('Ready')就出错。是否一定要在Client和Server两端都使用ServerSocket和ClientSocket控件才能解决?
 
这个我不太清楚,但是要是两方建立了连接以后一定可以的,要是用服务器去连接客户端的话要让客户端的程序使用ServerSocket才行
 
dedema先生,请提供一个实例好吗?谢谢!
 
第一次握手通信怎么办?只有客户端主动联系服务端才能在服务端上建立起客户列表。
 
ServerSocket1.Socket.Connections[0].SendBuf();
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
Edit1: TEdit;
Button1: TButton;
Edit2: TEdit;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Button3: TButton;
procedure Button2Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button3Click(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button2Click(Sender: TObject);
begin
ClientSocket1.Open;//建立连接
end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
showmessage('ok');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Clientsocket1.Socket.SendText(edit2.text); //客户机发送数据
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
s:string;
begin
s:=socket.ReceiveText;
showmessage(s);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Socket.Connections[0].SendText(edit1.text);//服务器发送数据给客户
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
s:string;
begin
s:=socket.ReceiveText;
showmessage(s);//显示收到信息
end;

end.
 
如果是大量客户端,而只想发送给某一个客户端,
那么服务器端可以使用指针来保存与客户端的socket连接
 
多人接受答案了。
 
后退
顶部