UDP Send and Listen Information on Ports

Posted by Onoitsu2 on Sat 02 Dec 2006 06:15 AM — 7 posts, 27,704 views.

USA #0
I was just messing around with the Immediate window, and UDP Sending, to a "program" I scripted in AutoIt, and noticed an issue in the port numbering. In LUA you can use ANY actual port number, as it will accept a LONG integer, and yet in VP it will only accept a SHORT integer, so only to about 32k and some hundreds more ...

It is not a real problem, except for those that try to send on a port that is VERY unlikely to be used for any COMMERCIAL application.

I don't know if it happens in other languages, but it is quite possible it would.

Just thought I'd post my findings.

Laterzzz
Onoitsu2
Australia Forum Administrator #1
What is VP?

Anyway, in the TCP/IP protocol, ports are a 16-bit number (a short) so one larger than 65535 is not possible.
USA #2
sorry lol VP = VB, as in VBS, you can only use a port numbering up to 32767, and yet in LUA you can use up to the 65535 limit.

I was rushed to finish posting the messages last night ...

Laterzzz,
Onoitsu2
USA #3
What do you mean by 'Lua'? Lua has no native support for sockets. Is this using the Lua Socket library? It states in the reference (for TCP/IP, at least) that the number can't be greater than 65k.
http://www.cs.princeton.edu/~diego/professional/luasocket/tcp.html#bind

However Lua itself doesn't distinguish between the various integer types (the default native number type is double), so it would let you enter whatever you want.

And is VB using a signed or unsigned short? A signed would go to 32k, but the unsigned short would go up to 65k which is the proper value.
Australia Forum Administrator #4
He was talking about UdpSend function:

http://www.gammon.com.au/scripts/doc.php?function=UdpSend

Since the prototype specifies a short, and not an unsigned short, you can probably only go to 32767.
USA #5
Ah, ok; so the VB binding to Winsock is what's limiting things? I checked the UDP protocol (e.g. http://en.wikipedia.org/wiki/User_Datagram_Protocol) and it says that the max is 65k, like TCP/IP.

I don't remember how VB handles type coercion between signed/unsigned types. If you were to enter 65k into a 'short' parameter, would that wrap around to the appropriate negative number (and then back to positive by the UDP handler) or does that generate an error?
Australia Forum Administrator #6
It is probably my fault for specifying short, MFC has generated this function prototype:


long CMUSHclientDoc::UdpSend(LPCTSTR IP, short Port, LPCTSTR Text) 


However a bit of testing reveals that it appears to correctly handle a number larger than 32767, as internally it calls htons, which has this prototype:


u_short htons (
  u_short hostshort  
);


It passes down "Port" to that function, so even though technically it is negative it all seems to resolve itself.

Thus you should be able to use a port in the range 0 to 65535.