How to rebind a port that is already bind

Discussion in 'Mac Programming' started by pkmk, Mar 30, 2010.

  1. pkmk macrumors newbie

    Joined:
    Mar 30, 2010
    #1
    Hi,
    I want to bind a port which is already bind by another process. Is there any way to bind the port that is already bind. I used sockopt() function to reuse the port but it not work.

    Is there any way to take a port that is bind by anyother process mean we take enforcely the port from other process and use this port for my process. I want to bind the following port no 32005.

    I am waiting for your quick response or give me any solution to this problem.
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    No, not that I am aware of. That would be a huge security hole if you were allowed to take over the port of another process.

    Frankly the only reasons I can think of are malicious. Why do you want to effectively hijack another processes port?

    Edit:

    The only way to bind to same port is if you bind to a different address, so the other process might be listening on 0.0.0.0:32005 and your process would listen on 1.1.1.1:32005 (for example).
     
  3. pkmk thread starter macrumors newbie

    Joined:
    Mar 30, 2010
    #3
    hi,
    thanx for your reply. I am clear this that i am not use this for hijack or any misuse purpose. Now come to problem the problem is i use the UDP socket. And i run two different application. And i want both application bind to same port for sending and receiving data. For this i use sockopt() function this function returns 0. But the bind function returns -1. I use the xcode tools.
    The real problem is when i run both application. Then first application is bind successfuly but second application are not bind to same port and return -1.
     
  4. Alkiera macrumors regular

    Joined:
    Mar 11, 2008
    #4
    That answer is that you can't do that. A port can be bound by only one application at a time; two applications cannot share a port without having a third app manage the port, and distribute messages to the two applications via some other method.

    I think the real issue is why you have two apps listening on the same port? Are you trying to test a client-server app? The client shouldn't need a well-known port... the server should detect what port the client is using from the first UDP packet, and use that report when it replies.
     

Share This Page