好像Client端发的信息Server可以接收,但Server发的信息Client收不到,使用INDY的TCP控件。
Client端代码:
unit ClientUnit;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient;type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Button1: TButton;
Shape1: TShape;
ComboBox1: TComboBox;
IdTCPClient1: TIdTCPClient;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
RecMsg: String;
begin
if comboBox1.text= '' then exit;
if edit1.text= '' then exit;
if edit2.text= '' then exit;
idtcpclient1.Host:= edit1.Text;
idtcpclient1.Port:= StrToInt(edit2.Text);
try
try
idtcpclient1.Connect;
idtcpclient1.writeln(comboBox1.text);
RecMsg:= idtcpclient1.readln;
showmessage(recmsg);
if RecMsg= 'r' then
begin
shape1.Brush.Color:= clRed;
idtcpClient1.Writeln('Red ok');
end
else if RecMsg= 'y' then
begin
shape1.Brush.Color:= clYellow;
idtcpClient1.Writeln('Yellow ok');
end
else if RecMsg= 'b' then
begin
shape1.Brush.Color:= clBlue;
idtcpClient1.Writeln('Blue ok');
end
else if RecMsg= 'g' then
begin
shape1.Brush.Color:= clGreen;
idtcpClient1.Writeln('Green ok');
end
else if RecMsg= 'l' then
begin
shape1.Brush.Color:= clLime;
idtcpClient1.Writeln('Lime ok');
end;
finally
idtcpclient1.Disconnect;
end;
except
on e: Exception do
ShowMessage('TimeOut!');
end;
end;end.Server端代码:
unit ServerUnit;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls;type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
RecMsg: String;
begin
RecMsg:= AThread.Connection.ReadLn;
showmessage(recMsg);
if pos('红', RecMsg)>1 then
AThread.Connection.writeln('r')
else if pos('黄', RecMsg)>1 then
AThread.Connection.Writeln('y')
else if pos('蓝', RecMsg)>1 then
AThread.Connection.Writeln('b')
else if pos('绿', RecMsg)>1 then
AThread.Connection.Writeln('g')
else if pos('青', RecMsg)>1 then
AThread.Connection.Writeln('l')
else if Pos('ok', RecMsg)>1 then
ShowMessage(RecMsg); end;end.
Client端代码:
unit ClientUnit;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient;type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Button1: TButton;
Shape1: TShape;
ComboBox1: TComboBox;
IdTCPClient1: TIdTCPClient;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
RecMsg: String;
begin
if comboBox1.text= '' then exit;
if edit1.text= '' then exit;
if edit2.text= '' then exit;
idtcpclient1.Host:= edit1.Text;
idtcpclient1.Port:= StrToInt(edit2.Text);
try
try
idtcpclient1.Connect;
idtcpclient1.writeln(comboBox1.text);
RecMsg:= idtcpclient1.readln;
showmessage(recmsg);
if RecMsg= 'r' then
begin
shape1.Brush.Color:= clRed;
idtcpClient1.Writeln('Red ok');
end
else if RecMsg= 'y' then
begin
shape1.Brush.Color:= clYellow;
idtcpClient1.Writeln('Yellow ok');
end
else if RecMsg= 'b' then
begin
shape1.Brush.Color:= clBlue;
idtcpClient1.Writeln('Blue ok');
end
else if RecMsg= 'g' then
begin
shape1.Brush.Color:= clGreen;
idtcpClient1.Writeln('Green ok');
end
else if RecMsg= 'l' then
begin
shape1.Brush.Color:= clLime;
idtcpClient1.Writeln('Lime ok');
end;
finally
idtcpclient1.Disconnect;
end;
except
on e: Exception do
ShowMessage('TimeOut!');
end;
end;end.Server端代码:
unit ServerUnit;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls;type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
RecMsg: String;
begin
RecMsg:= AThread.Connection.ReadLn;
showmessage(recMsg);
if pos('红', RecMsg)>1 then
AThread.Connection.writeln('r')
else if pos('黄', RecMsg)>1 then
AThread.Connection.Writeln('y')
else if pos('蓝', RecMsg)>1 then
AThread.Connection.Writeln('b')
else if pos('绿', RecMsg)>1 then
AThread.Connection.Writeln('g')
else if pos('青', RecMsg)>1 then
AThread.Connection.Writeln('l')
else if Pos('ok', RecMsg)>1 then
ShowMessage(RecMsg); end;end.
解决方案 »
- 程序运行没问题,提示 unable to invoke code completion due to errors in source code ?怎么解决
- 头痛头痛,都快疯了,高手救我
- FastReport字体设置问题,,,,你遇到过吗?
- 请问一下知不知何在delphi中用ADO导入导出ACCESS数据库(我想做数据库备份及数据还原功能)
- 关于2000与98的兼容问题?
- 怎么按日期查询统计数?
- 单机数据库程序如何最简便地转变为网络数据库程序
- 如何让我的应用程序截获关闭计算机,注销,重新启动计算机的消息?
- 星际争霸之对王之王
- 前台系统和后台系统是怎么区分的?
- 兄弟我改行了,给点建议!
- 请教关于Delphi的Service Application
而且你的程序逻辑好像有问题!不清楚你要什么效果:)