F
firstspring
Unregistered / Unconfirmed
GUEST, unregistred user!
现在有一个Unix应用程序它进行数据的接收和发送(Sco Unix),有一个Delphi编的Socket程序与其通信(Windows98),(98->Sco)发送成功但接收时如果用ClientSocket控件的方法来接收只能接收字符数和发送字符数相同,后来发现是用了非阻塞方式,后来用阻塞方式进行读定义了一个TCustomWinSocket进行读操作但总会死锁,请各位大虾帮忙。以下一段代码是我试的一个通信例子但你发了五个字符,发送完毕后,服务程序返回九个字符,如果一次读1个字符读了五次成功到读第六次发生死锁。
unit Socket;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
ClientSocket: TClientSocket;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
Var
wsStream:TWinSocketStream;
asBuf:Array of Char;
i:integer;
ss:String;
begin
ClientSocket.Active:=True;
ClientSocket.Socket.SendText(edit1.text);
SetLength(asBuf,);
while clientSocket.Socket.Connected do
begin
wsStream:=TWinSocketStream.Create(ClientSocket.Socket,1000);
Fillchar(asBuf,4,0);
if wsStream.WaitForData(1000) then
begin
if wsStream.Read(asBuf,1)=0 then
ClientSocket.Close;
end
else
ClientSocket.Close;
wsStream.Free;
end;
for i:=0 to length(asbuf)-1 do
ss:=ss+asBuf;
edit2.text:=ss;
ClientSocket.Active:=False;
end;
end.
希望大虾能举出实例!
unit Socket;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
ClientSocket: TClientSocket;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
Var
wsStream:TWinSocketStream;
asBuf:Array of Char;
i:integer;
ss:String;
begin
ClientSocket.Active:=True;
ClientSocket.Socket.SendText(edit1.text);
SetLength(asBuf,);
while clientSocket.Socket.Connected do
begin
wsStream:=TWinSocketStream.Create(ClientSocket.Socket,1000);
Fillchar(asBuf,4,0);
if wsStream.WaitForData(1000) then
begin
if wsStream.Read(asBuf,1)=0 then
ClientSocket.Close;
end
else
ClientSocket.Close;
wsStream.Free;
end;
for i:=0 to length(asbuf)-1 do
ss:=ss+asBuf;
edit2.text:=ss;
ClientSocket.Active:=False;
end;
end.
希望大虾能举出实例!