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.
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.