下面是我关于IdTCPClient组件的代码procedure TForm1.BitBtn1Click(Sender: TObject);
var
np: String; //记录用户名和密码的字符串
just: Integer; //判断用户名和密码返回结果是否正确
begin
if Edit1.Text='' then
Showmessage('用户名不能为空!')
else if Edit2.Text='' then
Showmessage('密码不能为空!')
else
begin
IdTCPClient1.Create(nil);
try
if IdTCPClient1.connected then IdTCPClient1.Disconnect;
IdTCPClient1.Host:='192.168.0.28';
IdTCPClient1.Port:=8199;
IdTCPClient1.connect;
except on e:exception do
begin
messagedlg('链接错误:'+#13+e.Message,mterror,[mbok],0);
IdTCPClient1.Destroy;
end;
end; np:=('1 '+Edit1.Text+' '+Edit2.Text);
IdTCPClient1.SendCmd(np);现在的问题是我不知道怎么获取服务器返回的信息!比如服务器返回一个字符串‘100’;
请问我该怎么获取?
补充:我的服务器端是用JAVA写的
var
np: String; //记录用户名和密码的字符串
just: Integer; //判断用户名和密码返回结果是否正确
begin
if Edit1.Text='' then
Showmessage('用户名不能为空!')
else if Edit2.Text='' then
Showmessage('密码不能为空!')
else
begin
IdTCPClient1.Create(nil);
try
if IdTCPClient1.connected then IdTCPClient1.Disconnect;
IdTCPClient1.Host:='192.168.0.28';
IdTCPClient1.Port:=8199;
IdTCPClient1.connect;
except on e:exception do
begin
messagedlg('链接错误:'+#13+e.Message,mterror,[mbok],0);
IdTCPClient1.Destroy;
end;
end; np:=('1 '+Edit1.Text+' '+Edit2.Text);
IdTCPClient1.SendCmd(np);现在的问题是我不知道怎么获取服务器返回的信息!比如服务器返回一个字符串‘100’;
请问我该怎么获取?
补充:我的服务器端是用JAVA写的
var
AHeader: TConnectorMessageHeader;
sMsgText: string;
i: Integer;
begin
while not Terminated do
begin
while (FTCPClient <> nil) and (not FTCPClient.ClosedGracefully) and FTCPClient.Connected do
try
//读信息头
FTCPClient.ReadTimeout := READ_TIMEOUT;
FTCPClient.ReadBuffer(AHeader, SizeOf(TConnectorMessageHeader)); if IsValidMessage(AHeader) then
begin
FTCPClient.ReadTimeout := 0;
//读消息本体
sMsgText := FTCPClient.ReadString(AHeader.MessageLength);
//进行反序列化
try
FReceivedMsg := ToMessageEntity(sMsgText);
except
TLogger.Debug('消息反序列化失败: %s', [sMsgText], LOG_CALLER);
raise;
end; try
if Assigned(FOnReceiveMsg) then
Synchronize(DoNotifyReceiveMsg); FreeAndNil(FReceivedMsg);
except
FreeAndNil(FReceivedMsg);
raise;
end;
end
else
TLogger.Error('非法的消息头: %s', [AHeader.EncryptedHeaderKey], LOG_CALLER);
except
on E: Exception do
if not (E is EIdReadTimeout) then
begin
TLogger.Error('消息接收发生错误: %s', [e.Message], LOG_CALLER);
raise; //屏蔽了EIdReadTimeOut,设置TimeOut只是为了让线程能结束
end;
end; if FTCPClient.ClosedGracefully then
FTCPClient.Disconnect;
//sleep
for i := 1 to 10 do
if not Terminated then Sleep(100);
end;
end;
有ReadBuffer,ReadChar,ReadLn等
请问这是什么类型啊?
请问怎么屏蔽啊?
--------------------
tools->debugger options->language exceptions->添加忽略的错误
还有那个去Read用什么比较好啊,服务器返回的可能是一个Integer或String
--------------------
一般通讯都有个消息头,你就根据消息头的约定再具体调用就好了。
package Test;import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;public class ThreadEchoHandler implements Runnable {
Socket socket;
byte[] inbyte=new byte[1024];
String incommands[]=new String[20];
public ThreadEchoHandler(Socket s) {
// TODO Auto-generated constructor stub
this.socket=s;
}
public void run() {
// TODO Auto-generated method stub
InputStream in=null;
OutputStream out=null;
try{
if(socket!=null&&!socket.isClosed()){
in=socket.getInputStream();
out=socket.getOutputStream();
}
boolean done=false;
System.out.println("线程启动了");
while(socket!=null&&!socket.isClosed()){
if(-1!=in.read(inbyte)){
String input=new String(inbyte,"UTF-8");
System.out.println("输进来的是:"+input);
incommands=input.split(" ");
String toclient=dosomething(incommands[0], incommands[1], incommands[2]);
if(!toclient.equals("3 ")){
System.out.println("处理结果是:"+toclient);
out.flush();
out.write(toclient.getBytes("UTF-8"));
out.flush();
}else{
System.out.println("关闭中......");
out.close();
in.close();
socket.close();
}
System.out.println("已发送出处理结果........");
}
done=socket.isClosed();
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public String dosomething(String type,String name,String pass){
System.out.println("参数传的是:"+type+name+pass);
if(type.equals("1")&&name.equals("123")&&pass.equals("123")){
System.out.println("ok");
return "1 ";
}
else if(type.equals("2")){
return "这辆车暂时没有欠费行为!";
}
// else if(type.trim().equals("000"))
// return "3 ";
else return "0 ";
}
}
上面是我服务器端的源代码部分,我是新手,关于客户端接受信息的方法或操作有没有上面简便的方法