Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. UDP connection -> outgoing port trouble (still)

UDP connection -> outgoing port trouble (still)

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelpsysadmin
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Souldrift
    wrote on last edited by
    #1

    Good morning, last week I already wrote about my little problem. Then I got sick and and couldn´t take part in the conversation. Even though I got an answer it didn´t help (or I misunderstood). Now I wanted to ask/clarify and would be glad for some help. The code:

    SOCKET m_oUDPSocket;
    SOCKADDR_IN m_oUDPAddress;

    .
    .
    .

    m_oUDPSocket = socket(AF_INET,SOCK_DGRAM,0);
    memset(&m_oUDPAddress,0,sizeof(SOCKADDR_IN));
    m_oUDPAddress.sin_family=AF_INET;
    m_oUDPAddress.sin_port=htons(m_iClientPort);
    m_oUDPAddress.sin_addr.s_addr=inet_addr(m_sClientIP.data());

    .
    .
    .

    int rc=sendto(m_oUDPSocket,(char*)packet,size+12,0,(SOCKADDR*)&m_oUDPAddress,sizeof(SOCKADDR_IN));

    The problem: I´d like to send the data via 'sendto' ALWAYS on the same outgoing port. The question: Where and how do I define this outgoing port? Do I have to create another SOCKADDR_IN with the server port and address and bind it to m_oUDPSocket? Like this?

    SOCKADDR_IN addr;
    memset(&addr,0,sizeof(SOCKADDR_IN));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(desiredPort);
    addr.sin_addr.s_addr=INADDR_ANY;
    rc=bind(m_oUDPSocket,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN));

    Or am I on the wrong path here? And how can I test if it indeed sends from the desired port? Thanks for any help. Souldrift

    P 1 Reply Last reply
    0
    • S Souldrift

      Good morning, last week I already wrote about my little problem. Then I got sick and and couldn´t take part in the conversation. Even though I got an answer it didn´t help (or I misunderstood). Now I wanted to ask/clarify and would be glad for some help. The code:

      SOCKET m_oUDPSocket;
      SOCKADDR_IN m_oUDPAddress;

      .
      .
      .

      m_oUDPSocket = socket(AF_INET,SOCK_DGRAM,0);
      memset(&m_oUDPAddress,0,sizeof(SOCKADDR_IN));
      m_oUDPAddress.sin_family=AF_INET;
      m_oUDPAddress.sin_port=htons(m_iClientPort);
      m_oUDPAddress.sin_addr.s_addr=inet_addr(m_sClientIP.data());

      .
      .
      .

      int rc=sendto(m_oUDPSocket,(char*)packet,size+12,0,(SOCKADDR*)&m_oUDPAddress,sizeof(SOCKADDR_IN));

      The problem: I´d like to send the data via 'sendto' ALWAYS on the same outgoing port. The question: Where and how do I define this outgoing port? Do I have to create another SOCKADDR_IN with the server port and address and bind it to m_oUDPSocket? Like this?

      SOCKADDR_IN addr;
      memset(&addr,0,sizeof(SOCKADDR_IN));
      addr.sin_family=AF_INET;
      addr.sin_port=htons(desiredPort);
      addr.sin_addr.s_addr=INADDR_ANY;
      rc=bind(m_oUDPSocket,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN));

      Or am I on the wrong path here? And how can I test if it indeed sends from the desired port? Thanks for any help. Souldrift

      P Offline
      P Offline
      pallaka
      wrote on last edited by
      #2

      What do u mean by outgoing port? Normally the ipaddr and the port number on which u need to send the data will be fixed. client must have the PortNo and Ipaddr to send the data in UDP to server.

      S 1 Reply Last reply
      0
      • P pallaka

        What do u mean by outgoing port? Normally the ipaddr and the port number on which u need to send the data will be fixed. client must have the PortNo and Ipaddr to send the data in UDP to server.

        S Offline
        S Offline
        Souldrift
        wrote on last edited by
        #3

        Well, by outgoing port I mean simply the UDP port. The question was, how do I get a fixed one? By now I figured that it is the 'bind' command that will do that. Though, what UDP port does the server use if I do NOT specify a bind? Regards Souldrift

        L 1 Reply Last reply
        0
        • S Souldrift

          Well, by outgoing port I mean simply the UDP port. The question was, how do I get a fixed one? By now I figured that it is the 'bind' command that will do that. Though, what UDP port does the server use if I do NOT specify a bind? Regards Souldrift

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          IIRC random between 1024 and 65535, or maybe between 50k and 65535

          S 1 Reply Last reply
          0
          • L Lost User

            IIRC random between 1024 and 65535, or maybe between 50k and 65535

            S Offline
            S Offline
            Souldrift
            wrote on last edited by
            #5

            Yes, thanks. It was just that I hadn´t thought of binding the socket to a certain port (like I mused in the second code block of my initial post). Now it´s working. Cheers Souldrift

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups