Re: [twsocket] Multicast question
Hi, this is an example of TUDP class that can be used in multicast (receive and transmit). This unit depends by other units, but you can easily extract what you need. Note that I set multicast ttl. I hope it will be usefull for you, Bye Emanuele unit ewPlatform_UDP; interface uses ewPlatform_Lists, ewPlatform_Logger, ewPlatform_Memory, ewPlatform_Packets, ewPlatform_Strings, OverByteIcsWsocket, WinSock, Windows, SysUtils, Classes; type TOnUDPError=procedure(aError:integer)of object; TOnUDPDataSent=procedure(aInteger:integer)of object; TOnUDPDataAvailable=procedure(aSender:TObject;aData:pointer;aSize:integer;aPeerIP:string;aPeerPort:integer) of object; TUDP=class protected fWS:TWSocket; fSent:boolean; fPeerSrc:TSockAddr; fPeerSrcLen:integer; fRxData:pointer; fRxSize:integer; fWhiteList:TStringList; fBlackList:TStringList; fTxList:TStringList; procedure DataAvailable(aSender:TObject;aError:word); procedure DataSent(aSender:TObject;aErrCode:word); procedure DnsLookupDone(aSender:TObject;aErrCode:word); procedure ChangeState(aSender:TObject;aOldState,aNewState:TSocketState); procedure Send; function GetStarted:boolean; public pLocalIP:string; pLocalPort:integer; pPeerIP:string; pPeerPort:integer; pMultiThreaded:boolean; pMulticast:boolean; pUseWhiteList:boolean; pUseBlackList:boolean; pMulticastTTL:integer; pGetPeerByData:boolean; pOnDataSent:TOnUDPDataSent; pOnDataAvailable:TOnUDPDataAvailable; pOnError:TOnUDPError; pOnChangeState:TChangeState; constructor Create; destructor Destroy;override; procedure StartRx; procedure SetTx; procedure Disconnect; procedure Connect; function Add2WhiteList(aIPPort:string):boolean; function Add2BlackList(aIPPort:string):boolean; procedure DeleteFromWhiteList(aIPPort:string); procedure DeleteFromBlackList(aIPPort:string); procedure ClearWhiteList; procedure ClearBlackList; function NewData(aData:pointer;aSize:integer):integer; property pStarted:boolean read GetStarted; end; implementation //-- //TUDP //-- constructor TUDP.Create; begin inherited Create; TLogger.Log('UDP socket creation threadID='+inttostr(GetCurrentThreadID),lsVerbose,true); fWS:=TWSocket.Create(nil); fWS.OnDataAvailable:=DataAvailable; fWS.OnDataSent:=DataSent; fWS.OnDnsLookupDone:=DnsLookUpDone; fWS.OnChangeState:=ChangeState; fWS.Proto:='udp'; pGetPeerByData:=false; fTxList:=TStringList.Create; fSent:=false; pLocalIP:='0.0.0.0'; pLocalPort:=0; pPeerIP:=''; pPeerPort:=0; pMultiThreaded:=false; pMulticast:=false; pMulticastTTL:=32; fWhiteList:=TStringList.Create; fBlackList:=TStringList.Create; pUseWhiteList:=false; pUseBlackList:=false; fRxSize:=256*1024; getmem(fRxData,fRxSize); fPeerSrcLen:=sizeof(fPeerSrc); end; destructor TUDP.Destroy; begin Disconnect; FreeObjAndNil(fWS); FreeMemAndNil(fRxData); FreeObjAndNil(fWhiteList); FreeObjAndNil(fBlackList); FreeList(fTxList); FreeObjAndNil(fTxList); inherited Destroy; end; procedure TUDP.StartRx; begin Disconnect; pLocalIP:=trim(pLocalIP); if (pLocalIP'')and(pLocalPort0) then begin fWS.Proto:='udp'; fWS.MultiThreaded:=pMultiThreaded; fWS.Addr:=pLocalIP; fWS.Port:=inttostr(pLocalPort); if fWS.Addr='' then fWS.Addr:='0.0.0.0'; if pMulticast then begin fWS.MultiCast:=true; fWS.ReuseAddr:=true; fWS.MultiCastAddrStr:=pPeerIP; end; fWS.Listen; fWS.SocketRcvBufSize:=fRXSize; if pMulticast then setsockopt(fWS.HSocket,IPPROTO_IP,IP_MULTICAST_TTL,@pMulticastTTL,sizeof(pMulticastTTL)); end else TLogger.Log('UDP socket start rx error LocalIP='+pLocalIP+' LocalPort='+inttostr(pLocalPort),lsError,true); end; function TUDP.GetStarted:boolean; begin result:=false; if (assigned(fWS)) then result:=(fWS.State=wsListening)or(fWS.State=wsConnected); end; procedure TUDP.SetTx; var lRes:boolean; begin pPeerIP:=trim(pPeerIP); lRes:=(pPeerIp'')and(pPeerPort0); TLogger.Log('UDP socket set tx PeerIP='+pPeerIP+' PeerPort='+inttostr(pPeerPort)+'... '+Boolean2Str(lRes,'ok','ko'),Boolean2Severity(lRes,lsVerbose,lsError),true); fPeerSrc.sin_family:=AF_INET; fPeerSrc.sin_addr.s_addr:=WSocket_inet_addr(AnsiString(pPeerIP)); fPeerSrc.sin_port:=htons(word(pPeerPort)); fSent:=false; FreeList(fTxList); end; procedure TUDP.Disconnect; begin fWS.Shutdown(SD_BOTH); fWS.Close; FreeList(fTxList); fSent:=false; end; procedure TUDP.Connect; begin Disconnect; fWS.Proto:='udp'; fWS.MultiThreaded:=pMultiThreaded; fWS.LocalAddr:=pLocalIP; fWS.Addr:=trim(pPeerIP); fWS.Port:=inttostr(pPeerPort); SetTx; if (fWS.Addr'')and(pPeerPort0) then
Re: [twsocket] Multicast question
Hi Emanuele, Thanks for the examples I actually have a working application with multicast, but I was wondering if it was correct, because I tested it with a switch that manages IGMP and no multicast groups were being created. I saw that you set TTL to 32, why? The default 1 would not work? I know that 1 would work just on local network (My case) What I´m confused is with the group creation, how my ICS based application with create a multicast group that is visible for switches (By using IGMP) and switches can manage this multicast distribution? Without that, multicast would be just like broadcast. I saw on TWSocket source that when I start listening it actually sets the socket to ADD_MEMBERSHIP tp the group, this may be OK for receiving data, but I´m confused about sending multicast data, because the sender is the one that was supposed to create the multicast group right? But to send multicast data on network I need to use Connect method instead of Listen from the TWSocket, in this case, on Connect method I didn´t find a mention to ADD_MEMBERSHIP or creating a multicast group. Thanks Eric -Mensagem Original- From: emanuele bizzarri Sent: Thursday, May 26, 2011 3:41 AM To: twsocket@elists.org Subject: Re: [twsocket] Multicast question Hi, this is an example of TUDP class that can be used in multicast (receive and transmit). This unit depends by other units, but you can easily extract what you need. Note that I set multicast ttl. I hope it will be usefull for you, Bye Emanuele unit ewPlatform_UDP; interface uses ewPlatform_Lists, ewPlatform_Logger, ewPlatform_Memory, ewPlatform_Packets, ewPlatform_Strings, OverByteIcsWsocket, WinSock, Windows, SysUtils, Classes; type TOnUDPError=procedure(aError:integer)of object; TOnUDPDataSent=procedure(aInteger:integer)of object; TOnUDPDataAvailable=procedure(aSender:TObject;aData:pointer;aSize:integer;aPeerIP:string;aPeerPort:integer) of object; TUDP=class protected fWS:TWSocket; fSent:boolean; fPeerSrc:TSockAddr; fPeerSrcLen:integer; fRxData:pointer; fRxSize:integer; fWhiteList:TStringList; fBlackList:TStringList; fTxList:TStringList; procedure DataAvailable(aSender:TObject;aError:word); procedure DataSent(aSender:TObject;aErrCode:word); procedure DnsLookupDone(aSender:TObject;aErrCode:word); procedure ChangeState(aSender:TObject;aOldState,aNewState:TSocketState); procedure Send; function GetStarted:boolean; public pLocalIP:string; pLocalPort:integer; pPeerIP:string; pPeerPort:integer; pMultiThreaded:boolean; pMulticast:boolean; pUseWhiteList:boolean; pUseBlackList:boolean; pMulticastTTL:integer; pGetPeerByData:boolean; pOnDataSent:TOnUDPDataSent; pOnDataAvailable:TOnUDPDataAvailable; pOnError:TOnUDPError; pOnChangeState:TChangeState; constructor Create; destructor Destroy;override; procedure StartRx; procedure SetTx; procedure Disconnect; procedure Connect; function Add2WhiteList(aIPPort:string):boolean; function Add2BlackList(aIPPort:string):boolean; procedure DeleteFromWhiteList(aIPPort:string); procedure DeleteFromBlackList(aIPPort:string); procedure ClearWhiteList; procedure ClearBlackList; function NewData(aData:pointer;aSize:integer):integer; property pStarted:boolean read GetStarted; end; implementation //-- //TUDP //-- constructor TUDP.Create; begin inherited Create; TLogger.Log('UDP socket creation threadID='+inttostr(GetCurrentThreadID),lsVerbose,true); fWS:=TWSocket.Create(nil); fWS.OnDataAvailable:=DataAvailable; fWS.OnDataSent:=DataSent; fWS.OnDnsLookupDone:=DnsLookUpDone; fWS.OnChangeState:=ChangeState; fWS.Proto:='udp'; pGetPeerByData:=false; fTxList:=TStringList.Create; fSent:=false; pLocalIP:='0.0.0.0'; pLocalPort:=0; pPeerIP:=''; pPeerPort:=0; pMultiThreaded:=false; pMulticast:=false; pMulticastTTL:=32; fWhiteList:=TStringList.Create; fBlackList:=TStringList.Create; pUseWhiteList:=false; pUseBlackList:=false; fRxSize:=256*1024; getmem(fRxData,fRxSize); fPeerSrcLen:=sizeof(fPeerSrc); end; destructor TUDP.Destroy; begin Disconnect; FreeObjAndNil(fWS); FreeMemAndNil(fRxData); FreeObjAndNil(fWhiteList); FreeObjAndNil(fBlackList); FreeList(fTxList); FreeObjAndNil(fTxList); inherited Destroy; end; procedure TUDP.StartRx; begin Disconnect; pLocalIP:=trim(pLocalIP); if (pLocalIP'')and(pLocalPort0) then begin fWS.Proto:='udp'; fWS.MultiThreaded:=pMultiThreaded; fWS.Addr:=pLocalIP; fWS.Port:=inttostr(pLocalPort); if fWS.Addr='' then fWS.Addr:='0.0.0.0'; if pMulticast then begin fWS.MultiCast:=true; fWS.ReuseAddr:=true; fWS.MultiCastAddrStr:=pPeerIP; end; fWS.Listen; fWS.SocketRcvBufSize:=fRXSize; if pMulticast then
[twsocket] Repair Connection?
I use the TWSocket and TWSocketServer components to implement communication between a host and client computers. Sometimes things get in a state where the client will fail to connect to the host no matter how many times it is attempted. Exiting the software, repairing the local area connection (under XP) and restarting the software *always* solved this problem. Now there is no repair function anymore in Windows and I'm wondering anyway if there is a function in the components that can be called that might clear this problem up? Is anyone else familiar with this problem or is it just me? David -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be