关于tcp如何停止发送的问题? ( 积分: 30 )

  • 主题发起人 主题发起人 鬼屋的程序员
  • 开始时间 开始时间

鬼屋的程序员

Unregistered / Unconfirmed
GUEST, unregistred user!
您好.我现在在用了TTcpServer和TTcpClient的控件来进行传输..我现在需要的是从服务端传输几乎一万个文件名到客户端显示..也就是接收一条显示一条....但是有一个问题了.我客户端在没有接收完所有的文件名的时候便退出了程序..然后程序再次打开的时候.他又接着上次没有接收完的文件名继续接收了.现在我想问的是.我应该在客户端退出的时候怎么样让服务端停止发送呢..或者是让客户端怎么样停止接收呢??

小弟的分是有些少...只有30分...不好意思了.希望你们能够帮帮我.在此谢谢先了.
 
您好.我现在在用了TTcpServer和TTcpClient的控件来进行传输..我现在需要的是从服务端传输几乎一万个文件名到客户端显示..也就是接收一条显示一条....但是有一个问题了.我客户端在没有接收完所有的文件名的时候便退出了程序..然后程序再次打开的时候.他又接着上次没有接收完的文件名继续接收了.现在我想问的是.我应该在客户端退出的时候怎么样让服务端停止发送呢..或者是让客户端怎么样停止接收呢??

小弟的分是有些少...只有30分...不好意思了.希望你们能够帮帮我.在此谢谢先了.
 
tcp协议一方主动断开时会有消息发送到另一方,另一方应该有一个连接断开的事件产生。
看看你的客户端退出时是否没有进行关闭TCP连接的操作。
 
我客户端退出的时候已经关闭了连接的...只是当我运行程序的时候又是直接打开了连接..是这样的..
 
还有.我再问一下一个问题. 在delphi7 internet面板上的tcpclient/tcpserver两控件进行通讯,tcpserver怎样返回信息给tcpclient,tcpclient怎样接收,对应什么事件.我就是用tcpclient接收不了.让我逼不得两边都用上了tcpserver.让得我两边都好像是服务端这样...高手帮帮忙吧
 
晕,我曾经遇到一个程序员如你所说写的代码,重要的不是怎么写,而是实现功能。
 
//netchat代码
unit Chatmain;

interface

uses
Classes, QControls, QStdCtrls, QExtCtrls, QButtons, QForms, Sockets;


type
TForm1 = class(TForm)
memRecv: TMemo;
Panel1: TPanel;
memSend: TMemo;
Panel2: TPanel;
btnSend: TButton;
Panel3: TPanel;
Label1: TLabel;
edtRemoteHost: TEdit;
Label2: TLabel;
edtRemotePort: TEdit;
Label3: TLabel;
edtLocalPort: TEdit;
btnActivateServer: TButton;
TcpClient1: TTcpClient;
TcpServer1: TTcpServer;
procedure btnSendClick(Sender: TObject);
procedure TcpServer1Accept(sender: TObject;
ClientSocket: TCustomIpClient);
procedure btnActivateServerClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

// you must create your own thread to synch
// writing to a gui component
TClientDataThread = class(TThread)
private
public
ListBuffer :TStringList;
TargetList :TStrings;
procedure synchAddDataToControl;
constructor Create(CreateSuspended: Boolean);
procedure Execute; override;
procedure Terminate;
end;

var
Form1: TForm1;
//DataThread: TClientDataThread;

implementation

{$R *.xfm}

//------------- TClientDataThread impl -----------------------------------------
constructor TClientDataThread.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := true;
ListBuffer := TStringList.Create;
end;

procedure TClientDataThread.Terminate;
begin
ListBuffer.Free;
inherited;
end;

procedure TClientDataThread.Execute;
begin
Synchronize(synchAddDataToControl);
end;

procedure TClientDataThread.synchAddDataToControl;
begin
TargetList.AddStrings(ListBuffer);
end;
//------------- end TClientDataThread impl -------------------------------------


procedure TForm1.btnActivateServerClick(Sender: TObject);
begin
TcpServer1.LocalPort := edtLocalPort.Text;
TcpServer1.Active := True;
end;

procedure TForm1.btnSendClick(Sender: TObject);
var
I: Integer;
begin
TcpClient1.RemoteHost := edtRemoteHost.Text;
TcpClient1.RemotePort := edtRemotePort.Text;
try
if TcpClient1.Connect then
for I := 0 to memSend.Lines.Count - 1 do
TcpClient1.Sendln(memSend.Lines);
finally
TcpClient1.Disconnect;
end;
end;

procedure TForm1.TcpServer1Accept(sender: TObject;
ClientSocket: TCustomIpClient);
var
s: string;
DataThread: TClientDataThread;
begin
// create thread
DataThread:= TClientDataThread.Create(true);
// set the TagetList to the gui list that you
// with to synch with.
DataThread.TargetList := memRecv.lines;

// Load the Threads ListBuffer
DataThread.ListBuffer.Add('*** Connection Accepted ***');
DataThread.ListBuffer.Add('Remote Host: ' + ClientSocket.LookupHostName(ClientSocket.RemoteHost) +
' (' + ClientSocket.RemoteHost + ')');
DataThread.ListBuffer.Add('===== Begin message =====');
s := ClientSocket.Receiveln;
while s <> '' do
begin
DataThread.ListBuffer.Add(s);
s := ClientSocket.Receiveln;
end;
DataThread.ListBuffer.Add('===== End of message =====');

// Call Resume which will execute and synch the
// ListBuffer with the TargetList
DataThread.Resume;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
TcpClient1.Disconnect;
end;

end.
 
谢谢maxim88
 
后退
顶部