关于ClientSocket1 采用阻塞方式接收不到对方返回包的问题 高分送

guoyunzhi 2007-12-16 11:21:05
procedure TFm_main.Button1Click(Sender: TObject);
var
return_text:string;
begin
label5.Caption:='交易包字串总长度:'+inttostr(length(trim(Edit3.Text)));
try
I:=0;
clientsocket1.Close;
clientSocket1.Address:=edit1.Text;
clientSocket1.Port:=strtoint(edit2.text);
clientsocket1.ClientType:=ctBlocking;
clientSocket1.Open;
while fm_main.ClientSocket1.Active do fm_main.ClientSocket1.Socket.SendText(edit3.text);
Fm_main.Memo1.Lines.Add('转格式:'+edit3.text);
while fm_main.ClientSocket1.Socket.ReceiveLength = 0 do
Application.ProcessMessages;
return_text:=fm_main.ClientSocket1.Socket.ReceiveText;
fm_main.Memo1.Lines.Add('1'+return_text);
fm_main.ClientSocket1.Close;

except
showmessage(edit1.text+#13#10+'未开机或未安装服务程序');
end;
end;


对方服务是linux 下标C写的。我采用ctBlocking死活接不到对方的的返回包。有没有这方面的源代码啊。跪求。高分送
...全文
111 点赞 收藏 14
写回复
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
meiZiNick 2008-05-01
不明LZ在说什么
回复
tigerii 2008-02-13
Don't use it in main thread if you use blocking mode.

Create thread to handle send/receive event while you use blocking mode. You can refer to Indy source code for how to use blocking mode.

回复
hello_world888 2008-02-04
delphi的网络控件不是很可靠,最好用api吧。
回复
僵哥 2008-02-04
delphi的网络控件不是很可靠,最好用api吧。
====================================
不可靠是因为不会使用。要能够用好API的人,同样也能够用好这些网络控件。
回复
zhouchunyu 2008-01-18
帮顶
回复
编码人11 2007-12-28
直接用API
回复
sy_315 2007-12-27
我用delphi 做的socket 发送消息,unix用c写的接收server.
下面是client的:

procedure TForm1.Button1Click(Sender: TObject);
const blocklen = 1024*8;
var Fby : file of byte;
blnum,relen : integer;
blbuf : array[0..blocklen] of char;
i : integer;
sl : integer;
client : tsocket;
ca : SOCKADDR_IN;
Haddr : u_long;
msg : string;
wsstatus: integer; //winsocket_status;
rece: TWSAData; //type:WSAData;
ver : integer;
blen : integer;
begin
ver:= 10101010;
wsstatus := WSAStartup(ver, rece);
client := Socket(AF_INET,SOCK_STREAM,0);

if (client = INVALID_SOCKET) then
begin
showmessage('socket create error!');
exit;
end;

msg := '00010';
msg := msg + 'A'; //A or B
msg := msg + '11111222223333344444'; //20位
msg := msg + memo1.Text;

msg :=fillleft(inttostr(length(msg)),'0',5)+copy(msg,6,length(msg));

//showmessage(msg);

ca.sa_family := PF_INET;

ca.sin_port := htons(strtoint(edit2.Text));
Haddr := inet_addr(pchar(trim(edit1.Text)));

if ( Haddr = -1 ) then
begin
showmessage('ip is error');
closesocket(client);
exit;
end
else
begin
ca.sin_addr.S_addr:= Haddr;
end;

if (connect(Client,ca,sizeof(ca)) <> 0) then
begin
showmessage('连接服务器失败');
exit;
end;

{
//msg 转换成 blbuf
//搞定,必须是msg的第1位开始,第0位应该是'0/'
for i :=0 to length(msg)-1 do
begin
blbuf[i]:=msg[i+1];
end; }

//msg:=dostounix(msg);
strcopy(blbuf,pchar(msg));

//blbuf := pchar(msg);
blen :=length(msg);
sl := send(client,blbuf,blen,0);

if (sl <= 0) then
begin
closesocket(client);
exit;
end;


end;
回复
楼申中 2007-12-27
UP
回复
编码人11 2007-12-19
用indy ,我最近就做了一个这样的 对方也是 linux 下的c写的 嵌入
回复
guoyunzhi 2007-12-16
帮忙,有没有阻塞模式的例子啊。
回复
edongxu 2007-12-16
procedure   TFm_main.Button1Click(Sender:   TObject); 
var
return_text:string;
begin
label5.Caption:='交易包字串总长度:'+inttostr(length(trim(Edit3.Text)));
try
I:=0;
clientsocket1.Close;
clientSocket1.Address:=edit1.Text;
clientSocket1.Port:=strtoint(edit2.text);
clientsocket1.ClientType:=ctBlocking;
clientSocket1.Open;
while fm_main.ClientSocket1.Active do
begin
fm_main.ClientSocket1.Socket.SendText(edit3.text);
Fm_main.Memo1.Lines.Add('转格式:'+edit3.text);
while fm_main.ClientSocket1.Socket.ReceiveLength = 0 do
Application.ProcessMessages;
return_text:=fm_main.ClientSocket1.Socket.ReceiveText;
fm_main.Memo1.Lines.Add('1'+return_text);
end;
fm_main.ClientSocket1.Close;

except
showmessage(edit1.text+#13#10+'未开机或未安装服务程序');
end;
end;
回复
guoyunzhi 2007-12-16
网络 上讲要用TWinSocketStream,有没有这方面的例子啊。
回复
guoyunzhi 2007-12-16
我把while fm_main.ClientSocket1.Active do 去掉了。还是不行啊。
回复
edongxu 2007-12-16
你这句是死循环啊,一直到链接断开
while fm_main.ClientSocket1.Active do fm_main.ClientSocket1.Socket.SendText(edit3.text);
回复
发动态
发帖子
网络通信/分布式开发
创建于2007-08-02

1565

社区成员

Delphi 网络通信/分布式开发
申请成为版主
社区公告
暂无公告