unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
Edit1: TEdit;
Button1: TButton;
Edit2: TEdit;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Button3: TButton;
procedure Button2Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button3Click(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject);
begin
ClientSocket1.Open;//建立连接
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
showmessage('ok');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Clientsocket1.Socket.SendText(edit2.text); //客户机发送数据
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
s:string;
begin
s:=socket.ReceiveText;
showmessage(s);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Socket.Connections[0].SendText(edit1.text);//服务器发送数据给客户
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
s:string;
begin
s:=socket.ReceiveText;
showmessage(s);//显示收到信息
end;
end.