青
青春无悔
Unregistered / Unconfirmed
GUEST, unregistred user!
unit NetUnt;interfaceuses windows, winsock2, SysUtils, Variants, yifanUnt;function StartNet(): integer;function udp_init(): integer;function Get_sockaddr(IPServer: pchar; port: integer): integer;function udp_bind(szServer: pchar; port: integer): integer;function udp_send(szBuz: pchar; dwlen: integer; ip: pchar; port: integer): integer;function udp_recv(var szBuz: array of char; dwlen: integer; var ip:array of char; var port: integer): integer;function Set_Timeout(timeout: integer): integer;var gSck: integer; wsdata: TWSADATA; sck_add: TSockAddr;implementationfunction StartNet(): integer;var ret: integer;begin ret := WSAStartup(MAKEWORD(2, 2), wsdata); if ret <> 0 then begin result := -1; exit; end; result := 0; gSck := 0;end;function Set_Timeout(timeout: integer): integer;begin setsockopt(gsck, SOL_SOCKET, SO_RCVTIMEO, @timeout, sizeof(timeout)); result := 0;end;function udp_init(): integer;var ret: integer; bOpt: Integer;begin gSck := socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (gSck = INVALID_SOCKET) then begin result := -3; exit; end; bOpt := 1; ret := setsockopt(gsck, SOL_SOCKET, SO_BROADCAST, @bOpt, SizeOf(bOpt)); if (SOCKET_ERROR = ret) then begin result := -3; exit; end; result := 0;end;function Get_sockaddr(IPServer: pchar; port: integer): integer;var TmpIp: string; host: PHostEnt;begin sck_add.sin_family := AF_INET; sck_add.sin_port := htons(port); TmpIp := StrPas(IPServer); if TmpIp <> '' then begin if TmpIp = '255.255.255.255' then begin sck_add.sin_addr.s_addr := htonl(INADDR_BROADCAST); result := 0; exit; end else begin sck_add.sin_addr.s_addr := inet_addr(IPServer); if (sck_add.sin_addr.s_addr = INADDR_NONE) then begin host := gethostbyname(IPServer); if host <> nil then begin result := -1; exit; end; CopyMemory(@sck_add.sin_addr, host^.h_addr_list^, host.h_length); result := 0; end; result := 0; end end else begin sck_add.sin_addr.s_addr := htonl(INADDR_ANY); result := 0; end;end;function udp_bind(szServer: pchar; port: integer): integer;begin if get_sockaddr(szServer, port) <> 0 then begin result := -1; exit; end; if bind(gsck, @sck_add, sizeof(sck_add)) = SOCKET_ERROR then begin result := -2; exit; end; result := 0;end;function udp_send(szBuz: pchar; dwlen: integer; ip: pchar; port: integer): integer;var ret: integer;begin ret := get_sockaddr(ip, port); if ret <> 0 then begin result := -1; exit; end; result := sendto(gSck, szBuz, dwlen, 0, @sck_add, sizeof(sck_add));end;function udp_recv(var szBuz: array of char; dwlen: integer; var ip:array of char; var port: integer): integer;var ret: integer; dwSenderSize: integer; LocalHostchar;begin dwSenderSize := sizeof(sck_add); GetMem(LocalHost,30); ZeroMemory(LocalHost,30); ret := recvfrom(gsck, szBuz, dwlen, 0, @sck_add, dwSenderSize); if (ret = SOCKET_ERROR) then result := -1 else begin LocalHost := inet_ntoa(sck_add.sin_addr); CopyMemory(@ip[0],LocalHost , 15); port := htons(sck_add.sin_port); result := ret; end;end;end.unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient, Sockets;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; IdUDPClient1: TIdUDPClient; Button6: TButton; UdpSocket1: TUdpSocket; Button7: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementationuses NetUnt;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);var ret:integer;begin ret := StartNet(); if ret<>0 then showmessage('初始化网络失败!') else showmessage('初始化网络成功!');end;procedure TForm1.Button2Click(Sender: TObject);var ret:integer;begin ret := udp_init(); if ret<>0 then showmessage('初始化网络套接字失败!') else showmessage('初始化网络套接字成功!');end;procedure TForm1.Button3Click(Sender: TObject);var ret,len:integer; TmpArr:array [0..100] of char;begin len:=101+16; for len:=0 to 100 do tmparr[len] := chr(len); ret := udp_send(@tmparr[0],len,pchar('192.168.1.100'),6500); if ret<>len then showmessage('发送数据失败!') else showmessage('发送数据成功!');end;procedure TForm1.Button4Click(Sender: TObject);var ret:integer;begin ret := Set_Timeout(1000); if ret<>0 then showmessage('设置超时失败!') else showmessage('设置超时成功!');end;procedure TForm1.Button5Click(Sender: TObject);var ret:integer;begin ret := udp_bind('',5102); if ret<>0 then showmessage('绑定失败!') else showmessage('绑定成功!');end;procedure TForm1.Button6Click(Sender: TObject);begin IdUDPClient1.Send('192.168.1.100',6500,'1234567890');end;end.代码如下,Button3Click发出来的数据前面会多16个字节的头,总长度不变,导致扣面16个字节的数据丢失!