Socket IPUDP MultiCast

xiaoxiao2021-03-06  21

Class A Internet URL: 7-digit network number 24-bit host number

011111111111111111111111 ---> 01111111 = 127

0..127 0..255 0..255 0..255

0.0.0.0 - 127.255.255.255

Class B Internet URL: 14-digit network number 16 host number

10111111111111111111111 ---> 10111111 = 191

128..191 0..255 0..255 0..255

128.0.0.0 - 191.255.255.255

C Internet URL: 21 network number 8 host number

11011111111111111111111 ---> 11011111 = 223

192..23 0..255 0..255 0..255

192.0.0.0 - 223.255.255.255

Multicast Address: 224.0.0.0 - 255.255.255.255

WinSock1 IP / UDP multicast small experiment:

Const mcastaddr = '234.5.6.7'; mcastport = 25000; local_port = 5555; wm_socket = WM_USER 55;

type TForm1 = class (TForm) Button1: TButton; Edit1: TEdit; ListBox1: TListBox; CheckBox1: TCheckBox; procedure FormCreate (Sender: TObject); procedure Button1Click (Sender: TObject); procedure FormClose (Sender: TObject; var Action: TCloseAction ); Private {private declarations} procedure wmsocket (var msg: tMSSAGE); Message WM_Socket;

Public {public declarations} END;

Var Form1: TFORM1;

IMPLEMentation

{$ R * .dfm}

Procedure errmsg (MSG: String); Begin MessageBox (0, Pchar (MSG), 'Ping Program Error', MB_ICONERROR); END;

procedure TForm1.WMSocket (var Msg: TMessage); var buf: array [0..255] of char; remote: TSockaddr; len: integer; p: string; begin case WSAGetSelectEvent (Msg.LParam) of FD_READ: begin recvfrom ( Msg.wParam, BUF [0], 255, 0, Remote, Len; getPeername (Msg.wParam, Remote, Len); P: = inet_ntoa (remote.sin_addr) 'Port:' INTOSTR (NTOHS (Remote. SIN_PORT)); ListBox1.Items.Add (P BUF); End; End; End; Procedure TFORM1.FormCreate (Sender: TOBJECT); VAR WSA: TWSADATA; Begin IF WSASTARTAP ($ 0202, WSA) <> 0 Then errmsg ( 'Windows socket is not responed.'); Listbox1.font.name: = 'Courier new'; ListBox1.font.size: = 9;

Procedure TForm1.FormClose (Sender: TpoBject; VAR Action: Tclosection); Begin IF WSACLEANUP <> 0 Then Errmsg ('Windows Socket CAN NOT BE CLOSED.'); END;

Procedure TForm1.Button1Click (Sender: Toscket); Var Sock: Tsocket; Local, Remote: TsockAddr; Bufsend: Array [0..31] of char; RET, I: S32; TTL, OPT: S32; BSENDER: BOOL; MCAST : ip_mReq; Begin Bsender: = CheckBox1.Checked;

SOCK: = Socket (AF_INET, SOCK_DGRAM, 0); if Sock = INVALID_SOCKET THEN BEGIN Errmsg ('Socket Failed:' INTOSTR (Wsagetlasterror); EXIT;

Local.sin_family: = af_INET; local.sin_port: = HTONS (U16 (mcastport)); local.sin_addr.s_addr: = inet_addr ('192.168.1.12');

// if bind (Sock, @local, sizeof (local)) = socket_error the begin errmsg ('Bind Failed:' INTOSTR (Wsagetlasterror); CloseSocket (SOCK); EXIT; END;

if WSAAsyncSelect (sock, Handle, WM_SOCKET, FD_READ) = socket_error then begin ErrMsg ( 'WSAAsyncSelect Failed:' IntToStr (WSAGetLastError)); closesocket (sock); exit; end; remote.sin_family: = AF_INET; remote.sin_port: = Htons (mcastport); remote.sin_addr.s_addr: = inet_addr (mcastaddr);

Mcast.imr_multiaddr.s_addr: = remote.sin_addr.s_addr; mcast.imr_interface.s_addr: = local.sin_addr.s_addr;

if setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, @mcast, sizeof (mcast)) = SOCKET_ERROR then begin ErrMsg ( 'setsockopt Failed:' IntToStr (WSAGetLastError)); closesocket (sock); exit; end;

Opt: = 8; if setsockopt (sock, IPPROTO_IP, IP_MULTICAST_TTL, @opt, sizeof (opt)) = SOCKET_ERROR then begin ErrMsg ( 'setsockopt Failed:' IntToStr (WSAGetLastError)); closesocket (sock); exit; end;

// The UDP data to be sent FillChar (bufsend, 32, 'c');

IF BSENDER THEN for i: = 0 to 3 do begin RET: = Sendto (Sock, Bufsend [0], 32, 0, Remote, SizeOf (Remote)); if Ret = Socket_ERROR THEN BEGIN Errmsg ('Sendto Failed:' INTSTOSTR (WsagetLastError); // CloseSocket (SOCK); BREAK; End; End;

// CloseSocket (SOCK); // CloseSocket (SOCKM);

I also want to experiment with Winsock2 multicast, but I found that I don't support Wide Area Network, I am not interested ~~~

转载请注明原文地址:https://www.9cbs.com/read-40281.html

New Post(0)