0

I have two C files i.e server.c and client.c. Initially in server side I created the socket and fixed a port on which the server can listen and send . But in client side when I send any message to the server side how should I explicitly assign a client source port on which it can listen. Everytime the kernel assigns a random port no. between 1024 to 65535. But how can I explicitly define the client source port in UDP communication?

This link I found for TCP communication. I want for UDP.

For example: On server side: Server Created Binded Correctly Listening ... Connection Established connection established with IP : 10.0.2.15 and PORT : 40064 Client : Hello

On Client side: Client Created Binded Correctly Client Connected Server : Hello

40064 is randomly assigned by the kernel to the client. But I want to explicitly set that port on client side for UDP. Is there any function or command to do it?

ottomeister
  • 5,415
  • 2
  • 23
  • 27
  • See the first answer of this post https://stackoverflow.com/a/27893987/6156470 – John Z. Li Oct 08 '18 at 07:50
  • 1
    In this case the solution is the same for UDP as it is for TCP. Make a call to the `bind()` function to associate your client's UDP socket with a specific port before you start sending datagrams through that socket. If you don't need to bind the client socket to a specific address then you should set an all-zero address in the `struct sockaddr_in` argument to the `bind()` call. (Your TCP example sets a specific address, but that's not required and it's often not desirable.) – ottomeister Oct 08 '18 at 17:20
  • Yeah got it . Thank you @ottomeister. – Debiprasad Das Oct 09 '18 at 05:40

0 Answers0