O
okgxsh
Unregistered / Unconfirmed
GUEST, unregistred user!
TcpServer/TcpClient控件(Delphi6.0)Server向Client发送消息的问题
首先声明:我看过了Delphi7自带的chat的例子,那里只有TcpClient向TcpServer发送消息的功能,没有Server向Client发送消息
//=====================================================
procedure TForm1.TcpServer1Accept(sender: TObject;
ClientSocket: TCustomIpClient);
var
s,s_Addr: string;
DataThread: TClientDataThread;
//=====================
S_Str:string;
I_S,I_Len:integer;
Addr:TSockAddr;
//=====================
begin
// create thread
DataThread:= TClientDataThread.Create(true);
// set the TagetList to the gui list that you
// with to synch with.
DataThread.TargetList := memRecv.lines;
// Load the Threads ListBuffer
DataThread.ListBuffer.Add('*** Connection Accepted ***');
DataThread.ListBuffer.Add('Remote Host: ' + ClientSocket.LookupHostName(ClientSocket.RemoteHost) +
' (' + ClientSocket.RemoteHost + ')');
DataThread.ListBuffer.Add('===== Begin message =====');
//=============================================================
s_Addr:=ClientSocket.RemoteHost;
DataThread.ListBuffer.Add('From : '+s_Addr);
//=============================================================
s := ClientSocket.Receiveln;
while s <> '' do
begin
DataThread.ListBuffer.Add(s);
s := ClientSocket.Receiveln;
end;
DataThread.ListBuffer.Add('===== End of message =====');
S_Str:='ok';
Addr:= ClientSocket.GetSocketAddr(ClientSocket.RemoteHost,ClientSocket.RemotePort);
//用这个给客户端Client发送信息 但是,TcpClient端无法接受到,事件触发不了******???
TcpServer1.SendTo(S_Str,Sizeof(S_Str), Addr);
DataThread.Resume;
end;
//=================================
//无法触发这个事件
procedure TForm1.TcpClient1Receive(Sender: TObject; Buf: PChar;
var DataLen: Integer);
var
SSS:string;
begin
TcpClient1.ReceiveBuf(Buf,DataLen) ;
SSS:=(Buf);
memRecv.Lines.Add('Rece RECE:'+sss)
end;
procedure TForm1.TcpClient1Send(Sender: TObject; Buf: PChar;
var DataLen: Integer);
var
SSS:string;
begin
SSS:=(Buf);
// TcpClient1.ReceiveBuf(SSS,DataLen) ;
memRecv.Lines.Add('Send RECE:'+sss)
end;
首先声明:我看过了Delphi7自带的chat的例子,那里只有TcpClient向TcpServer发送消息的功能,没有Server向Client发送消息
//=====================================================
procedure TForm1.TcpServer1Accept(sender: TObject;
ClientSocket: TCustomIpClient);
var
s,s_Addr: string;
DataThread: TClientDataThread;
//=====================
S_Str:string;
I_S,I_Len:integer;
Addr:TSockAddr;
//=====================
begin
// create thread
DataThread:= TClientDataThread.Create(true);
// set the TagetList to the gui list that you
// with to synch with.
DataThread.TargetList := memRecv.lines;
// Load the Threads ListBuffer
DataThread.ListBuffer.Add('*** Connection Accepted ***');
DataThread.ListBuffer.Add('Remote Host: ' + ClientSocket.LookupHostName(ClientSocket.RemoteHost) +
' (' + ClientSocket.RemoteHost + ')');
DataThread.ListBuffer.Add('===== Begin message =====');
//=============================================================
s_Addr:=ClientSocket.RemoteHost;
DataThread.ListBuffer.Add('From : '+s_Addr);
//=============================================================
s := ClientSocket.Receiveln;
while s <> '' do
begin
DataThread.ListBuffer.Add(s);
s := ClientSocket.Receiveln;
end;
DataThread.ListBuffer.Add('===== End of message =====');
S_Str:='ok';
Addr:= ClientSocket.GetSocketAddr(ClientSocket.RemoteHost,ClientSocket.RemotePort);
//用这个给客户端Client发送信息 但是,TcpClient端无法接受到,事件触发不了******???
TcpServer1.SendTo(S_Str,Sizeof(S_Str), Addr);
DataThread.Resume;
end;
//=================================
//无法触发这个事件
procedure TForm1.TcpClient1Receive(Sender: TObject; Buf: PChar;
var DataLen: Integer);
var
SSS:string;
begin
TcpClient1.ReceiveBuf(Buf,DataLen) ;
SSS:=(Buf);
memRecv.Lines.Add('Rece RECE:'+sss)
end;
procedure TForm1.TcpClient1Send(Sender: TObject; Buf: PChar;
var DataLen: Integer);
var
SSS:string;
begin
SSS:=(Buf);
// TcpClient1.ReceiveBuf(SSS,DataLen) ;
memRecv.Lines.Add('Send RECE:'+sss)
end;