用Indy组件写了个小聊天室。但好象客户端不能的消息没有发送到服务器。
procedure TForm1.EditMsgKeyPress(Sender: TObject; var Key: Char);
begin
if key = #13 then
begin
if IdTCPClient.Connected = True then
begin
IdTCPClient.WriteLn(EditName.Text+':'+EditMsg.Text+EOL);
EditMsg.Clear;
end;
end;
end;
这个客户端发送消息事件的代码。也不知道是不是服务器没有转发,下面的服务器处理收到消息的代码:
procedure TForm1.IdTCPServerConnect(AThread: TIdPeerThread);
var
Buf: string;
i:integer;
begin
Buf:=AThread.Connection.ReadLn;
if Buf='' then exit;
if Buf[1]=CMD then
begin
//系统命令
Buf:=Copy(Buf,2,Length(Buf)-1);
AThread.Connection.WriteLn('Welcome:'+Buf+EOL);
end else begin
//聊天信息
list:=IdTCPServer.Threads.LockList;
try
for i:=0 to list.Count-1 do
try
AThread.Connection.WriteLn(Buf);
except
AThread.Stop;
end;
finally
IdTCPServer.Threads.UnlockList;
end;
end;
end;
请高手帮忙看下是怎么回事,万分感谢!
昨天有点头晕了,几句话都说不清楚了。
总归就是客户端没有正常显示收到的消息。补充一个客户端处理收到消息的事件:
procedure TForm1.IdTCPClientConnected(Sender: TObject);
var
Lin:string;
begin
Lin:=IdTCPClient.ReadLn();
if Lin='' then exit;
ListBoxMsg.Items.Add(Lin);
end;
Delphi Socekt
答案:2 悬赏:10 手机版
解决时间 2021-01-23 11:44
- 提问者网友:半生酒醒
- 2021-01-23 02:37
最佳答案
- 五星知识达人网友:轮獄道
- 2021-01-23 04:12
IdTCPServerConnect
改用IdTCPServerExecute事件
改用IdTCPServerExecute事件
全部回答
- 1楼网友:洒脱疯子
- 2021-01-23 04:35
好像是事件写错地方了
客户端的,你看下.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ImgList, StdCtrls, Buttons, ComCtrls, ScktComp, ToolWin,
ExtCtrls;
const
SocketHost = '172.20.100.22'; //服务器端地址
type
TfrmClientMain = class(TForm)
ControlBar1: TControlBar;
ClientSocket: TClientSocket;
Edit1: TEdit;
Memo1: TMemo;
StatusBar: TStatusBar;
btnSend: TBitBtn;
ImageList1: TImageList;
tbConnected: TButton;
tbSend: TButton;
tbclose: TButton;
tbDisconnected: TButton;
procedure tbConnectedClick(Sender: TObject);
procedure tbDisconnectedClick(Sender: TObject);
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
procedure tbSendClick(Sender: TObject);
procedure tbCloseClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmClientMain: TfrmClientMain;
implementation
{$R *.dfm}
//打开套接字连接
procedure TfrmClientMain.tbConnectedClick(Sender: TObject);
begin
ClientSocket.Open ;
end;
//关闭套接字连接
procedure TfrmClientMain.tbDisconnectedClick(Sender: TObject);
begin
ClientSocket.Close;
end;
//接受服务器端的回复
procedure TfrmClientMain.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(Socket.ReceiveText);
end;
//发送信息到服务器端
procedure TfrmClientMain.tbSendClick(Sender: TObject);
begin
ClientSocket.Socket.SendText(Edit1.Text);
end;
procedure TfrmClientMain.tbCloseClick(Sender: TObject);
begin
Close;
end;
//设置要连接的服务器端地址
procedure TfrmClientMain.FormShow(Sender: TObject);
begin
ClientSocket.Host := SocketHost;
end;
//已经连接到服务器端
procedure TfrmClientMain.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
tbSend.Enabled := True;
tbDisconnected.Enabled :=True;
btnSend.Enabled := True;
StatusBar.Panels[0].Text := '已经连接到 '+ Socket.RemoteHost ;
end;
//正在连接到服务器端
procedure TfrmClientMain.ClientSocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar.Panels[0].Text := '正在连接到服务器... ' ;
end;
//当断开与服务器端的连接时发生
procedure TfrmClientMain.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
tbSend.Enabled := False;
btnSend.Enabled := False;
tbDisconnected.Enabled := False;
StatusBar.Panels[0].Text := '已经断开与 '+ Socket.RemoteHost +' 的连接';
end;
procedure TfrmClientMain.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
ClientSocket.Close ;
end;
//当与服务器端的连接发生错误时
procedure TfrmClientMain.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
StatusBar.Panels[0].Text := '与服务器端的连接发生错误';
ErrorCode := 0;
end;
end.
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