using htons()

B

bobfilar

I am working on a port enumerator and I am coming up with a problem.
Here is the part of my code that is giving me fits right now:
for (int i=0; i < (int) pTcpTable->dwNumEntries; i++) {
if(pTcpTable->table.dwState == MIB_TCP_STATE_LISTEN)
{

printf("localhost:%ld <---> %ld\n", pTcpTable->table.dwLocalPort,

pTcpTable->table.dwRemotePort & 0xFFFF);
}

Now when that prints out the Local port and remote ports it is not
print the right numbers. After some reseach I concluded that I needed
to use htons(), but the only problem is I dont know where to implement
it. Any help on what can do to convert DWORD to uINT or just to get it
to print the right info to the buffer?
 
J

Jim Langston

I am working on a port enumerator and I am coming up with a problem.
Here is the part of my code that is giving me fits right now:
for (int i=0; i < (int) pTcpTable->dwNumEntries; i++) {
if(pTcpTable->table.dwState == MIB_TCP_STATE_LISTEN)
{

printf("localhost:%ld <---> %ld\n", pTcpTable->table.dwLocalPort,

pTcpTable->table.dwRemotePort & 0xFFFF);
}

Now when that prints out the Local port and remote ports it is not
print the right numbers. After some reseach I concluded that I needed
to use htons(), but the only problem is I dont know where to implement
it. Any help on what can do to convert DWORD to uINT or just to get it
to print the right info to the buffer?


From MSDN documentation:
Remarks
The htons function takes a 16-bit number in host byte order and returns a
16-bit number in network byte order used in TCP/IP networks.

I don't see how this has anything to do with ports though, only IP
addresses.

I think you might get better results from a group dealing with your specific
OS. Maybe windows? Try microsoft.public.vc.language
 
I

Ian Collins

I am working on a port enumerator and I am coming up with a problem.
Here is the part of my code that is giving me fits right now:
for (int i=0; i < (int) pTcpTable->dwNumEntries; i++) {
^^^
Why the cast?
if(pTcpTable->table.dwState == MIB_TCP_STATE_LISTEN)
{

printf("localhost:%ld <---> %ld\n", pTcpTable->table.dwLocalPort,

pTcpTable->table.dwRemotePort & 0xFFFF);


What type is pTcpTable->table.dwRemotePort?

Here would be an OK place to output the port number, just use htons(port);
}

Now when that prints out the Local port and remote ports it is not
print the right numbers. After some reseach I concluded that I needed
to use htons(), but the only problem is I dont know where to implement
it. Any help on what can do to convert DWORD to uINT or just to get it
to print the right info to the buffer?

I don't know what DWORD or uINT are.
 
I

Ian Collins

Jim said:
From MSDN documentation:
Remarks
The htons function takes a 16-bit number in host byte order and returns a
16-bit number in network byte order used in TCP/IP networks.

I don't see how this has anything to do with ports though, only IP
addresses.
The (16 bit) port number still goes over the wire in network byte order.
 
L

loufoque

(e-mail address removed) a écrit :
After some reseach I concluded that I needed
to use htons()

You're using %ld, that means you have a long.
That would be htonl then.

Plus, according to logic you should use ntohl and not htonl, but they're
the same on non middle-endian systems (and no one uses middle-endian
anymore)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top