UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2002-07-25 08:19 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: 3.9 - How can I determine the local port number?

len = sizeof ( server ); 
if ( getsockname ( sock, &server, &len ) < 0 ) 
  perror ( "getsockname" ); 
  else 
    fprintf ( stderr, "local port number before connect: %d\n", ntohs ( ((struct sockaddr_in *)&server)->sin_port )); 
     
    connected = connect(sock, (struct sockaddr *) &address, sizeof(address)); 
     
    len = sizeof ( server ); 
    if ( getsockname ( sock, &server, &len ) < 0 ) 
      perror ( "getsockname" ); 
      else 
        fprintf ( stderr, "local port number after connect : %d\n", ntohs ( ((struct sockaddr_in *)&server)->sin_port ));

Offline

Board footer

Powered by FluxBB