Sockets, multicast/broadcast, UDP, TCP, SO_REUSEADDR, SO_REUSEPORT and SO_EXCLUSIVEADDRUSE

EDIT: the topic was moved to https://social.msdn.microsoft.com/Forums/vstudio/en-US/6b0fbcbc-1139-4d7a-a62a-a5b16a6e0229/sockets-multicastbroadcast-udp-tcp-soreuseaddr-soreuseport-and-soexclusiveaddruse?forum=wcf .
Hello,
in the project Dao (https://github.com/daokoder/dao) we got stuck upon a problem with a wrapper interface for bind(), which should be multiplatform (work on all Windows versions from XP higher and also on BSDs and also other POSIX systems) and offer the
following two options:
1) A safe bind of one socket, one address (specific or wildcard) and one port such that no other process (disregarding the user the process runs under and disregarding user privileges) could be successful with a call to bind() on the same socket with the
same address and the same port.
2) A bind with allowing multicast/broadcast, reuse of address, port and all these disregarding the state of the socket (e.g. TIME_WAIT) and disregarding the type of connection (UDP, TCP). In other words, the behavior should be the same a the behavior of
BSD sockets in all cases and under all conditions.
The main differences between BSD and Windows are conveniently described on http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t . According to that, one would say that a solution:
1) For BSD "0" and for Windows "SO_EXCLUSIVEADDRUSE"
2) For BSD "SO_REUSEPORT | SO_REUSEADDR" and for Windows "SO_REUSEADDR"
is enough. But it's not true. For example due to Enhanced Socket Security and their behavior described in the table http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621(v=vs.85).aspx (after first call to bind() with wildcard address and SO_REUSEADDR
set, a second call to bind() with non-wildcard address and SO_REUSEADDR set will fail with error WSAEACCES).
A solution might be to gather all the needed information (like user id, if there are any processes using that particular socket and if a call to connect() was already issued in case of TCP) and according to this information use the right mix of options SO_REUSEADDR,
SO_EXCLUSIVEADDRUSE and others if needed. We didn't find though enough documentation about where to get all the needed information. Note that this solution would need an atomic query on socket (for all the information) in conjunction with a call to bind().
Another solution could be to call bind() with different flags and decide upon the return error codes. This is though a nasty hack we would like to avoid.
The question for you is, if you happen to know about any better solution than the ones mentioned above. There is a public discussion about this problem starting with comment https://github.com/daokoder/dao/issues/243#issuecomment-60434078 .
Thank you in advance!

Hi dao_lang_rulezz,
Thanks for posting in the TechNet Forum.
As my point of viewer, this requirement is related to program developing issue.
This forum is related to Windows 8.1 Client.
To receive better support, it is recommend to ask in corresponding developing forum.
Best regards,
Fangzhou CHEN
Fangzhou CHEN
TechNet Community Support

Similar Messages

  • Socket for broadcast and receive?

    Hi all,
    sorry for cross post but I need an answer soon.
    The server need to broadcast an inviting message continuosly. and at the same time the socket is able to receive any messages from the client.
    I use 2 threads. One iniiated with Socket for listening and the other multicast socket with datagram.
    However, I don't think I did it the right way?????
    one thread: skt = new ServerSocket(4446);
    broadcast thread:
    MulticastSocket socket = new MulticastSocket(4446);
    InetAddress address = InetAddress.getByName("230.0.0.1");
    socket.joinGroup(address);
    DatagramPacket packet;
    any suggestions?
    Any souce code for server and client?
    any documents, url?
    thanks
    andrew

    ServerSockets go with MulticastSockets like baloney and whip cream.
    ServerSockets are for connection-based sockets.
    MulticastSockets are for connection-less datagram sockets
    Pick one type based on your requirements and stick with it.
    Either ServerSockets and Sockets
    or MulticastSocket with MulticastSocket.

  • Sockets .. IO controlling TCP frame length and number of packets

    Here's the deal, I'm trying to develop a proxy for this application, an application wich is registred and can only be used from a specific host. I want to do it from another location, so I am writing a proxy.
    This should be trivial. But it has proven not to be!
    1. I am using java.net.Socet and ServerSocket
    2. I am using DataInput/OutputStream writeByte/readByte
    3. flushing after each byte OR not, same result
    It is not working, I GET the data just fine, and transmit it just fine, but the server doesnt respond.
    With a network sniffer, the only difference I can see is that with the original application (C++) only ONE TCP packet is transmitted over the network length = 52
    With my proxy TWO TCP packets is transmitted first LEN=1 and second LEN=51 for a grand total of the exact data ..
    So all other things being equal, im thinking that this must be my issue!
    Anyway I can deal with this ?
    Any ideas welcome!
    ps. tried .setBuffer .noTcpDelay etc. and gotten the same results .... perhaps nio gives more options ?
    Thanks.

    Your network "sniffer" operates at a lower level than you application. The differences in Ethernet frame that you are detecting with the network sniffer should not change the behavior of your TCP/IP application because, at the sockets programming level, the TCP stream has no boundaries.
    I have noticed the behavior in Java that you mention. I think I have seen it in the implementation of java.io.DataOutputStream.writeBytes(String), where they write the bytes one-byte-at-a-time and your get this side-effect. The source for that method looks like:
        public final void writeBytes(String s) throws IOException {
            int len = s.length();
            for (int i = 0 ; i < len ; i++) {
                out.write((byte)s.charAt(i));
            incCount(len);
        }What happens is that in that first call to out.write(), the something decides to send the one byte right away, and only on the second and third bytes does something (not sure if it is Java or TCP/IP) see that it is piling up and attempt to bunch up the bytes into a single frame.
    If you, the the Java programmer, wanted to avoid this, you would have to avoid all use of writeBytes(String s) and convert your String's to byte arrays explicitly, and then only call write(byte[] b, int off, int len), but all this should change it what you see in your network sniffer, not how the application behaves.

  • Maximum connections on UDP/TCP

    I'm trying to find how many connections could be established and handled on java TCP/UDP sockets:
    1. In TCP when I'm using a ServerSocket.accept(), how many clients can I handle, is it O/S dependent?, or is it set by the 2nd parameter when creating a new Server socket: public ServerSocket(int port, int backlog)?
    2. In UDP, when using DatagramSocket.receive(packet), how many client calls could be handled, is it also O/S dependent or could be set?
    Thank you!
    Boaz

    IP only has 65535 ports, so that limits the number of clients you can serve at a time.

  • Should (must )i give port number  Multicast / broadcast using JMF ?

    hi,
    i transmit jmf webcam usng RTP to unicast and multi-unicast ..
    now i would like to multicast .. and broadcast ..
    do i must give port number while multicast, broadcast ...
    i came to multicast to avoid give port number same in receive and transmit
    is there any way just transmit and receive webcam without giving port number ( multicat or broad cast or any other way)
    plz give exact difference between multicast and broad cast ..
    plzz...

    Broadcast is link level, so LAN level. You transmit to all others host in the subnet using the broadcast addrees of the subnet(lan).
    Multicast is over udp, so over ip. You can transmit over internet, if the router supports multicast. You need to use multicast ip addresses and of course 2 ports(udp) for each session.

  • Udp, tcp data validation

    Is there a way to do data validation that gets send over to server via udp/tcp that uses the least process to the client end?
    I was thinking about the CRC, but that put a lot of stress to the client machine, so I am wondering if there are methods that I am not aware of to accomplish this ...
    Thanks so much

    In TCP, such a packet will be automatically re-transmitted. The transport layer is responsible for error-correction. [http://en.wikipedia.org/wiki/TCP/IP_model]
    In UDP, such a packet will be either transmitted (by the router for example) or dropped. Your application should expect to loose some packet with UDP so dropping a few won't matter.
    Either way, you won't see corrupt packets and there is no pure Java way to detect a packet was corrupted.

  • IPS Tech Tip - Evasions - TCP/IP examples and handling - Sig team presentation

    Hi Customers,
    Its summer time and nothing evokes cool quite like a discussion into the TCP / IP stack and how creative attacker types try to hide attacks behind it. This presentation will feature a security researcher from our signature team and will be the first of several presentations on evastions and how the Cisco IPS handle them.
    We hope that you can make it.
    Thanks,
    -Robert
    Robert Albach invites you to attend a 30-45 minute Web seminar on the Cisco IPS internal operations using WebEx. This event requires registration.
    Topic: Cisco IPS Tech Tips - Handling Evasions
    Host: Robert Albach
    Date and Time:
    August 25, 2011 9:30 am, Central Daylight Time (Chicago, GMT-05:00)
    To register for the online event
    1. Go to https://ciscosales.webex.com/ciscosales/onstage/g.php?d=201261254&t=a&EA=ralbach%40cisco.com&ET=64ed8e6d81005252203f6671cfeee480&ETR=fb46b8799a6afe989e9a744f0fac0d77&RT=MiM3&p
    2. Click "Register".
    3. On the registration form, enter your information and then click "Submit".
    Once the host approves your registration, you will receive a confirmation email message with instructions on how to join the event.

    Sadly we did not get the recording done. The presentation and the example pcaps  however are on this forum now.
    -Robert

  • Sending udp packets using java and receiving it using c

    hi,
    Is it possible to send udp packets using java and receive the same using c??????? if yes.... plz help immediately.

    The biggest issue is data format. The JVM is big endian, with 16-bit characters. The machine running 'C' could be almost anything. A (signed!) byte array is probably the easiest unit of exchange.
    The Java program has its own techniques for storing/retrieving data to/from the byte array - and the C program has its own techniques. ASCII Strings are often the easiest to exchange - just convert the java String objects to byte array and send them.
    apaliwal1 has already given the UDP calls to send/receive the data.

  • 100083/rpc/tcp still active and was not reconfigur

    Hi,
    When I restarted my Inetd daemon , it logs the following line to /var/adm/messages/
    May 2 15:31:40 MyMachine inetd[121]: config: 100083/rpc/tcp still active and was not reconfigured.
    Can anyone explain me what does it mean?
    ( the /etc/inetd.conf contains this entry :
    100083/1 tli rpc/tcp wait root /usr/dt/bin/rpc.ttdbserverd /usr/dt/bin/rpc.ttdbserverd )
    Thanks

    Well the message means that someone else is using this port/service.
    Sometimes people who have wrappers built around a particular service
    see that if they have already have an instance of the service running and
    they try to stop/start the inetd daemon.

  • Does closing socket with close() also closes the outputStream and inputStre

    hi i have a simple query
    does closing socket with close() also closes the outputStream and inputStream() associated with the socket

    Yes.

  • What is tcp-keepalives-in and tcp-keepalives-out

    Can anyone help me out by telling the both things and the differences between
    tcp-keepalives-in and tcp-keepalives-out
    Thanks
    Irshad

    Irshad,
    If you have already not read this link...
    http://www.cisco.com/en/US/tech/tk801/tk36/technologies_tech_note09186a00801365f3.shtml

  • ITunes 11 Stops Responding to Bonjour Discovery Multicast Broadcasts, Why?  This stops the the Apple TV 3 from being able to start a new stream from the home share and the iPad remote app can no longer see the home share too.

    I'm having an issue where my Apple TV 3 and all our iPads periodically seem to loose connectivity to an iTunes home share on a Windows 7 PC.  Using a network protocol analyser on the PC I have identified that the point of failure corresponds to the ATV3 sending a bonjour discovery request and getting no reply from iTunes.  Why the ATV3 'forgets' where the home share is is possibly another issue but the root cause of the failure is that iTunes, or more specifically the mDNSResponder service, is not responding to the UDP multicast port 5353 broadcast discovery packet sent by the ATV3 or iPad.  The mDNSResponder service does start responding again after iTunes is restarted thus making the home share accessible once more, however, this frequent drop out is unacceptable and often happens after only a single TV show has been watched from the home share making the solution unworkable for a family, we simply cannot be restarting iTunes constantly it ruins the user experience completely!
    The nature of this failure indicates that the ATV3, the iPads, the host PC and network are all working correctly and the point of failure here is the lack of response to the bonjour discovery protocol with the net result of either a spinning 'connecting to home share' message or the home share just disappearing from the computers section.
    If there is a configuration fix for this please let me know as I haven't located a fix!  Otherwise this seems very much to be a code flaw in iTunes 11 or the mDNSResponder and I would appreciate some input from Apple!  Going on other questions in these forums it would seem the problem is not limited to Windows PCs but also Macs too.  NB: This is not a TCP issue, when the ATV3 or iPad knows the IP of the iTunes server all works flawlessly, it just periodically they seem to refresh the list of home shares and at this point they loose the information about the home share they have just been using because of the non-response to the UDP multicast discovery broadcast packet, that is arriving at the host PC and isn't being blocked by the firewall.
    Many Thanks!

    In my case there was no import from a former mac.
    My problem (at least mine) is that no app that offers media sharing works properly. Neither itunes home sharing nor AirVideo nor EyeTV sharing.
    So I'm pretty sure that this is network issue.
    Adding another user on my mac and sharing a new library works not also. But sharing from another laptop in my WiFi works. So this has to be a network issue on my mac, not only my user, but an issue of the whole system.
    But I'm not willing to reinstall MacOS X for that if I don't have to.

  • UDP/Multicast Broadcasts

    Hello,
    I am interested in writing some P2P-ish type networking
    applications which need to use UDP multicasting to discover other
    participating hosts on the network. Does AS3 networking libraries
    have the ability to do UDP and multicasting?
    Thanks,
    Brad

    If you want the UDP connection to have multiple listeners:
    - Open the file: LabVIEW.ini (it is most likely located in C:\Program Files\National Instruments\LabVIEW 7.x, where "x" is the version number.)
    - Add the line: SocketSetReuseAddr=TRUE
    - Restart LabVIEW. The changes should take effect.
    If you do not want to have multiple listeners, but want to be able to have multiple writers to the same port, this does not apply. You can open different local ports so as not to generate Error 60 and then write to the same remote port without causing an error.
    If you are trying to communicate with multiple devices using the LabVIEW UDP VIs where each device has a unique IP addresses that are both listening on the same port in parallel, you can do the following:
    This error occurs because you cannot open multiple connections on the same local port. However, the local port and the remote port do not need to be the same. The local port is determined by the "port" inputs of the UDP Open VIs. The remote port is determined by the "port" inputs of the UDP Write VIs. For each write, set the remote port to the one on which the corresponding device is listening.
    I would also recommend that you look at the following link:
    http://zone.ni.com/devzone/conceptd.nsf/webmain/BB41313D7AB28CBD86256802007B8DC5?opendocument
    Nandini Subramaniam
    Applications Engineer
    National Instruments

  • Can applet use multicast broadcasting?

    Hello
    Can applet listen to server sending to multiple sockets?. When I tried to run applet exception showed up. Applet would listen for message, server was sending messages in a thread. Could You direct me to some examples which can do such thing?. (Server sending String message, two or more applets receive it, Talk from Java Tutorial is not working). Can this only be done by broadcasting?.
    Cheers
    Message was edited by:
    macmacmac

    But is there a way to get around this restriction?.I'm not sure there is a general restriction. I have
    just used my muticast bus to receive multicast in a
    JApplet using the Applet Viewer and it works
    perfectly. Therefore, if there is a restiction then
    it probably is a securtiy restriction so that it
    should be possible to overcome this by signing the
    Applet.This is what I thought too but ejp thinks different and he wrote a book on it so I don't know. He last post seems to also show this can't be done.
    >
    If I get some time later I will test this and post
    the result.I would be interested to hear the results.
    I would also be interested in anything that might point out why this decision was made if it is truly impossible. It seems dumb to me but perhaps an explanation of what the thought process was would help.

  • Does multicasting work with TCP?

    This question is very important to me, because it may help me with my final year project. I have seen Java code where multicasting is used with DatagramPackets (that is UDP). I would like the communication to be reliable though, so I don't know whether multicasting works also with TSP. Does anyone know?

    Sure you didn't see code with UDP and broadcasts?
    Broadcast and multicast are not the same.

Maybe you are looking for