From owner-freebsd-questions@FreeBSD.ORG Fri Dec 18 16:18:57 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 58F1A1065739 for ; Fri, 18 Dec 2009 16:18:57 +0000 (UTC) (envelope-from dxnada@gmail.com) Received: from mail-yw0-f172.google.com (mail-yw0-f172.google.com [209.85.211.172]) by mx1.freebsd.org (Postfix) with ESMTP id 166608FC2D for ; Fri, 18 Dec 2009 16:18:56 +0000 (UTC) Received: by ywh2 with SMTP id 2so3320656ywh.27 for ; Fri, 18 Dec 2009 08:18:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=E87IBVX0KPSnoBZ04TDaGgz/x9qgjDzgBNlkYYs5wqE=; b=VRc3bN8j4by3PKdXIIriV3LHgrH73N1dyoHaDIghRw5Mh8igGMSCTjBmPfgPpTJjA/ Kk5FiT+7JkIAlvtYws8DFFz3A2kg6MOtQptnVP/Cv9OCP4w+COPtdgJzyo2QIL9+zkXo VDN+D730dwEv+BLWwFcGEbwWfnHLvT48Hwkv4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=OI8w7fGLcrRt3JtsANBfSHRawvKzBsrhy+Z/VZJVwHzBUrk3OxLD2TuOrUEXIBC7b4 Z6z5oTPu6rDrZs0hP8Mt1aF8rQZDea32e3Vic3zLGLos9UDCbmPAfcQB1SB8yqG8hGZM ulzF2wOobaTe46ODTIkq8ynMBdwnw6SgB16tk= MIME-Version: 1.0 Received: by 10.101.152.16 with SMTP id e16mr6394256ano.180.1261153136271; Fri, 18 Dec 2009 08:18:56 -0800 (PST) In-Reply-To: <200912172128.14426.pieter@degoeje.nl> References: <200912172128.14426.pieter@degoeje.nl> Date: Fri, 18 Dec 2009 10:18:55 -0600 Message-ID: From: Dex Nada To: Pieter de Goeje Content-Type: text/plain; charset=ISO-8859-1 Cc: freebsd-questions@freebsd.org Subject: Re: Joining multiple Multicast Streams X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 18 Dec 2009 16:18:57 -0000 Thank you very much. I currently use the SO_REUSEADDR flag. I will try the SO_REUSEPORT as well - makes sense. Thank again. -DxN On Thu, Dec 17, 2009 at 2:28 PM, Pieter de Goeje wrote: > On Thursday 17 December 2009 19:45:24 Dex Nada wrote: >> Hi: >> >> I am writing an application that joins a multicast stream on a specific UDP >> port. But when I run more than one instance of the same application, the >> second instance complains that the port is already in use. For example if I >> join stream 229.10.10.133:2000 on one instance and 229.10.10..134:2000 on >> another instance, the second one fails to join - I can however join it if I >> kill the first instance. I have compiled that application with SOCKET_REUSE >> option, but I wonder if I need to enable/recompile-with any special >> multicast kernel option for this to work. I do not have this problem when I >> run this code on Linux (Ubuntu 9.10) but I really want to get this working >> on FreeBSD. >> >> Thanks in advance. >> >> -DxN > > Try SO_REUSEPORT. The manpage (getsockopt(2)) specifically mentions multiple > listeners for the same multicast stream. The following pseudo C seems to work > fine: > > int reuseport = 1; > > memset(&addr, 0, sizeof(addr)); > addr.sin_family = AF_INET; > addr.sin_addr.s_addr = htonl(INADDR_ANY); > addr.sin_port = htons(port); > > mreq.imr_multiaddr.s_addr = maddr; > mreq.imr_interface.s_addr = INADDR_ANY; > > int fd = socket(AF_INET, SOCK_DGRAM, 0); > setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuseport, sizeof(reuseport)); > bind(fd, (struct sockaddr *)&addr, sizeof(addr)); > setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); > recvfrom(fd, ....); > > Good luck, > > Pieter de Goeje >