[Gambas-user] UDP socket component & UDP broadcasts

p.r. faasse faasse at ...1252...
Tue Dec 6 10:14:00 CET 2005


On Friday 02 December 2005 22:37, Daniel Campos wrote:
> Hi:
> 
> Could you send me your patch? I will add it!
> 
> thanks,
> 
> D. Campos

Hold on to your hat: here it comes: I've limited the code to the dgram_start routine of CUdpSocket.c, to not 
bother you with all sections of code that i have *not* modified. My modifications are between the <prf> and </prf> comment
lines. Note that i have misused the error-code: error code 10 is probably not the best one possible, but i have not found anything
better in the list of socket error codes. 

int dgram_start(CUDPSOCKET *mythis,int myport)
{
	int NoBlock=1;
	// <prf>
	const int on = 1;
	// </prf>
	struct sockaddr_in Srv;

	if (mythis->iStatus > 0) return 1;
	if ( (myport <0) || (myport>65535) ) return 8;

	if ( (mythis->Socket = socket(AF_INET,SOCK_DGRAM,0))<1 )
	{
		mythis->iStatus=-2;
		GB.Ref(mythis);
		GB.Post(CUdpSocket_post_error,(long)mythis);
		return 2;
	}

	//
	// <prf>
	//
	// enable broadcasts, reuseaddress
	//
	if ( setsockopt(mythis->Socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) ) {
		//
		close (mythis->Socket);
		mythis->iStatus=-10;
		GB.Ref(mythis);
		GB.Post(CUdpSocket_post_error,(long)mythis);
		return 10;
		//
	}
	//
	if ( setsockopt(mythis->Socket, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) ) {
		//
		close (mythis->Socket);
		mythis->iStatus=-10;
		GB.Ref(mythis);
		GB.Post(CUdpSocket_post_error,(long)mythis);
		return 10;
		//
	}
	//
	// </prf>
	//
	

	Srv.sin_family=AF_INET;
	Srv.sin_addr.s_addr=htonl(INADDR_ANY);
	Srv.sin_port=htons(myport);
	bzero(&(Srv.sin_zero),8);

	if ( bind (mythis->Socket,(struct sockaddr*)&Srv,sizeof(struct sockaddr)) < 0)
	{
		close (mythis->Socket);
		mythis->iStatus=-10;
		GB.Ref(mythis);
		GB.Post(CUdpSocket_post_error,(long)mythis);
		return 10;
	}

	mythis->iStatus=1;
	ioctl(mythis->Socket,FIONBIO,&NoBlock);
	GB.Watch (mythis->Socket,GB_WATCH_WRITE,(void *)CUdpSocket_CallBack,(long)mythis);
	mythis->stream.desc=&UdpSocketStream;
	mythis->stream._free[0]=(long)mythis;
	return 0;
}





More information about the User mailing list