5分钟写的程序,可别见笑:delphi 5 下编译通过,windows 2000下运行无问题。
客户端:
unit UClient;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, StdCtrls, ScktComp;
type
TForm1 = class(TForm)
ClientSocket: TClientSocket;
sendtxt: TEdit;
Label1: TLabel;
sendButton: TButton;
MainMenu1: TMainMenu;
clientmenu: TMenuItem;
connect1: TMenuItem;
disconnect1: TMenuItem;
exit1: TMenuItem;
procedure exit1Click(Sender: TObject);
procedure connect1Click(Sender: TObject);
procedure ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure sendButtonClick(Sender: TObject);
procedure disconnect1Click(Sender: TObject);
procedure ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.exit1Click(Sender: TObject);
begin
Application.Terminate;
end;
procedure TForm1.connect1Click(Sender: TObject);
begin
if ClientSocket.Active then
ClientSocket.Close;
ClientSocket.Address:='204.111.111.53';
ClientSocket.Port:=5678;
ClientSocket.Open;
end;
procedure TForm1.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Label1.Caption:='connect to :'+Socket.RemoteAddress;
end;
procedure TForm1.sendButtonClick(Sender: TObject);
var
sendstr:string;
begin
if not ClientSocket.Active then
ShowMessage('no connect');
sendstr:=sendtxt.Text;
ClientSocket.Socket.SendText(sendstr);
end;
procedure TForm1.disconnect1Click(Sender: TObject);
begin
if ClientSocket.Active then
ClientSocket.Close;
end;
procedure TForm1.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Label1.Caption:='disconnect to :'+Socket.RemoteAddress;
end;
end.
------------------------------------------------
服务端:
unit UServer;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, ScktComp, StdCtrls;
type
TForm2 = class(TForm)
MainMenu1: TMainMenu;
ServerSocket: TServerSocket;
system1: TMenuItem;
start1: TMenuItem;
stop1: TMenuItem;
exit1: TMenuItem;
Label1: TLabel;
procedure exit1Click(Sender: TObject);
procedure start1Click(Sender: TObject);
procedure ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
procedure TForm2.exit1Click(Sender: TObject);
begin
if ServerSocket.Active then
ServerSocket.Close;
Application.Terminate;
end;
procedure TForm2.start1Click(Sender: TObject);
begin
if ServerSocket.Active then
ServerSocket.Close;
ServerSocket.Port:=5678;
ServerSocket.Open;
end;
procedure TForm2.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
strarray:array[0..4] of string;
icount :integer;
begin
icount:=0;
strarray[icount]:=Socket.ReceiveText;
Label1.Caption:=strarray[icount];
end;
end.