From owner-freebsd-net@FreeBSD.ORG Mon Dec 1 15:14:09 2014 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 025C9B34 for ; Mon, 1 Dec 2014 15:14:09 +0000 (UTC) Received: from a0i241.smtpcorp.com (a0i241.smtpcorp.com [216.22.15.73]) (using TLSv1.2 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id CFC38B8D for ; Mon, 1 Dec 2014 15:14:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=smtpcorp.com; s=a0_1; h=Feedback-ID:X-Smtpcorp-Track:Message-ID:Subject:To:From:Date; bh=A5a3nLMOKlF1AuXwoBSEj2ABxaJAqLjkqMDclFrx/sw=; b=JskrTzPw/8/KsyJ9RqHUw2kBDQz5k3AIhFRwSc/+adoYh6m+i+G+/+7yzAjxdZaZi3NX5exXP36LlGd+sw6tYrnIPrQyBBaC+6MDgwwq080lgulCwNa8c4uGncIieh5H1rEBGKSGduAGJ3ZdQK6/h7MwrPMQKL02FznklzpeYYc=; Date: Mon, 1 Dec 2014 07:02:25 -0800 From: Daniel Corbe To: Yuri Subject: Re: Can multiple apps listen for TCP on the same port? Message-ID: <20141201150225.GB64370@apollo.corbe.net> References: <547C5DD3.90604@rawbw.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <547C5DD3.90604@rawbw.com> User-Agent: Mutt/1.5.23 (2014-03-12) X-Smtpcorp-Track: 1bvSld4gfFdSL3.fmuUa5TuF Feedback-ID: 10661m:10661aegzayD:10661sTV54kKSiK:SMTPCORP Cc: freebsd-net@freebsd.org X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Dec 2014 15:14:09 -0000 Generally the answer to your question is no. Two applications cannot occupy the same port on the same protocol at the same time. To expand on this answer and to hopefully shed some light on why the behavior you're observing with your application is absolutely correct; the calling application (in this case, nc) has to explicitly call bind(2) before it can begin accepting connections. If that port is already in use then the call to bind(2) will fail. And in your case I suspect nc is simply choosing to silently fail. -Daniel On Mon, Dec 01, 2014 at 04:23:47AM -0800, Yuri wrote: > I have a simple 'nc' based TCP server with shell script serving http > protocol. But when I run the second instance, it never gets any > connections and never fails. > > 'nc -l PORT' first calls listen with backlog=1 and socket option > SO_REUSEADDR, and then calls accept. Once client is accepted, it closes > the listening socket and only works with this connection. > > Why the second nc instance still never accepts any connections, once the > first connection is accepted, and first listening socket is closed? > > When one listening socket is closed, shouldn't other listening sockets > on the same port be accepting connections? It looks like the whole port > is blocked without even having the listening socket on it by one alive > connection (which after it was accepted doesn't have much to do with the > original port). > > Yuri > _______________________________________________ > freebsd-net@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-net > To unsubscribe, send any mail to "freebsd-net-unsubscribe@freebsd.org" >