Wisozk Holo 🚀

How do SOREUSEADDR and SOREUSEPORT differ

February 16, 2025

How do SOREUSEADDR and SOREUSEPORT differ

Web programming tin beryllium a thorny concern, fraught with refined errors and surprising behaviour. 1 communal origin of disorder for builders revolves about socket choices, particularly SO_REUSEADDR and SO_REUSEPORT. Piece their names propose akin performance, these choices code chiseled situations and knowing their nuances is important for gathering strong and businesslike web purposes. Misunderstanding these choices tin pb to irritating debugging periods and possibly unstable functions. This article delves into the variations betwixt SO_REUSEADDR and SO_REUSEPORT, offering broad explanations and applicable examples to aid you navigate the complexities of socket programming.

What is SO_REUSEADDR?

SO_REUSEADDR is a socket action that permits a socket to hindrance to an code and larboard that is already successful usage, however successful a TIME_WAIT government. This government happens last a socket has been closed however the working scheme inactive holds onto the code and larboard for a abbreviated play, sometimes to guarantee that immoderate delayed packets are dealt with appropriately. With out SO_REUSEADDR, trying to hindrance to this code and larboard would consequence successful an mistake. This is peculiarly utile successful eventualities wherever you’re restarting a server exertion rapidly and don’t privation to delay for the TIME_WAIT play to expire.

A communal false impression is that SO_REUSEADDR permits aggregate processes to hindrance to the direct aforesaid code and larboard concurrently. This is not the lawsuit. Piece it permits binding to an code successful TIME_WAIT, it does not license aggregate progressive listeners connected the aforesaid larboard. Trying to bash truthful volition inactive consequence successful a binding mistake.

What is SO_REUSEPORT?

SO_REUSEPORT, connected the another manus, does let aggregate processes to hindrance to the aforesaid code and larboard concurrently. This is extremely utile for burden balancing crossed aggregate situations of a server exertion. The working scheme past distributes incoming connections reasonably crossed these listening processes. This performance offers improved show and redundancy, arsenic aggregate processes tin grip incoming requests.

The instauration of SO_REUSEPORT addressed limitations of former burden balancing methods, providing a much businesslike and scalable resolution. It simplifies the procedure of distributing incoming connections and removes the demand for analyzable person-abstraction burden balancing mechanisms.

Cardinal Variations and Once to Usage All

The center quality lies successful their dealing with of aggregate processes binding to the aforesaid larboard. SO_REUSEADDR offers with the TIME_WAIT government last a socket closes, piece SO_REUSEPORT permits aggregate processes to hindrance to the aforesaid larboard concurrently for burden balancing.

Present’s a array summarizing the cardinal distinctions:

Characteristic SO_REUSEADDR SO_REUSEPORT
Aggregate Processes Binding to Aforesaid Larboard Nary Sure
Handles TIME_WAIT Government Sure Sometimes Nary
Usage Lawsuit Stopping hindrance errors connected server restart Burden balancing crossed aggregate server cases

Selecting betwixt the 2 relies upon connected your circumstantial wants. If you demand to forestall binding errors connected server restarts, usage SO_REUSEADDR. If you demand to burden equilibrium crossed aggregate server cases, usage SO_REUSEPORT.

Applicable Examples

Fto’s see a existent-planet script. Ideate you’re moving a net server that wants to grip a advanced measure of requests. Utilizing SO_REUSEPORT permits you to motorboat aggregate situations of the net server, all sure to the aforesaid larboard. The working scheme past distributes incoming connections crossed these situations, enhancing show and resilience.

Different illustration is a server exertion that often restarts. Utilizing SO_REUSEADDR ensures that the exertion tin rapidly hindrance to the desired larboard equal if the former case late closed and the larboard is inactive successful TIME_WAIT.

Seat this insightful article connected Stack Overflow for much particulars.

FAQ

Q: Tin I usage some SO_REUSEADDR and SO_REUSEPORT unneurotic?

A: Sure, it’s imaginable to usage some choices concurrently. Nevertheless, realize their chiseled functionalities and however they work together inside your circumstantial working scheme.

Q: Is SO_REUSEPORT supported connected each working techniques?

A: Nary, SO_REUSEPORT is not universally supported. Cheque your OS documentation for compatibility.

  • Realize the circumstantial wants of your exertion.
  • Take the due socket action primarily based connected whether or not you demand to grip TIME_WAIT oregon execute burden balancing.
  1. Place your exertion’s necessities.
  2. Seek the advice of your working scheme’s documentation.
  3. Instrumentality the chosen socket action successful your codification.

Infographic Placeholder: (Ocular cooperation of SO_REUSEADDR vs. SO_REUSEPORT)

By knowing the nuances of SO_REUSEADDR and SO_REUSEPORT, you tin make much strong and businesslike web functions. Cautious information of these choices volition aid you debar communal pitfalls and physique extremely performant server infrastructure. Research additional assets and documentation for your circumstantial working scheme to maestro these important socket choices and elevate your web programming expertise. Larn much astir precocious socket programming strategies present. Dive deeper into the planet of web programming and unlock the afloat possible of your functions. See exploring associated matters similar socket programming champion practices and precocious web architectures for enhanced show and reliability. For additional speechmaking connected web programming and socket choices, mention to these assets: Linux Socket Male Leaf, Home windows Sockets, and Beej’s Usher to Web Programming.

Question & Answer :
The male pages and programmer documentations for the socket choices SO_REUSEADDR and SO_REUSEPORT are antithetic for antithetic working programs and frequently extremely complicated. Any working techniques don’t equal person the action SO_REUSEPORT. The WWW is afloat of contradicting accusation concerning this taxable and frequently you tin discovery accusation that is lone actual for 1 socket implementation of a circumstantial working scheme, which whitethorn not equal beryllium explicitly talked about successful the matter.

Truthful however precisely is SO_REUSEADDR antithetic than SO_REUSEPORT?

Are methods with out SO_REUSEPORT much constricted?

And what precisely is the anticipated behaviour if I usage both 1 connected antithetic working techniques?

Invited to the fantastic planet of portability… oregon instead the deficiency of it. Earlier we commencement analyzing these 2 choices successful item and return a deeper expression however antithetic working techniques grip them, it ought to beryllium famous that the BSD socket implementation is the parent of each socket implementations. Fundamentally each another programs copied the BSD socket implementation astatine any component successful clip (oregon astatine slightest its interfaces) and past began evolving it connected their ain. Of class the BSD socket implementation was developed arsenic fine astatine the aforesaid clip and frankincense methods that copied it future acquired options that had been missing successful programs that copied it earlier. Knowing the BSD socket implementation is the cardinal to knowing each another socket implementations, truthful you ought to publication astir it equal if you don’t attention to always compose codification for a BSD scheme.

Location are a mates of fundamentals you ought to cognize earlier we expression astatine these 2 choices. A TCP/UDP transportation is recognized by a tuple of 5 values:

{<protocol>, <src addr>, <src larboard>, <dest addr>, <dest larboard>}

Immoderate alone operation of these values identifies a transportation. Arsenic a consequence, nary 2 connections tin person the aforesaid 5 values, other the scheme would not beryllium capable to separate these connections immoderate longer.

The protocol of a socket is fit once a socket is created with the socket() relation. The origin code and larboard are fit with the hindrance() relation. The vacation spot code and larboard are fit with the link() relation. Since UDP is a connectionless protocol, UDP sockets tin beryllium utilized with out connecting them. But it is allowed to link them and successful any instances precise advantageous for your codification and broad exertion plan. Successful connectionless manner, UDP sockets that have been not explicitly sure once information is dispatched complete them for the archetypal clip are normally routinely sure by the scheme, arsenic an unbound UDP socket can not have immoderate (answer) information. Aforesaid is actual for an unbound TCP socket, it is routinely certain earlier it volition beryllium linked.

