Link to home
Start Free TrialLog in
Avatar of jchristn123
jchristn123

asked on

How do I convert an integer value representing a specific TCP or UDP port into the correct 2-byte value in C?

How do I convert an integer value representing a specific TCP or UDP port into the correct 2-byte value in C?
Avatar of Infinity08
Infinity08
Flag of Belgium image

Just use :

        htons(port)
Avatar of jchristn123
jchristn123

ASKER

Hi infinity08,

I tried this expecting that it would put the data into a 2-byte string:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <netinet/in.h>

int main(int nArg, char* pszArgs[])
{
  int portnum;
  portnum=14392;
  char portnumhtons[3];
  portnumhtons=htons(portnum);
  printf("portnumhtons=%s\n",portnumhtons);
  return 0;
}

but of course, the result was:

htons.c: In function 'main':
htons.c:16: error: incompatible types in assignment

and when I try with portnumhtons as an int, i.e.:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <netinet/in.h>

int main(int nArg, char* pszArgs[])
{
  int portnum;
  portnum=14392;
  int portnumhtons;
  portnumhtons=htons(portnum);
  printf("portnumhtons=%d\n",portnumhtons);
  return 0;
}

I get:

portnumhtons=14392

Which doesn't convert to a 2-byte value.  I'm looking for the 2-byte representation of the integer port number in char array format if possible.

Thanks for all of your help!
>> I tried this expecting that it would put the data into a 2-byte string:

No, it returns a 16 bit value that represents the port number in network byte order.

If you want, you can access the individual bytes like this :

        int portnum = 14392;
        unsigned short port_nbo = htons(portnum);
        printf("first byte = %02x\nsecond byte = %02x\n", ((unsigned char*) &port_nbo)[0], ((unsigned char*) &port_nbo)[1]);

But I don't see why ...

If you just want to print the port number, then just do :

        int portnum = 14392;
        printf("port = %d\n", portnum);

Or if you have it in network byte order :

        int portnum = 14392;
        unsigned short port_nbo = htons(portnum);
        printf("port = %d\n", (int) ntohs(port_nbo));
Hi infinity08,

Perhaps some clarification on what I'm wanting to do will help.  I need to embed a return port number into the datagram being sent - not the same as the source port that is specified in the datagram header.  The port number needs to be 2 bytes, and the recipient will receive the datagram, parse through the data portion, and extract the two bytes that identify the return port number to which it should reply, which again is not the same port number used as the source port in the original datagram.

Thus, if I want to embed 14392 as the return port number (even though the source port number in the datagram header is different) as a 2-byte character, I need to convert it first to the 2-byte character.

Does that help?

Thanks again
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial