tcp/ip组件, 为什么 可以发送,却接受不到?(200分)

  • 主题发起人 主题发起人 dcom229
  • 开始时间 开始时间
D

dcom229

Unregistered / Unconfirmed
GUEST, unregistred user!
client:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Sockets, StdCtrls;

type
TForm1 = class(TForm)
GroupBox2: TGroupBox;
Edit2: TEdit;
Edit3: TEdit;
connect: TButton;
Edit4: TEdit;
senddata: TButton;
TcpClient1: TTcpClient;
procedure FormCreate(Sender: TObject);
procedure connectClick(Sender: TObject);
procedure senddataClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
TCPClient1.Protocol:=6;// tcp
end;



procedure TForm1.connectClick(Sender: TObject);
//var
// bufout: string;
begin
// bufout:=edit4.text;
TCPClient1.LocalPort:='0';
tcpClient1.RemoteHost:=edit2.Text;
tcpclient1.RemotePort:=edit3.text;
if not tcpclient1.Connect then
inputbox('OK','ok','CANT CONNECT');
{
else
begin
inputbox('bufout','bufout', inttostr(tcpclient1.SendBuf(bufout,length(bufout),0)));
IF inttostr(tcpclient1.SendBuf(bufout,length(bufout),0))='-1' THEN
exit;
end;
}
end;

procedure TForm1.senddataClick(Sender: TObject);
var
buf: string;
begin
//test
buf:=edit4.Text;
//if not tcpclient1.Active then
// tcpclient1.Active:=true;
if tcpclient1.Connect then
begin
tcpclient1.Close;
end;
if not tcpclient1.Connect then
showmessage('fail connect to server');
inputbox(inttostr(tcpclient1.SendBuf(buf,length(buf),0)),'out','out');
inputbox('buf','',buf);
inputbox('remotehost','',tcpclient1.LookupHostAddr(edit2.Text));

// if tcpserver1.Active then
// inputbox('out','out',bufout);
end;
end.

server:

unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Sockets, StdCtrls;

type
TForm2 = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
Edit1: TEdit;
listen: TButton;
ListBox1: TListBox;
TcpServer1: TTcpServer;
procedure FormCreate(Sender: TObject);
procedure listenClick(Sender: TObject);
procedure TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
procedure TcpServer1Listening(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
TCPserver1.Protocol:=6;// tcp
end;

procedure TForm2.listenClick(Sender: TObject);
begin
TCPserver1.Protocol:=6;// tcp
tcpserver1.LocalHost:='linagxiang';
tcpserver1.LocalPort:=(edit1.Text);
tcpserver1.Open ;
if not tcpserver1.Listening then
exit;
end;

procedure TForm2.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
bufin: string;
icount: integer;
buf00: variant;
begin
icount:=0;
sleep(1000);
//icount:=integer(tcpserver1.BytesReceived);
inputbox(inttostr(tcpserver1.ReceiveBuf(buf00,icount,0)),'bufIN','bufIN');
if inttostr(tcpserver1.ReceiveBuf(buf00,icount,0))='-1' then
exit;
inputbox('bufin','bufin',buf00);

end;


procedure TForm2.TcpServer1Listening(Sender: TObject);
var
bufin2: string ;
icount: integer;
begin
icount:=integer(tcpserver1.BytesReceived);
inputbox(inttostr(tcpserver1.ReceiveBuf(bufin2,icount,0)),'IN','IN');
listbox1.Items.Add(bufin2);
end;

end.
 
所有的tcpserver1.ReceiveBuf改成ClientSocket.ReceiveBuf
 
问题是
我现在是做 client 与 server 的通信,不是只为了达到能接收的问题
mywyn,继续,各位加油阿,我等待
 
不明白你到底想要什么。
 
端口,端口,要正确啊!
 
感觉TTcpServer、TTcpClient控件不如TClientSocket、TServerSocket控件好用,也许是我不会用吧。

TClientSocket、TServerSocket控件也是符合C/S规范的,通过采用阻塞式通信方式(ctNonBlocking),你可以在它们相应的事件(OnRead、OnClientRead)中接收数据。最后要说的是,这套组件好像是Delphi6带的,5.0可能没有。
 
端口, 为 5153, 客户和服务器都设置5153,
还是不行
 
可能你的设置有问题,你最好把他带DEMO好好看看
 
i will look it carefully with you
 
i have seen the demo ,
now the question dispear
thanks. :)
 
后退
顶部