If you explicitly hindrance a socket, it is imaginable to hindrance it to larboard zero, which means “immoderate larboard”. Since a socket can not truly beryllium sure to each present ports, the scheme volition person to take a circumstantial larboard itself successful that lawsuit (normally from a predefined, OS circumstantial scope of origin ports). A akin wildcard exists for the origin code, which tin beryllium “immoderate code” (zero.zero.zero.zero successful lawsuit of IPv4 and :: successful lawsuit of IPv6). Dissimilar successful lawsuit of ports, a socket tin truly beryllium certain to “immoderate code” which means “each origin IP addresses of each section interfaces”. If the socket is linked future connected, the scheme has to take a circumstantial origin IP code, since a socket can’t beryllium linked and astatine the aforesaid clip beryllium certain to immoderate section IP code. Relying connected the vacation spot code and the contented of the routing array, the scheme volition choice an due origin code and regenerate the “immoderate” binding with a binding to the chosen origin IP code.

By default, nary 2 sockets tin beryllium certain to the aforesaid operation of origin code and origin larboard. Arsenic agelong arsenic the origin larboard is antithetic, the origin code is really irrelevant. Binding socketA to ipA:portA and socketB to ipB:portB is ever imaginable if ipA != ipB holds actual, equal once portA == portB. E.g. socketA belongs to a FTP server programme and is certain to 192.168.zero.1:21 and socketB belongs to different FTP server programme and is sure to 10.zero.zero.1:21, some bindings volition win. Support successful head, although, that a socket whitethorn beryllium domestically certain to “immoderate code”. If a socket is sure to zero.zero.zero.zero:21, it is sure to each current section addresses astatine the aforesaid clip and successful that lawsuit nary another socket tin beryllium sure to larboard 21, careless which circumstantial IP code it tries to hindrance to, arsenic zero.zero.zero.zero conflicts with each present section IP addresses.

Thing stated truthful cold is beautiful overmuch close for each great working scheme. Issues commencement to acquire OS circumstantial once code reuse comes into drama. We commencement with BSD, since arsenic I mentioned supra, it is the parent of each socket implementations.

BSD

SO_REUSEADDR

If SO_REUSEADDR is enabled connected a socket anterior to binding it, the socket tin beryllium efficiently sure except location is a struggle with different socket sure to precisely the aforesaid operation of origin code and larboard. Present you whitethorn wonderment however is that immoderate antithetic than earlier? The key phrase is “precisely”. SO_REUSEADDR chiefly modifications the manner however wildcard addresses (“immoderate IP code”) are handled once looking for conflicts.

With out SO_REUSEADDR, binding socketA to zero.zero.zero.zero:21 and past binding socketB to 192.168.zero.1:21 volition neglect (with mistake EADDRINUSE), since zero.zero.zero.zero means “immoderate section IP code”, frankincense each section IP addresses are thought of successful usage by this socket and this consists of 192.168.zero.1, excessively. With SO_REUSEADDR it volition win, since zero.zero.zero.zero and 192.168.zero.1 are not precisely the aforesaid code, 1 is a wildcard for each section addresses and the another 1 is a precise circumstantial section code. Line that the message supra is actual careless successful which command socketA and socketB are certain; with out SO_REUSEADDR it volition ever neglect, with SO_REUSEADDR it volition ever win.

To springiness you a amended overview, fto’s brand a array present and database each imaginable mixtures:

SO_REUSEADDR socketA socketB Consequence --------------------------------------------------------------------- Connected/Disconnected 192.168.zero.1:21 192.168.zero.1:21 Mistake (EADDRINUSE) Connected/Disconnected 192.168.zero.1:21 10.zero.zero.1:21 Fine Connected/Disconnected 10.zero.zero.1:21 192.168.zero.1:21 Fine Disconnected zero.zero.zero.zero:21 192.168.1.zero:21 Mistake (EADDRINUSE) Disconnected 192.168.1.zero:21 zero.zero.zero.zero:21 Mistake (EADDRINUSE) Connected zero.zero.zero.zero:21 192.168.1.zero:21 Fine Connected 192.168.1.zero:21 zero.zero.zero.zero:21 Fine Connected/Disconnected zero.zero.zero.zero:21 zero.zero.zero.zero:21 Mistake (EADDRINUSE) 

The array supra assumes that socketA has already been efficiently certain to the code fixed for socketA, past socketB is created, both will get SO_REUSEADDR fit oregon not, and eventually is sure to the code fixed for socketB. Consequence is the consequence of the hindrance cognition for socketB. If the archetypal file says Connected/Disconnected, the worth of SO_REUSEADDR is irrelevant to the consequence.

Fine, SO_REUSEADDR has an consequence connected wildcard addresses, bully to cognize. But that isn’t its lone consequence it has. Location is different fine identified consequence which is besides the ground wherefore about group usage SO_REUSEADDR successful server applications successful the archetypal spot. For the another crucial usage of this action we person to return a deeper expression connected however the TCP protocol plant.

If a TCP socket is being closed, usually a three-manner handshake is carried out; the series is referred to as FIN-ACK. The job present is, that the past ACK of that series whitethorn person arrived connected the another broadside oregon it whitethorn not person arrived and lone if it has, the another broadside besides considers the socket arsenic being full closed. To forestall re-utilizing an code+larboard operation, that whitethorn inactive beryllium thought of unfastened by any distant equal, the scheme volition not instantly see a socket arsenic asleep last sending the past ACK however alternatively option the socket into a government generally referred to arsenic TIME_WAIT. It tin beryllium successful that government for minutes (scheme babelike mounting). Connected about methods you tin acquire about that government by enabling lingering and mounting a linger clip of zero1 however location is nary warrant that this is ever imaginable, that the scheme volition ever award this petition, and equal if the scheme honors it, this causes the socket to beryllium closed by a reset (RST), which is not ever a large thought. To larn much astir linger clip, person a expression astatine my reply astir this subject.

The motion is, however does the scheme dainty a socket successful government TIME_WAIT? If SO_REUSEADDR is not fit, a socket successful government TIME_WAIT is thought of to inactive beryllium sure to the origin code and larboard and immoderate effort to hindrance a fresh socket to the aforesaid code and larboard volition neglect till the socket has truly been closed. Truthful don’t anticipate that you tin rebind the origin code of a socket instantly last closing it. Successful about instances this volition neglect. Nevertheless, if SO_REUSEADDR is fit for the socket you are attempting to hindrance, different socket sure to the aforesaid code and larboard successful government TIME_WAIT is merely ignored, last each its already “fractional asleep”, and your socket tin hindrance to precisely the aforesaid code with out immoderate job. Successful that lawsuit it performs nary function that the another socket whitethorn person precisely the aforesaid code and larboard. Line that binding a socket to precisely the aforesaid code and larboard arsenic a dying socket successful TIME_WAIT government tin person sudden, and normally undesired, broadside results successful lawsuit the another socket is inactive “astatine activity”, however that is past the range of this reply and thankfully these broadside results are instead uncommon successful pattern.

Location is 1 last happening you ought to cognize astir SO_REUSEADDR. Every little thing written supra volition activity arsenic agelong arsenic the socket you privation to hindrance to has code reuse enabled. It is not essential that the another socket, the 1 which is already certain oregon is successful a TIME_WAIT government, besides had this emblem fit once it was sure. The codification that decides if the hindrance volition win oregon neglect lone inspects the SO_REUSEADDR emblem of the socket fed into the hindrance() call, for each another sockets inspected, this emblem is not equal seemed astatine.

SO_REUSEPORT

SO_REUSEPORT is what about group would anticipate SO_REUSEADDR to beryllium. Fundamentally, SO_REUSEPORT permits you to hindrance an arbitrary figure of sockets to precisely the aforesaid origin code and larboard arsenic agelong arsenic each anterior sure sockets besides had SO_REUSEPORT fit earlier they have been sure. If the archetypal socket that is sure to an code and larboard does not person SO_REUSEPORT fit, nary another socket tin beryllium sure to precisely the aforesaid code and larboard, careless if this another socket has SO_REUSEPORT fit oregon not, till the archetypal socket releases its binding once more. Dissimilar successful lawsuit of SO_REUSEADDR the codification dealing with SO_REUSEPORT volition not lone confirm that the presently certain socket has SO_REUSEPORT fit however it volition besides confirm that the socket with a conflicting code and larboard had SO_REUSEPORT fit once it was certain.

SO_REUSEPORT does not connote SO_REUSEADDR. This means if a socket did not person SO_REUSEPORT fit once it was certain and different socket has SO_REUSEPORT fit once it is certain to precisely the aforesaid code and larboard, the hindrance fails, which is anticipated, however it besides fails if the another socket is already dying and is successful TIME_WAIT government. To beryllium capable to hindrance a socket to the aforesaid addresses and larboard arsenic different socket successful TIME_WAIT government requires both SO_REUSEADDR to beryllium fit connected that socket oregon SO_REUSEPORT essential person been fit connected some sockets anterior to binding them. Of class it is allowed to fit some, SO_REUSEPORT and SO_REUSEADDR, connected a socket.

Location is not overmuch much to opportunity astir SO_REUSEPORT another than that it was added future than SO_REUSEADDR, that’s wherefore you volition not discovery it successful galore socket implementations of another techniques, which “forked” the BSD codification earlier this action was added, and that location was nary manner to hindrance 2 sockets to precisely the aforesaid socket code successful BSD anterior to this action.

About group cognize that hindrance() whitethorn neglect with the mistake EADDRINUSE, nevertheless, once you commencement enjoying about with code reuse, you whitethorn tally into the unusual occupation that link() fails with that mistake arsenic fine. However tin this beryllium? However tin a distant code, last each that’s what link provides to a socket, beryllium already successful usage? Connecting aggregate sockets to precisely the aforesaid distant code has ne\’er been a job earlier, truthful what’s going incorrect present?

Arsenic I mentioned connected the precise apical of my answer, a transportation is outlined by a tuple of 5 values, retrieve? And I besides stated, that these 5 values essential beryllium alone other the scheme can not separate 2 connections immoderate longer, correct? Fine, with code reuse, you tin hindrance 2 sockets of the aforesaid protocol to the aforesaid origin code and larboard. That means 3 of these 5 values are already the aforesaid for these 2 sockets. If you present attempt to link some of these sockets besides to the aforesaid vacation spot code and larboard, you would make 2 linked sockets, whose tuples are perfectly an identical. This can not activity, astatine slightest not for TCP connections (UDP connections are nary existent connections anyhow). If information arrived for both 1 of the 2 connections, the scheme might not archer which transportation the information belongs to. Astatine slightest the vacation spot code oregon vacation spot larboard essential beryllium antithetic for both transportation, truthful that the scheme has nary job to place to which transportation incoming information belongs to.

Truthful if you hindrance 2 sockets of the aforesaid protocol to the aforesaid origin code and larboard and attempt to link them some to the aforesaid vacation spot code and larboard, link() volition really neglect with the mistake EADDRINUSE for the 2nd socket you attempt to link, which means that a socket with an similar tuple of 5 values is already linked.

Multicast Addresses

About group disregard the information that multicast addresses be, however they bash be. Piece unicast addresses are utilized for 1-to-1 connection, multicast addresses are utilized for 1-to-galore connection. About group obtained alert of multicast addresses once they realized astir IPv6 however multicast addresses besides existed successful IPv4, equal although this characteristic was ne\’er wide utilized connected the national Net.

The that means of SO_REUSEADDR adjustments for multicast addresses arsenic it permits aggregate sockets to beryllium certain to precisely the aforesaid operation of origin multicast code and larboard. Successful another phrases, for multicast addresses SO_REUSEADDR behaves precisely arsenic SO_REUSEPORT for unicast addresses. Really, the codification treats SO_REUSEADDR and SO_REUSEPORT identically for multicast addresses, that means you might opportunity that SO_REUSEADDR implies SO_REUSEPORT for each multicast addresses and the another manner circular.

FreeBSD/OpenBSD/NetBSD

Each these are instead advanced forks of the first BSD codification, that’s wherefore they each 3 message the aforesaid choices arsenic BSD and they besides behave the aforesaid manner arsenic successful BSD.

macOS (MacOS X)

Astatine its center, macOS is merely a BSD-kind UNIX named “Darwin”, primarily based connected a instead advanced fork of the BSD codification (BSD four.three), which was past future connected equal re-synchronized with the (astatine that clip actual) FreeBSD 5 codification basal for the Mac OS 10.three merchandise, truthful that Pome may addition afloat POSIX compliance (macOS is POSIX licensed). Contempt having a microkernel astatine its center ("Mach"), the remainder of the kernel ("XNU") is fundamentally conscionable a BSD kernel, and that’s wherefore macOS presents the aforesaid choices arsenic BSD and they besides behave the aforesaid manner arsenic successful BSD.

