Udp Sockets: ports and bind

This is a discussion on Udp Sockets: ports and bind within the Linux Programming forums, part of the Platform Specific Boards category; I was wondering, why must I bind a udp socket. If I remember correctly with udp sockets you specify the ...

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    Udp Sockets: ports and bind

    I was wondering, why must I bind a udp socket. If I remember correctly with udp sockets you specify the ip address and port when you sendto messages and with recvfrom you have these details stored in a varibale of struct sockaddr type.

    So if I don't bind the socket of the server does this mean that it will listen to a random port or not at all?

    Also in the client side, you just create a socket and then sendto a message with the details of the server (IP, port). Does the socket get bind(ed) automatically by the system to a random port and uses that from then on?

  2. #2
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Also is there any way to use a socket to find out on what ports someone is trying to connect;
    I am trying to make a simple port knocking program thingy and I can't decide whether to listen to all the sockets involved to the knock procedure and then search for an IP with the correct combination or have only the first port open then the second and so on until someone nails it...
    But I am wondering about the possible connection attempts to other ports in between the correct tries...

    So one way I can thing of is
    1)Listen in <port1>. When someone sends a ping message I sen back a pong and then listen to <port2>... Then after <portN> I listen to port in TCP protocol for communication

    2)Listen to all ports. When someone attempts a connection to one of them I get their IP and add it to a list/struct (I will work the details later) if its on the first port of the combination. If it's on some other port then I search my list/struct... for the IP and see if this "knock" is in correct order. If yes I add it/check a flag/raise an enumarator/smth and go on else I remove it from the list/struct/smth

    So can anyone give me an insight on these?
    Personally for me it makes more sense to listen to a bunch of ports instead of opening one after the other...
    Last edited by Phoenix_Rebirth; 11-28-2009 at 12:36 PM.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Phoenix_Rebirth View Post
    I was wondering, why must I bind a udp socket. If I remember correctly with udp sockets you specify the ip address and port when you sendto messages and with recvfrom you have these details stored in a varibale of struct sockaddr type.
    Unless you bind, how will you know what port your are listening to, and therefore how will the client know it?

    Also in the client side, you just create a socket and then sendto a message with the details of the server (IP, port). Does the socket get bind(ed) automatically by the system to a random port and uses that from then on?
    Yes, if you do not bind it yourself the system binds it for you. Unless you want an unpredictable source port, it's better practice to always bind it yourself.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Another question:

    If I send a udp packet to a port of another host/terminal/pc usually how long does it stay there? Let's say that I have two terminal and on one I run a program that receives msgs containing ping on various ports and responds with pong (using select) and on the other a program that sends msgs containing ping and receives the msgs containing pong. If I make the sender first send all the messages and then receive the responds is there any chance that some may be lost because of the delay of calling the function recvfrom?

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Phoenix_Rebirth View Post
    Another question:

    If I send a udp packet to a port of another host/terminal/pc usually how long does it stay there? Let's say that I have two terminal and on one I run a program that receives msgs containing ping on various ports and responds with pong (using select) and on the other a program that sends msgs containing ping and receives the msgs containing pong. If I make the sender first send all the messages and then receive the responds is there any chance that some may be lost because of the delay of calling the function recvfrom?
    That's going to entirely depend on how the network stack works for the host in question. Each socket will have a queue of packets waiting, but you have no idea how big those queues are, whether they expire, whether they are fixed size or based on network traffic patterns, etc.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875

    MT means never missing a packet

    Quote Originally Posted by brewbuck View Post
    That's going to entirely depend on how the network stack works for the host in question. Each socket will have a queue of packets waiting, but you have no idea how big those queues are, whether they expire, whether they are fixed size or based on network traffic patterns, etc.
    That is exactly why having an MT server for such things set up is essential on any server other than a play server; if there is a packet, grab it and stash it into a queue or something so that you don't lost packets at times of high congestion. For UDP its easier b/c unless you are doing something using multipacket messages, have a thread sit there and suck in packets (while packets are waiting) and stuffing them into a queue for processing by another thread is the way to go. For TCP it gets a little more interesting but the process is essentially the same, only instead of stashing a packet you are stashing a socket connection...

    HTH...
    Jeff

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bind problem
    By moment in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-24-2009, 12:36 PM
  2. Can I bind a UDP socket to a port, but send to any other port?
    By trillianjedi in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-25-2009, 04:27 PM
  3. sockets programming
    By kaijuu in forum Networking/Device Communication
    Replies: 9
    Last Post: 03-25-2008, 12:07 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21