UDP programming - set local port

This is a discussion on UDP programming - set local port within the C++ Programming forums, part of the General Programming Boards category; Is there a way to specify a particular port for UDP sockets to send packets *out* of? I've tried using ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    1

    UDP programming - set local port

    Is there a way to specify a particular port for UDP sockets to send packets *out* of? I've tried using bind(), and it does in fact seem to bind the socket (using netstat to check), but using sendto() the packets are still sent from an arbitrary port. I don't have all that much experience with UDP, but that seems strange....

    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    Yes I also Have this problem, I can send udp packets to a server, But how to I specify the port for the client to use. Rather than any old random one thats free. Info Im finding info mainly releated to server side.

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Code:
    struct sockaddr_in sa;
    memset(&sa, 0, sizeof(sa));
    sa.sin_port = htons( port );
    considering the above code i think your code will be having something like sa.sin_port = htons(0); where 0 tells it to use any random port.. You can specify your own port here instead of 0 to use your custom specified port..

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    Hi!

    I have a question regarding UDP Communications.
    What is necessary to make the built-in Fastnet NMUDP component (Borland C++ Builder 5) work?

    There are 3 PCs at home, the Borland C++ Builder is installed on each one. Each has Windows XP, SP2, but my programs using this NMUDP component are only working on 2 of the PCs.
    It seems that there's no UDP communications allowed on that PC at all! I checked the firewalls, the router, etc. but the UDP is not forbidden anywhere.

    I tested my program on other PCs too, with Windows2000. That was quit interesting too. Those PCs were on a LAN about which I don't have detailed information. On those PCs reception of the UDP packets worked, but they couldn't send packets to my notebook.

    Does anybody have an idea what could be the issue?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 07:52 AM
  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. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 08:47 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM

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