unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls, ACMWaveOut, ACMWaveIn, ACMDialog;
type
TMainForm = class(TForm)
ConnectButton: TButton;
AddrEdit: TEdit;
Label1: TLabel;
ServerSocket: TServerSocket;
ClientSocket: TClientSocket;
ACMDialog: TACMDialog;
ACMWaveIn: TACMWaveIn;
ACMWaveOut: TACMWaveOut;
DropButton: TButton;
Memo: TMemo;
Label2: TLabel;
ClearButton: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ConnectButtonClick(Sender: TObject);
procedure DropButtonClick(Sender: TObject);
procedure ClearButtonClick(Sender: TObject);
procedure ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
procedure ACMWaveInData(data: Pointer; size: Integer);
procedure ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
Const
BufLen:Integer=1024*1024;
Type
Buffer=Array[1..1] of Byte;
BufPointer=^Buffer;
Var
MemStream:TMemoryStream;
Buf:^Buffer;
IsServer:Boolean;
Client:TCustomWinSocket;
procedure TMainForm.FormCreate(Sender: TObject);
Var
Format
ointer;
begin
ServerSocket.Port:=3366;
ServerSocket.ServerType:=stNonBlocking;
ServerSocket.Open;
GetMem(Buf,BufLen);
Memo.Lines.Add('Local server srarted.');
Format:=ACMDialog.OpenDialog;
If Format=nil Then Exit;
ACMWaveOut.Open(Format);
ACMWaveIn.Open(Format);
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
FreeMem(Buf);
ACMWaveIn.Close;
ACMWaveOut.Close;
ServerSocket.Close;
Memo.Lines.Add('Local server shutdown.');
end;
procedure TMainForm.ConnectButtonClick(Sender: TObject);
begin
ClientSocket.Port:=3366;
ClientSocket.ClientType:=ctNonBlocking;
ClientSocket.Address:=AddrEdit.Text;
ClientSocket.Open;
IsServer:=False;
end;
procedure TMainForm.DropButtonClick(Sender: TObject);
begin
ClientSocket.Close;
end;
procedure TMainForm.ClearButtonClick(Sender: TObject);
begin
Memo.Clear;
end;
procedure TMainForm.ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo.Lines.Add('A client connected.');
IsServer:=True;
Client:=Socket;
end;
procedure TMainForm.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo.Lines.Add('A client droped.');
end;
procedure TMainForm.ClientSocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo.Lines.Add('Connecting to server '+AddrEdit.Text);
end;
procedure TMainForm.ACMWaveInData(data: Pointer; size: Integer);
Var
sending,sent,index,temp:Longint;
s:String;
begin
{ temp:=size;
sent:=0;
Index:=1;
While sent<temp do
Begin
sending:=ClientSocket.Socket.SendBuf(BufPointer(data)[index],Temp);
If sending<>-1 Then
Begin
sent:=sent+sending;
index:=index+sending;
temp:=temp-sending;
End;
End;}
Case IsServer of
True:
Begin
If Client.Connected Then
Begin
Client.SendBuf(data^,size);
End;
End;
False:
Begin
If ClientSocket.Socket.Connected Then
Begin
Str(ClientSocket.Socket.SendBuf(data^,size),s);
Memo.Lines.Add('Sent '+s);
End;
End;
End;
end;
procedure TMainForm.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
Var
temp:Longint;
begin
temp:=Socket.ReceiveBuf(Buf^,BufLen);
If NOT IsServer Then Exit;
ACMWaveOut.PlayBack(Buf,temp);
end;
procedure TMainForm.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
Var
temp:Longint;
begin
temp:=Socket.ReceiveBuf(Buf^,BufLen);
If IsServer Then Exit;
ACMWaveOut.PlayBack(Buf,temp);
end;
end.