TclientSocket問題?(100分)

  • 主题发起人 主题发起人 zzitwang
  • 开始时间 开始时间
Z

zzitwang

Unregistered / Unconfirmed
GUEST, unregistred user!
現在我想用TclientSocket給一服務器發送些數據,數據的多少不固定,而且我要接收服務器反回的信息!
var
csocket:TclientSocket;
data:string;

procedure TForm1.Button2Click(Sender: TObject);
var
log:integer;
i:integer;
begin
log:=length(memo1.lines.count);
for i:=0 to log-1 do
begin
data:= memo1.Lines.Strings;//發送的數據
csocket.Host:=hosturl;
csocket.Port :=hostport;
csocket.Open;
{這裡怎麼寫等待的代碼?
如果寫成
while true do
application.ProcessMessages();
那麼TClientSocket只發送一條數據就不再發了
為什麼?}
end;
end;

procedure TForm1.csocketConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
socket.SendText(data);
end;

procedure TForm1.csocketError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
errorcode:=0;socket.Close;
end;

procedure TForm1.csocketRead(Sender: TObject; Socket: TCustomWinSocket);
begin
memo2.Text:=Memo2.text+socket.ReceiveText;
end;
 
正确的做法是先尝试连接,如果连接成功,那么一条一条的发送数据;而不是每条数据连接一次吧?
 
C:/Program Files/Borland/Delphi5/Demos/Internet/Chat
 
我写过这样的程序,可以qq上联系我13773996
 
to jiajiajia888
不好意思,我不方便上QQ,可以在此做答嗎?
 
鉴于你的水平,建议你别用这个了, 估计连异步模式概念都不知道呢,还是用indy控件吧, 简单的阻塞模式通信,一问一答, 最省事了.
 
对,用indy吧
我一直用来着
 
unit Chatmain;

interface

uses
Classes, QControls, QStdCtrls, QExtCtrls, QButtons, QForms, Sockets, SysUtils;
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);
private
{ Private declarations }
public
{ Public declarations }
end;

TClientDataThread = class(TThread)
private
public
ListBuffer :TStringList;
TargetList :TStrings;
procedure synchAddDataToControl;
constructor Create(CreateSuspended: Boolean);
procedure Execute; override;
procedure Terminate;
end;

var
Form1: TForm1;

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;
StrTmp : string;
begin
TcpClient1.RemoteHost := edtRemoteHost.Text;
TcpClient1.RemotePort := edtRemotePort.Text;
StrTmp := '';
try
if TcpClient1.Connect then
for I := 0 to memSend.Lines.Count - 1 do
StrTmp := StrTmp + '/n/r' + memSend.Lines;

TcpClient1.Sendln(StrTmp);
finally
TcpClient1.Disconnect;
end;
end;

procedure TForm1.TcpServer1Accept(sender: TObject;
ClientSocket: TCustomIpClient);
var
s: string;
DataThread: TClientDataThread;
SaveStr : String;
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;
s :=StringReplace(s,'/n/r',#10+#13,[rfReplaceAll]);
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;
end.
D7中没有clientSocket 而是TcpClient。
 
不要用DELPHI带的控件clientSocket和ServerSocket。有很多BUG
这么简单的原理可以直接使用API编写。
 
clientSocket和ServerSocket控件以及indy控件都好用的很,而且很方便。所谓的bug都是自己处理逻辑上的问题。
 
呵呵,自己处理逻辑上的问题。楼上的太相信控件了!
你在坛子上找找关于这方面的讨论很多!
 
求助!!!!
我用delphi 的ServerSocket做了一个服务端监控软件,但是我每双击一下主界面或是打开一个form后关闭他,客户端就会断开,怎么办?
 
呵呵,楼上的楼上,是啊,Borland公司里的人都是白痴,Windows API也都是BUG,Intel的CPU也到处是Bug,还是你自己拿石英砂捏一个CPU,再写一个OS玩吧!
 
看你的要求并没有难度,很容易做。
qq:13773996。可以在qq上支持你
 
我参考delphi的chat,用socketServer和SocketClient作了一个交谈,文件传送的功能,
用得不错,传送900多M的文件也没有错误!楼主也试一试
 
unit U_Client;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, IdBaseComponent, IdComponent, IdUDPBase,
IdUDPClient, ExtCtrls;

type
TForm1 = class(TForm)
ClientSocket: TClientSocket;
Button1: TButton;
MemReadMsg: TMemo;
IdUDPClient1: TIdUDPClient;
btnSend: TButton;
MemSendMsg: TMemo;
Label2: TLabel;
Label1: TLabel;
Panel1: TPanel;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
procedure btnSendClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Try
ClientSocket.Active := true;
Except
MessageBox(0,'连接失败,请查看服务端是否有启动!','',MB_OK + MB_ICONWARNING);
end;
end;

procedure TForm1.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
var
len:word;
buf:array[1..14] of char;
msg:string;
begin
{try
Socket.ReceiveBuf(len, 2);
if len < 1 then Exit;
setlength(buf, len);
Socket.ReceiveBuf(buf[0], len);
//msg:=copy(buf, 0, len);
setlength(msg, len);
Move(buf[0], msg[1], len);
MemReadMsg.Lines.Add(msg);
except
end; }
try
fillchar(buf,sizeof(buf),' ');
Socket.ReceiveBuf(buf,14);
msg:=strpas(@buf);
len:=length(trim(msg));
if len>0 then
MemReadMsg.Lines.Add(msg);
except
end;
end;

{procedure TForm1.Button2Click(Sender: TObject);
var
tmp:string;
begin
tmp :=Trim(Memo2.Text);
try
IdUDPClient1.Active := true;
IdUDPClient1.Send(tmp);
except
end;
end;}

procedure TForm1.btnSendClick(Sender: TObject);
Var
len : Word;
Msg : String;
buf : array of char;
begin //前两个字节存放消息体内容长度
{Msg := Trim(MemSendMsg.Text);
Len := Length(Msg);
SetLength(buf,Len + 2);
Move(Len,buf[0],2);
Move(Msg[1],buf[2],Len);
ClientSocket.Socket.SendBuf(buf[0],Len + 2) }
msg:=MemSendMsg.Text;
len:=length(msg);
setlength(buf,len); //设置buf长度 //前两个字节存放消息体内容长度
//Move(len, buf[0], 2) ; //把消息体内容长度放入前两个字节
Move(msg[1], buf[0], len); //把消息体内容放入包中 从第三个字节开始存放
try
ClientSocket.Socket.SendBuf(buf[0], len);
except
ClientSocket.Socket.Close;

end;
end;
{begin
ClientSocket.Socket.SendText(Trim(MemSendMsg.Text))
end;}

end.
 
为什么我不可以提问?说我帐号没确认,这是怎么回事啊?请知道的朋友告诉我,小弟不胜感激。
小弟想提问:高手请指点
界面:Form1,CheckBox1
现在我想在按下Form1的关闭按钮“X”时触发CheckBox1的事件如何设置?
我现在在CheckBox1的OnExit事件里面设置,在其他控件可以触发,唯独以上的关闭
按钮不能触发,我该如何处理?
 
帮顶!

╭=========================================╮

80G海量源代码,控件,书籍全免费狂下不停!

http://www.source520.com

个人网站站长开发推广同盟,让所有人知道你的世界!

http://www.source520.com/search/search.asp

╰=========================================╯
 
后退
顶部