iOS / watchOS / tvOS

iOS is conscionable a macOS fork with a somewhat modified and trimmed kernel, slightly stripped behind person abstraction toolset and a somewhat antithetic default model fit. watchOS and tvOS are iOS forks, that are stripped behind equal additional (particularly watchOS). To my champion cognition they each behave precisely arsenic macOS does.

Linux

Linux < three.9

Anterior to Linux three.9, lone the action SO_REUSEADDR existed. This action behaves mostly the aforesaid arsenic successful BSD with 2 crucial exceptions:

  1. Arsenic agelong arsenic a listening (server) TCP socket is sure to a circumstantial larboard, the SO_REUSEADDR action is wholly ignored for each sockets concentrating on that larboard. Binding a 2nd socket to the aforesaid larboard is lone imaginable if it was besides imaginable successful BSD with out having SO_REUSEADDR fit. E.g. you can not hindrance to a wildcard code and past to a much circumstantial 1 oregon the another manner circular, some is imaginable successful BSD if you fit SO_REUSEADDR. What you tin bash is you tin hindrance to the aforesaid larboard and 2 antithetic non-wildcard addresses, arsenic that’s ever allowed. Successful this facet Linux is much restrictive than BSD.
  2. The 2nd objection is that for case sockets, this action behaves precisely similar SO_REUSEPORT successful BSD, arsenic agelong arsenic some had this emblem fit earlier they had been certain. The ground for permitting that was merely that it is crucial to beryllium capable to hindrance aggregate sockets to precisely to the aforesaid UDP socket code for assorted protocols and arsenic location utilized to beryllium nary SO_REUSEPORT anterior to three.9, the behaviour of SO_REUSEADDR was altered accordingly to enough that spread. Successful that facet Linux is little restrictive than BSD.

Linux >= three.9

Linux three.9 added the action SO_REUSEPORT to Linux arsenic fine. This action behaves precisely similar the action successful BSD and permits binding to precisely the aforesaid code and larboard figure arsenic agelong arsenic each sockets person this action fit anterior to binding them.

But, location are inactive 2 variations to SO_REUSEPORT connected another techniques:

  1. To forestall “larboard hijacking”, location is 1 particular regulation: Each sockets that privation to stock the aforesaid code and larboard operation essential be to processes that stock the aforesaid effectual person ID! Truthful 1 person can’t “bargain” ports of different person. This is any particular magic to slightly compensate for the lacking SO_EXCLBIND/SO_EXCLUSIVEADDRUSE flags.
  2. Moreover the kernel performs any “particular magic” for SO_REUSEPORT sockets that isn’t recovered successful another working techniques: For UDP sockets, it tries to administer datagrams evenly, for TCP listening sockets, it tries to administer incoming link requests (these accepted by calling judge()) evenly crossed each the sockets that stock the aforesaid code and larboard operation. Frankincense an exertion tin easy unfastened the aforesaid larboard successful aggregate kid processes and past usage SO_REUSEPORT to acquire a precise cheap burden balancing.

Android

Equal although the entire Android scheme is slightly antithetic from about Linux distributions, astatine its center plant a somewhat modified Linux kernel, frankincense all the things that applies to Linux ought to use to Android arsenic fine.

Home windows

Home windows lone is aware of the SO_REUSEADDR action, location is nary SO_REUSEPORT. Mounting SO_REUSEADDR connected a socket successful Home windows behaves similar mounting SO_REUSEPORT and SO_REUSEADDR connected a socket successful BSD, with 1 objection:

Anterior to Home windows 2003, a socket with SO_REUSEADDR might ever been certain to precisely the aforesaid origin code and larboard arsenic an already sure socket, equal if the another socket did not person this action fit once it was certain. This behaviour allowed an exertion “to bargain” the linked larboard of different exertion. Pointless to opportunity that this has great safety implications!

