H
hq_pan
Unregistered / Unconfirmed
GUEST, unregistred user!
我使用了 ICS 套件种的 TWSocket 控件,协议方式为 UDP.
测试是发送字符串句没问题,但是发送二进制,接收就有问题!!!
我使用 ACM 获取语音输入,然后通过 UDP 方式发送出去,
接收后播放是刺耳的声音,而不是话筒输入的声音。如果不通过
网络发送,而直接播放话筒输入的声音就没问题。我已经用过
了 Indy 的控件,下载的源代码用的是 NMUDP。下载的代码
运行的好好的,换了ICS/Indy就不行了,请我是不是我服务器端
接收的代码有错啊?我用二进制方式传送字符串,接收过来
却是正确的。
请高手帮帮忙!!!明天就要检查了...[!]
分不是问题,手上还有几千分。
================================================================================
unit uMainFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, WSocket, WinSock, ACMIn, ACMOut, ACMConvertor, ExtCtrls;
type
TForm1 = class(TForm)
edtRmtIP: TEdit;
btnStart: TButton;
btnStop: TButton;
Memo1: TMemo;
UDPSrv: TWSocket;
UDPCli: TWSocket;
Label1: TLabel;
Label2: TLabel;
edtRmtPort: TEdit;
btnSend: TButton;
Splitter1: TSplitter;
btnStartWave: TButton;
btnStopWave: TButton;
UDPWaveCli: TWSocket;
UDPWaveSrv: TWSocket;
ACMOut1: TACMOut;
ACMIn1: TACMIn;
ACMConvertor1: TACMConvertor;
procedure btnStartClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
procedure UDPSrvDataAvailable(Sender: TObject; ErrCode: Word);
procedure FormCreate(Sender: TObject);
procedure btnSendClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnStartWaveClick(Sender: TObject);
procedure btnStopWaveClick(Sender: TObject);
procedure UDPWaveSrvDataAvailable(Sender: TObject; ErrCode: Word);
procedure ACMIn1BufferFull(Sender: TObject; Data: Pointer;
Size: Integer);
private
{ Private declarations }
//FBufSize: Longint;
public
{ Public declarations }
procedure prcToggleBtnState(Start: Boolean);
procedure prcTglWaveBtn(const ToStart: Boolean);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
SrvPort = '2828';
SockType = 'UDP';
procedure TForm1.prcToggleBtnState(Start: Boolean);
begin
btnStart.Enabled := Start;
btnStop.Enabled := not Start;
end;
procedure TForm1.prcTglWaveBtn(const ToStart: Boolean);
begin
btnStartWave.Enabled := ToStart;
btnStopWave.Enabled := not ToStart;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
edtRmtPort.Text := SrvPort;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UDPSrv.Close;
end;
procedure TForm1.btnStartClick(Sender: TObject);
begin
UDPCli.Close;
UDPSrv.Proto := SockType;
UDPSrv.Addr := '0.0.0.0';
UDPSrv.Port := edtRmtPort.Text;
//UDPSrv.BufSize := ACMConvertor1.InputBufferSize;
UDPSrv.Listen;
prcToggleBtnState(False);
Memo1.Lines.Add('Server start at ' + DateTimeToStr(Now));
end;
procedure TForm1.btnStopClick(Sender: TObject);
begin
UDPSrv.Close;
prcToggleBtnState(True);
Memo1.Lines.Add('Server stop at ' + DateTimeToStr(Now));
end;
procedure TForm1.UDPSrvDataAvailable(Sender: TObject; ErrCode: Word);
var
Buffer : PChar; // ; Pointer array [0..8192] of char
Bufsize: integer;
Len : Integer;
//Src : TSockAddrIn;
//SrcLen : Integer;
begin
Memo1.Lines.Add('Recieved data at ' + DateTimeToStr(Now));
Bufsize := 1024;
Getmem(Buffer, Bufsize);
Len := 0;
try
try
Len := UDPSrv.Receive(Buffer, Bufsize);// SizeOf(Buffer)
except
Memo1.Lines.Add('Reciev error!');
end;
try
Memo1.Lines.Add('Recieved data length: ' + IntToStr(Len));
except
Memo1.Lines.Add('Show reciev data length error!');
end;
if Len > 0 then
begin
Buffer[Len] := #0;
Memo1.Lines.Add('================');
Memo1.Lines.Add(StrPas(Buffer));
Memo1.Lines.Add('================');
end;
finally
FreeMem(Buffer);
Memo1.Lines.Add('Finally FreeMem.');
end;
//Len := UDPSrv.LastError;
Memo1.Lines.Add('Last error code: ' + IntToStr(UDPSrv.LastError)
+ #13#10'ErrCode: ' + IntToStr(ErrCode));
Memo1.Lines.Add('Recieved data finished.');
end;
procedure TForm1.btnSendClick(Sender: TObject);
var
data: Pointer;
s: string;
sl: integer;
begin
Memo1.Lines.Add('Sending data out: ' + DateTimeToStr(Now));
//GetMem(data, Length(Memo1.Lines[1]));
s := Memo1.Lines[1];
data := @s[1];
UDPCli.Proto := SockType;
UDPCli.Addr := edtRmtIP.Text;
UDPCli.Port := edtRmtPort.Text;
UDPCli.LocalPort := '0';
UDPCli.Connect;
sl := UDPCli.Send(data, Length(s));
UDPCli.Close;
if sl = -1 then
begin
Memo1.Lines.Add('Last Error code: ' + IntToStr(UDPCli.LastError));
exit;
end;
Memo1.Lines.Add('Sending data finished: ' + DateTimeToStr(Now));
end;
//------------------------------------------------------------------------------
procedure TForm1.btnStartWaveClick(Sender: TObject);
begin
ACMOut1.Open(ACMConvertor1.FormatIn);
UDPWaveCli.Close;
UDPWaveSrv.Proto := SockType;
UDPWaveSrv.Addr := '0.0.0.0';
UDPWaveSrv.Port := edtRmtPort.Text;
//UDPSrv.BufSize := ACMConvertor1.InputBufferSize;
UDPWaveSrv.Listen;
prcTglWaveBtn(False);
ACMIn1.Open(ACMConvertor1.FormatIn);
Memo1.Lines.Add('Wave Server started at ' + DateTimeToStr(Now));
end;
procedure TForm1.btnStopWaveClick(Sender: TObject);
begin
ACMIn1.Close;
UDPWaveSrv.Close;
ACMOut1.Close;
prcTglWaveBtn(True);
Memo1.Lines.Add('Wave Server stop at ' + DateTimeToStr(Now));
end;
procedure TForm1.UDPWaveSrvDataAvailable(Sender: TObject; ErrCode: Word);
var
Buffer : PChar;
Bufsize: integer;
Len : Integer;
Src : TSockAddrIn;
SrcLen : Integer;
begin
Bufsize := 8192; //ACMConvertor1.FormatIn.Format.nAvgBytesPerSec + 100;
Getmem(Buffer, Bufsize);
try
Len := UDPWaveSrv.ReceiveFrom(Buffer, Bufsize, Src, SrcLen);
{Memo1.Lines.Add('Recieved data form ' + StrPas(inet_ntoa(Src.sin_addr)) +
':' + IntToStr(ntohs(Src.sin_port)));
Memo1.Lines.Add('Recieved data length: ' + IntToStr(Len));}
if Len > 0 then
begin
ACMOut1.Play(Buffer, Len);
end;
finally
FreeMem(Buffer);
end;
//Memo1.Lines.Add('Recieved data finished.');
end;
procedure TForm1.ACMIn1BufferFull(Sender: TObject; Data: Pointer;
Size: Integer);
begin
UDPWaveCli.Proto := SockType;
UDPWaveCli.Addr := edtRmtIP.Text;
UDPWaveCli.Port := edtRmtPort.Text;
UDPWaveCli.LocalPort := '0';
UDPWaveCli.Connect;
UDPWaveCli.Send(Data, Size);
UDPWaveCli.Close;
//Memo1.Lines.Add('Send wave data at ' + DateTimeToStr(Now));
end;
end.
测试是发送字符串句没问题,但是发送二进制,接收就有问题!!!
我使用 ACM 获取语音输入,然后通过 UDP 方式发送出去,
接收后播放是刺耳的声音,而不是话筒输入的声音。如果不通过
网络发送,而直接播放话筒输入的声音就没问题。我已经用过
了 Indy 的控件,下载的源代码用的是 NMUDP。下载的代码
运行的好好的,换了ICS/Indy就不行了,请我是不是我服务器端
接收的代码有错啊?我用二进制方式传送字符串,接收过来
却是正确的。
请高手帮帮忙!!!明天就要检查了...[!]
分不是问题,手上还有几千分。
================================================================================
unit uMainFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, WSocket, WinSock, ACMIn, ACMOut, ACMConvertor, ExtCtrls;
type
TForm1 = class(TForm)
edtRmtIP: TEdit;
btnStart: TButton;
btnStop: TButton;
Memo1: TMemo;
UDPSrv: TWSocket;
UDPCli: TWSocket;
Label1: TLabel;
Label2: TLabel;
edtRmtPort: TEdit;
btnSend: TButton;
Splitter1: TSplitter;
btnStartWave: TButton;
btnStopWave: TButton;
UDPWaveCli: TWSocket;
UDPWaveSrv: TWSocket;
ACMOut1: TACMOut;
ACMIn1: TACMIn;
ACMConvertor1: TACMConvertor;
procedure btnStartClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
procedure UDPSrvDataAvailable(Sender: TObject; ErrCode: Word);
procedure FormCreate(Sender: TObject);
procedure btnSendClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnStartWaveClick(Sender: TObject);
procedure btnStopWaveClick(Sender: TObject);
procedure UDPWaveSrvDataAvailable(Sender: TObject; ErrCode: Word);
procedure ACMIn1BufferFull(Sender: TObject; Data: Pointer;
Size: Integer);
private
{ Private declarations }
//FBufSize: Longint;
public
{ Public declarations }
procedure prcToggleBtnState(Start: Boolean);
procedure prcTglWaveBtn(const ToStart: Boolean);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
SrvPort = '2828';
SockType = 'UDP';
procedure TForm1.prcToggleBtnState(Start: Boolean);
begin
btnStart.Enabled := Start;
btnStop.Enabled := not Start;
end;
procedure TForm1.prcTglWaveBtn(const ToStart: Boolean);
begin
btnStartWave.Enabled := ToStart;
btnStopWave.Enabled := not ToStart;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
edtRmtPort.Text := SrvPort;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UDPSrv.Close;
end;
procedure TForm1.btnStartClick(Sender: TObject);
begin
UDPCli.Close;
UDPSrv.Proto := SockType;
UDPSrv.Addr := '0.0.0.0';
UDPSrv.Port := edtRmtPort.Text;
//UDPSrv.BufSize := ACMConvertor1.InputBufferSize;
UDPSrv.Listen;
prcToggleBtnState(False);
Memo1.Lines.Add('Server start at ' + DateTimeToStr(Now));
end;
procedure TForm1.btnStopClick(Sender: TObject);
begin
UDPSrv.Close;
prcToggleBtnState(True);
Memo1.Lines.Add('Server stop at ' + DateTimeToStr(Now));
end;
procedure TForm1.UDPSrvDataAvailable(Sender: TObject; ErrCode: Word);
var
Buffer : PChar; // ; Pointer array [0..8192] of char
Bufsize: integer;
Len : Integer;
//Src : TSockAddrIn;
//SrcLen : Integer;
begin
Memo1.Lines.Add('Recieved data at ' + DateTimeToStr(Now));
Bufsize := 1024;
Getmem(Buffer, Bufsize);
Len := 0;
try
try
Len := UDPSrv.Receive(Buffer, Bufsize);// SizeOf(Buffer)
except
Memo1.Lines.Add('Reciev error!');
end;
try
Memo1.Lines.Add('Recieved data length: ' + IntToStr(Len));
except
Memo1.Lines.Add('Show reciev data length error!');
end;
if Len > 0 then
begin
Buffer[Len] := #0;
Memo1.Lines.Add('================');
Memo1.Lines.Add(StrPas(Buffer));
Memo1.Lines.Add('================');
end;
finally
FreeMem(Buffer);
Memo1.Lines.Add('Finally FreeMem.');
end;
//Len := UDPSrv.LastError;
Memo1.Lines.Add('Last error code: ' + IntToStr(UDPSrv.LastError)
+ #13#10'ErrCode: ' + IntToStr(ErrCode));
Memo1.Lines.Add('Recieved data finished.');
end;
procedure TForm1.btnSendClick(Sender: TObject);
var
data: Pointer;
s: string;
sl: integer;
begin
Memo1.Lines.Add('Sending data out: ' + DateTimeToStr(Now));
//GetMem(data, Length(Memo1.Lines[1]));
s := Memo1.Lines[1];
data := @s[1];
UDPCli.Proto := SockType;
UDPCli.Addr := edtRmtIP.Text;
UDPCli.Port := edtRmtPort.Text;
UDPCli.LocalPort := '0';
UDPCli.Connect;
sl := UDPCli.Send(data, Length(s));
UDPCli.Close;
if sl = -1 then
begin
Memo1.Lines.Add('Last Error code: ' + IntToStr(UDPCli.LastError));
exit;
end;
Memo1.Lines.Add('Sending data finished: ' + DateTimeToStr(Now));
end;
//------------------------------------------------------------------------------
procedure TForm1.btnStartWaveClick(Sender: TObject);
begin
ACMOut1.Open(ACMConvertor1.FormatIn);
UDPWaveCli.Close;
UDPWaveSrv.Proto := SockType;
UDPWaveSrv.Addr := '0.0.0.0';
UDPWaveSrv.Port := edtRmtPort.Text;
//UDPSrv.BufSize := ACMConvertor1.InputBufferSize;
UDPWaveSrv.Listen;
prcTglWaveBtn(False);
ACMIn1.Open(ACMConvertor1.FormatIn);
Memo1.Lines.Add('Wave Server started at ' + DateTimeToStr(Now));
end;
procedure TForm1.btnStopWaveClick(Sender: TObject);
begin
ACMIn1.Close;
UDPWaveSrv.Close;
ACMOut1.Close;
prcTglWaveBtn(True);
Memo1.Lines.Add('Wave Server stop at ' + DateTimeToStr(Now));
end;
procedure TForm1.UDPWaveSrvDataAvailable(Sender: TObject; ErrCode: Word);
var
Buffer : PChar;
Bufsize: integer;
Len : Integer;
Src : TSockAddrIn;
SrcLen : Integer;
begin
Bufsize := 8192; //ACMConvertor1.FormatIn.Format.nAvgBytesPerSec + 100;
Getmem(Buffer, Bufsize);
try
Len := UDPWaveSrv.ReceiveFrom(Buffer, Bufsize, Src, SrcLen);
{Memo1.Lines.Add('Recieved data form ' + StrPas(inet_ntoa(Src.sin_addr)) +
':' + IntToStr(ntohs(Src.sin_port)));
Memo1.Lines.Add('Recieved data length: ' + IntToStr(Len));}
if Len > 0 then
begin
ACMOut1.Play(Buffer, Len);
end;
finally
FreeMem(Buffer);
end;
//Memo1.Lines.Add('Recieved data finished.');
end;
procedure TForm1.ACMIn1BufferFull(Sender: TObject; Data: Pointer;
Size: Integer);
begin
UDPWaveCli.Proto := SockType;
UDPWaveCli.Addr := edtRmtIP.Text;
UDPWaveCli.Port := edtRmtPort.Text;
UDPWaveCli.LocalPort := '0';
UDPWaveCli.Connect;
UDPWaveCli.Send(Data, Size);
UDPWaveCli.Close;
//Memo1.Lines.Add('Send wave data at ' + DateTimeToStr(Now));
end;
end.