Brug TCP/IP. Det er nok det smarteste. TCP/IP er efterhånden standarden
indenfor alt netværk. Men der selvfølgelig også IPX, men nok om det.
Jeg har engang lavet et program der snakkede sammen med klienter og server
gennem filer på disken. Dette krævede at jeg havde et delt dir, som alle
kunne få adgang til. Den først instans der startes op, fungerede som server.
De dertil startede instancer kommer op som klienter. Programmet virker
perfekt. Og man kunne sende beskeder til alle, eller individuelle
instancer.. sjovt at lave, men ikke en elegant løsning!!
Check Hori's DirectX extensions, og komponenter. Det er meget nemt at bruge:
http://www.yks.ne.jp/~hori/index-e.html
Her er koden til en Chat, som er et lille sample program til ovenstående
komponenter.
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DXPlay, DirectX, Gauges,winbasic;
const
MaxActiveFiles = 100;
DXCHAT_MESSAGE = 0;
DXDATA_MESSAGE = 1;
DXREQ_SEND_MESSAGE = 2;
type
TDXChatMessage = record
MessageType: DWORD; { MessageType is absolutely necessary. }
Len: Integer;
C: array[0..0] of Char;
end;
TDXDATAMessage = record
MessageType: DWORD; { MessageType is absolutely necessary. }
Len: word;
data: array[0..0] of byte;
end;
datapointer = ^datatype;
datatype = array[0..65535] of byte;
type
TMainForm = class(TForm)
DXPlay1: TDXPlay;
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
CheckBox1: TCheckBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
ListBox1: TListBox;
Label5: TLabel;
OpenDialog1: TOpenDialog;
Label6: TLabel;
Label7: TLabel;
Button3: TButton;
Button4: TButton;
Gauge1: TGauge;
procedure DXPlay1AddPlayer(Sender: TObject; Player: TDXPlayPlayer);
procedure DXPlay1DeletePlayer(Sender: TObject; Player: TDXPlayPlayer);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure DXPlay1Message(Sender: TObject; Player: TDXPlayPlayer; Data:
Pointer;
DataSize: Integer);
procedure Button1Click(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure DXPlay1Open(Sender: TObject);
procedure MySendMEssage(PlayerID : integer; textmsg : string);
procedure Button2Click(Sender: TObject);
procedure MySendData(PlayerID : integer; data : datapointer; len :word);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
Procedure RequestFileSend(PlayerID : integer; filetableindex : word);
end;
var
MainForm: TMainForm;
ioerr : integer;
implementation
{$R *.DFM}
procedure TMainForm.DXPlay1AddPlayer(Sender: TObject; Player:
TDXPlayPlayer);
begin
Memo1.Lines.Add(Format(' %s entered a room.', [Player.Name]));
end;
procedure TMainForm.DXPlay1DeletePlayer(Sender: TObject;
Player: TDXPlayPlayer);
begin
Memo1.Lines.Add(Format(' %s left a room.', [Player.Name]));
end;
procedure TMainForm.DXPlay1Open(Sender: TObject);
var
i: Integer;
begin
for i:=0 to DXPlay1.Players.Count-1 do
if DXPlay1.Players[i].RemotePlayer then
Memo1.Lines.Add(Format(' %s is entering a room.',
[DXPlay1.Players[i].Name]));
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
DXPlay1.Close;
application.Terminate;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
// TCP/IP init
try
DXPlay1.Open;
except
on E: Exception do
begin
Application.ShowMainForm := False;
Application.HandleException(E);
Application.Terminate;
end;
end;
MainForm.Caption := Format('%s : %s', [DXPlay1.ProviderName,
DXPlay1.SessionName]);
filemode:=0;
activefiles:=0;
randomize;
end;
procedure TMainForm.MySendMEssage(PlayerID : integer; textmsg : string);
var
Msg: ^TDXChatMessage;
MsgSize: Integer;
begin
MsgSize := SizeOf(TDXChatMessage)+Length(textmsg);
GetMem(Msg, MsgSize);
try
Msg.MessageType := DXCHAT_MESSAGE;
Msg.Len := Length(textmsg);
StrLCopy(@Msg^.c, PChar(textmsg), Length(textmsg));
{ The message is sent all. }
DXPlay1.SendMessage(PlayerID, Msg, MsgSize);
finally
FreeMem(Msg);
end;
end;
procedure TMainForm.MySendData(PlayerID : integer; data : datapointer; len :
word);
var
Msg: ^TDXDATAMessage;
MsgSize: Integer;
begin
MsgSize := SizeOf(TDXDATAMessage)+Len;
GetMem(Msg, MsgSize);
try
Msg.MessageType := DXDATA_MESSAGE;
Msg.Len := len;
move(data,msg^.data,len);
// StrLCopy(@Msg^.c, PChar(textmsg), Length(textmsg));
{ The message is sent. }
DXPlay1.SendMessage(PlayerID, Msg, MsgSize);
finally
FreeMem(Msg);
end;
end;
Procedure TMainForm.RequestFileSend(PlayerID : integer; filetableindex :
word);
var
Msg: ^TDXDATAMessage;
MsgSize: Integer;
tmpfilerec : filetablerec;
begin
MsgSize := SizeOf(TDXDATAMessage)+SizeOf(Filetablerec);
GetMem(Msg, MsgSize);
try
Msg.MessageType := DXREQ_SEND_MESSAGE;
Msg.Len := msgsize;
tmpfilerec:=filetable[filetableindex];
move(tmpfilerec,msg^.data,sizeof(filetablerec));
// StrLCopy(@Msg^.c, PChar(textmsg), Length(textmsg));
{ The message is sent. }
DXPlay1.SendMessage(PlayerID, Msg, MsgSize);
finally
FreeMem(Msg);
end;
end;
procedure TMainForm.DXPlay1Message(Sender: TObject; Player: TDXPlayPlayer;
Data: Pointer; DataSize: Integer);
var
s: string;
tel : word;
tmpfilerec : filetablerec;
offse : word;
begin
case DXPlayMessageType(Data) of
DXCHAT_MESSAGE:
begin
if TDXChatMessage(Data^).Len<=0 then
s := ''
else begin
SetLength(s, TDXChatMessage(Data^).Len);
StrLCopy(PChar(s), @TDXChatMessage(Data^).c, Length(s));
end;
memo1.Lines.Add('Message from PlayerID: '+inttostr(player.ID));
Memo1.Lines.Add(Format('%s> %s', [Player.Name, s]));
// MySendMessage(player.ID,'Message accepted!');
end;
DXDATA_MESSAGE:
begin
end;
DXREQ_SEND_MESSAGE:
begin // some user wants to send a file ... let him ?
offse:=sizeof(TDXDATAMessage);
move(data^,tmpfilerec,sizeof(tmpfilerec));
memo1.Lines.Add('File send request arrived...');
memo1.Lines.Add(tmpfilerec.filename);
end;
end;
end;
procedure TMainForm.Button1Click(Sender: TObject);
var
Msg: ^TDXChatMessage;
MsgSize: Integer;
begin
MsgSize := SizeOf(TDXChatMessage)+Length(Edit1.Text);
GetMem(Msg, MsgSize);
try
Msg.MessageType := DXCHAT_MESSAGE;
Msg.Len := Length(Edit1.Text);
StrLCopy(@Msg^.c, PChar(Edit1.Text), Length(Edit1.Text));
{ The message is sent all. }
DXPlay1.SendMessage(DPID_ALLPLAYERS, Msg, MsgSize);
{ The message is sent also to me. }
DXPlay1.SendMessage(DXPlay1.LocalPlayer.ID, Msg, MsgSize);
Edit1.Text := '';
finally
FreeMem(Msg);
end;
end;
procedure TMainForm.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key=VK_RETURN then
begin
Button1Click(nil);
key := 0;
end;
end;
procedure TMainForm.Button2Click(Sender: TObject);
var
tel: word;
begin
if dxplay1.IsHost then checkbox1.Checked:=true else
checkbox1.Checked:=false;
label2.Caption:=dxplay1.TCPIPSetting.HostName;
label4.caption:=inttostr(dxplay1.TCPIPSetting.Port);
listbox1.Clear;
for tel:=0 to dxplay1.Players.Count-1 do
listbox1.Items.Add(dxplay1.players[tel].name+' -
'+inttostr(dxplay1.players[tel].id));
dxplay1.TCPIPSetting.HostName:='THESESSION';
end;
end.
Håber det hjælper dig.
Venligst
Ingolf