这是我自己编的一个试验程序,用sendbuf 传一个记录ChessPoint ,
If IsServer then
serversocket.Socket.Connections[0].SendBuf(ChessPoint,sizeof(chesspoint))
else
clientsocket.Socket.SendBuf(ChessPoint,sizeof(chesspoint));
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
在win98+delphi5 下通过。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;
type
TInfo = record
x : integer ;
y : integer ;
clr : integer ;
end;
TForm1 = class(TForm)
clientsocket: TClientSocket;
serversocket: TServerSocket;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Edit3: TEdit;
Edit4: TEdit;
Button3: TButton;
Label1: TLabel;
Edit5: TEdit;
Edit6: TEdit;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure clientsocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure serversocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
procedure serversocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure serversocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure clientsocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure clientsocketRead(Sender: TObject; Socket: TCustomWinSocket);
procedure serversocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ChessPoint : TInfo ;
IsServer : Boolean;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
IsServer := False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
clientsocket.Active := False;
serversocket.Active := True;
end;
procedure TForm1.Button2Click(Sender: TObject);
var server : string ;
begin
If serversocket.Active then serversocket.Active := False;
if clientsocket.active then clientsocket.active := false;
If InputQuery('Connect To','connect to',server) then
with clientsocket do
begin
host := server ;
active := True;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
chesspoint.x := strtoint(edit1.text);
chesspoint.y := strtoint(edit2.text);
chesspoint.clr := strtoint(edit3.text);
If IsServer then
serversocket.Socket.Connections[0].SendBuf(ChessPoint,sizeof(chesspoint))
else
clientsocket.Socket.SendBuf(ChessPoint,sizeof(chesspoint));
end;
procedure TForm1.clientsocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
label1.Caption := 'success to connect to server ';
end;
procedure TForm1.serversocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
IsServer := True;
end;
procedure TForm1.serversocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
label1.Caption :='success to connect to client ';
end;
procedure TForm1.serversocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
label1.Caption := 'disconnect to client';
end;
procedure TForm1.clientsocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
label1.Caption :='disconnect to server ';
end;
procedure TForm1.clientsocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
clientsocket.Socket.ReceiveBuf(chesspoint,12);
edit4.Text := inttostr( chesspoint.x );
edit5.text := inttostr ( chesspoint.y);
edit6.text := inttostr ( chesspoint.clr);
end;
procedure TForm1.serversocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
serversocket.Socket.Connections[0].ReceiveBuf(chesspoint,12);
edit4.Text := inttostr( chesspoint.x );
edit5.text := inttostr ( chesspoint.y);
edit6.text := inttostr ( chesspoint.clr);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
serversocket.Close ;
clientsocket.Close ;
end;
end.