× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.




On 04/02/2006, at 1:57 PM, Dave Johnson wrote:

I am trying to figure out some basic sockets programming using Scott Klement's tutorial. This is an excellent tutorial!

Here is the problem I am finding.

I use the code from Scott's sample:

Code deleted.

and it works fine. The problem is that it shouldn't work because I do not actually have a socket server listening on the port I specify in the SIN_PORT paramater. That is, if I use a port # of 8540 in SIN_PORT for example but my socket server is actually listening on port 3994, then I should get an error, right? But I don't. Instead, I get the return value = 0 and the program continues on.

If it makes a difference, my socket server is running on a pc and I use the DOS command NETSTAT -a to see what is active on all the ports. I see my socket server listening on port 3994 and nothing on 8540, and yet the connect does not return a negative value.

Obviously I am missing something here, but I am so new at this stuff that I cannot see what it is.

Anyone have any ideas of things I could check?

Couple of things:

First, byte-ordering. The bytes for network values must be in network byte order. This happens to be big-endian but PCs and other toys use little-endian thus your port number is probably being misinterpreted. You should always use the byte-order conversion functions when setting values in a socket address structure.

         c                   eval      sin_addr = htonl(IP)
         c                   eval      sin_port = htons(port)

The example code you showed will work on OS/400 because it is a big-endian system.

Given your specified port of 8540 which in hex is 215C which in little-endian is 5C21 which will be interpreted as port 23585 by the socket code on a PC. A similar misinterpretation will occur with the IP address so you may be attempting to connect to a completely different server which does happen to have a listener on port 23585.

Secondly, it is not apparent from your code but I presume IP is a 32-bit integer value and NOT the dotted-decimal form of the address. If it is the dotted-decimal address (i.e., a character value) then you will have to convert from the dotted-decimal form to the network form using inet_addr or inet_aton (or inet_pton if you want IPv6 compatibility).

Thirdly, you should be checking errno when a socket function indicates an error condition. This will give you a better** indication of the reason for the failure. For example, ETIMEDOUT, EHOSTUNREACH, or ENETUNREACH if no server is found or ECONNREFUSED if no listener is active on the target port.

Try running the example code on OS/400 and see what happens.

**better is a relative term: in this case it means better by Unix standards which are quite pathetic by OS/400 standards but better than nothing.

Regards,
Simon Coulter.
--------------------------------------------------------------------
   FlyByNight Software         AS/400 Technical Specialists

   http://www.flybynight.com.au/
   Phone: +61 3 9419 0175   Mobile: +61 0411 091 400        /"\
   Fax:   +61 3 9419 0175                                   \ /
                                                             X
                 ASCII Ribbon campaign against HTML E-Mail  / \
--------------------------------------------------------------------



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.