Microsoft realized that and added different crucial socket action: SO_EXCLUSIVEADDRUSE. Mounting SO_EXCLUSIVEADDRUSE connected a socket makes certain that if the binding succeeds, the operation of origin code and larboard is owned solely by this socket and nary another socket tin hindrance to them, not equal if it has SO_REUSEADDR fit.

This default behaviour was modified archetypal successful Home windows 2003, Microsoft calls that “Enhanced Socket Safety” (comic sanction for a behaviour that is default connected each another great working techniques). For much particulars conscionable sojourn this leaf. Location are 3 tables: The archetypal 1 exhibits the classical behaviour (inactive successful usage once utilizing compatibility modes!), the 2nd 1 reveals the behaviour of Home windows 2003 and ahead once the hindrance() calls are made by the aforesaid person, and the 3rd 1 once the hindrance() calls are made by antithetic customers.

Solaris

Solaris is the successor of SunOS. SunOS was primitively primarily based connected a fork of BSD, SunOS 5 and future was based mostly connected a fork of SVR4, nevertheless SVR4 is a merge of BSD, Scheme V, and Xenix, truthful ahead to any grade Solaris is besides a BSD fork, and a instead aboriginal 1. Arsenic a consequence Solaris lone is aware of SO_REUSEADDR, location is nary SO_REUSEPORT. The SO_REUSEADDR behaves beautiful overmuch the aforesaid arsenic it does successful BSD. Arsenic cold arsenic I cognize location is nary manner to acquire the aforesaid behaviour arsenic SO_REUSEPORT successful Solaris, that means it is not imaginable to hindrance 2 sockets to precisely the aforesaid code and larboard.

Akin to Home windows, Solaris has an action to springiness a socket an unique binding. This action is named SO_EXCLBIND. If this action is fit connected a socket anterior to binding it, mounting SO_REUSEADDR connected different socket has nary consequence if the 2 sockets are examined for an code struggle. E.g. if socketA is certain to a wildcard code and socketB has SO_REUSEADDR enabled and is certain to a non-wildcard code and the aforesaid larboard arsenic socketA, this hindrance volition usually win, until socketA had SO_EXCLBIND enabled, successful which lawsuit it volition neglect careless the SO_REUSEADDR emblem of socketB.

Another Techniques

Successful lawsuit your scheme is not listed supra, I wrote a small trial programme that you tin usage to discovery retired however your scheme handles these 2 choices. Besides if you deliberation my outcomes are incorrect, delight archetypal tally that programme earlier posting immoderate feedback and perchance making mendacious claims.

Each that the codification requires to physique is a spot POSIX API (for the web components) and a C99 compiler (really about non-C99 compiler volition activity arsenic fine arsenic agelong arsenic they message inttypes.h and stdbool.h; e.g. gcc supported some agelong earlier providing afloat C99 activity).

Each that the programme wants to tally is that astatine slightest 1 interface successful your scheme (another than the section interface) has an IP code assigned and that a default path is fit which makes use of that interface. The programme volition stitchery that IP code and usage it arsenic the 2nd “circumstantial code”.

It checks each imaginable combos you tin deliberation of:

  • TCP and UDP protocol
  • Average sockets, perceive (server) sockets, multicast sockets
  • SO_REUSEADDR fit connected socket1, socket2, oregon some sockets
  • SO_REUSEPORT fit connected socket1, socket2, oregon some sockets
  • Each code combos you tin brand retired of zero.zero.zero.zero (wildcard), 127.zero.zero.1 (circumstantial code), and the 2nd circumstantial code recovered astatine your capital interface (for multicast it’s conscionable 224.1.2.three successful each checks)

and prints the outcomes successful a good array. It volition besides activity connected techniques that don’t cognize SO_REUSEPORT, successful which lawsuit this action is merely not examined.

What the programme can not easy trial is however SO_REUSEADDR acts connected sockets successful TIME_WAIT government arsenic it’s precise difficult to unit and support a socket successful that government. Happily about working techniques appears to merely behave similar BSD present and about of the clip programmers tin merely disregard the beingness of that government.

Present’s the codification (I can’t see it present, solutions person a dimension bounds and the codification would propulsion this answer complete the bounds).