level 3
changtaiyq
楼主
使用UDP发送数据,发送一个十几个字节的数据(TIdbytes),结果报SOCKET Error 10040
message too long
才十几字节。。,哪里太长了。我用的事D7和indy 10.5.6
代码附上:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, IdUDPClient, IdBaseComponent, IdComponent,
IdUDPBase, IdUDPServer,IdSocketHandle,IdGlobal;
type
TestDate=packed record
I1:Integer;
B1:Byte;
D1:Double;
end;
PTestDate=^TestDate;
TForm1 = class(TForm)
udpsrvr1: TIdUDPServer;
udpclnt1: TIdUDPClient;
btnSend: TBitBtn;
btnReady: TBitBtn;
mmo1: TMemo;
procedure btnSendClick(Sender: TObject);
procedure udpsrvr1UDPRead(AThread: TIdUDPListenerThread;
AData: TIdBytes; ABinding: TIdSocketHandle);
procedure btnReadyClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
client: TIdSocketHandle;
implementation
{$R *.dfm}
procedure TForm1.btnSendClick(Sender: TObject);
var
data:TestDate;
ddata:tidbytes;
begin
if Assigned(client) then
begin
data.I1:=1;
data.B1:=2;
data.D1:=1.6;
ddata:=RawToBytes(data,SizeOf(data));
client.Connect;
mmo1.Lines.Add('send count:'+inttostr(client.Send(ddata))); //发送了13字节
end;
end;
procedure TForm1.udpsrvr1UDPRead(AThread: TIdUDPListenerThread;
AData: TIdBytes; ABinding: TIdSocketHandle);
begin
if PChar(@adata[0])='Login' then
begin
client:=abinding; //登记
end;
end;
procedure readclient;
var
ddata:TIdBytes;
data:TestDate;
begin
with form1 do
begin
udpclnt1.Connect;
udpclnt1.Send('Login'); //先报到
while True do
begin
if udpclnt1.ReceiveBuffer(ddata)>0 then //这里报错
begin
BytesToRaw(ddata,data,SizeOf(data));
mmo1.Lines.Add('I1:'+inttostr(data.I1)+';b1:'+inttostr(data.B1)+';d1:'+floattostr(data.d1));
end;
end;
end;
end;
procedure TForm1.btnReadyClick(Sender: TObject);
var
dw:DWORD;
begin
udpsrvr1.Active:=True;
CreateThread(nil,0,@readclient,nil,0,dw);
end;
end.
比较少用UDP,实在搞不懂,希望有高手能指点下
2015年03月09日 02点03分
1
message too long
才十几字节。。,哪里太长了。我用的事D7和indy 10.5.6
代码附上:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, IdUDPClient, IdBaseComponent, IdComponent,
IdUDPBase, IdUDPServer,IdSocketHandle,IdGlobal;
type
TestDate=packed record
I1:Integer;
B1:Byte;
D1:Double;
end;
PTestDate=^TestDate;
TForm1 = class(TForm)
udpsrvr1: TIdUDPServer;
udpclnt1: TIdUDPClient;
btnSend: TBitBtn;
btnReady: TBitBtn;
mmo1: TMemo;
procedure btnSendClick(Sender: TObject);
procedure udpsrvr1UDPRead(AThread: TIdUDPListenerThread;
AData: TIdBytes; ABinding: TIdSocketHandle);
procedure btnReadyClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
client: TIdSocketHandle;
implementation
{$R *.dfm}
procedure TForm1.btnSendClick(Sender: TObject);
var
data:TestDate;
ddata:tidbytes;
begin
if Assigned(client) then
begin
data.I1:=1;
data.B1:=2;
data.D1:=1.6;
ddata:=RawToBytes(data,SizeOf(data));
client.Connect;
mmo1.Lines.Add('send count:'+inttostr(client.Send(ddata))); //发送了13字节
end;
end;
procedure TForm1.udpsrvr1UDPRead(AThread: TIdUDPListenerThread;
AData: TIdBytes; ABinding: TIdSocketHandle);
begin
if PChar(@adata[0])='Login' then
begin
client:=abinding; //登记
end;
end;
procedure readclient;
var
ddata:TIdBytes;
data:TestDate;
begin
with form1 do
begin
udpclnt1.Connect;
udpclnt1.Send('Login'); //先报到
while True do
begin
if udpclnt1.ReceiveBuffer(ddata)>0 then //这里报错
begin
BytesToRaw(ddata,data,SizeOf(data));
mmo1.Lines.Add('I1:'+inttostr(data.I1)+';b1:'+inttostr(data.B1)+';d1:'+floattostr(data.d1));
end;
end;
end;
end;
procedure TForm1.btnReadyClick(Sender: TObject);
var
dw:DWORD;
begin
udpsrvr1.Active:=True;
CreateThread(nil,0,@readclient,nil,0,dw);
end;
end.
比较少用UDP,实在搞不懂,希望有高手能指点下