如果你要把发送的数据定义在一个结构中,就应该把字符串定义为字符数组,这样才可以
通过SizeOf来获取结构的真实大小。
(我本不想把代码贴出来污染版面的……)
//服务器端代码:需要在窗体上放一个TServerSocket,两个TButton,一个TMemo
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
ServerSocket1: TServerSocket;
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
const
dim = 2048;
var
ar: array[1..dim] of Integer;
i,sz,rs: Integer;
begin
sz := SizeOf(ar);
rs := Socket.ReceiveBuf(ar, sz);
if (rs>0) then
begin
for i:=0 to dim do
Memo1.Lines.Add(IntToStr(ar));
end
else
ShowMessage('接收失败!');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Open;
Caption := 'Socket服务器启动';
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ServerSocket1.Close;
Caption := 'Socket服务器关闭';
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('接收到来自'+Socket.RemoteAddress+'的客户端连接');
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('客户端'+Socket.RemoteAddress+'断开连接');
end;
end.
//客户端代码,需要在窗体上放一个TServerSocket,两个TButton,一个TMemo
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
ClientSocket1: TClientSocket;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ClientSocket1.Open;
end;
procedure TForm1.Button2Click(Sender: TObject);
const
dim = 2048;
var
ar: array[1..dim] of Integer;
i,rs,sz: Integer;
begin
for i:=1 to dim do
ar := i;
sz := SizeOf(ar);
rs := ClientSocket1.Socket.SendBuf(ar, sz);
if (rs>=sz) then
begin
ShowMessage('发送成功!');
for i:=1 to dim do
Memo1.Lines.Add(IntToStr(ar));
end
else
ShowMessage('发送失败!');
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ClientSocket1.Close;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('连接到服务器:'+Socket.RemoteAddress);
end;
procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('从服务器'+Socket.RemoteAddress+'断开连接');
end;
end.