views:

67

answers:

1

From this rpg socket tutorial we created a socket client in rpg that calls a java server socket

The problem is that connect()/send() operations blocks and we have a requirement that if the connect/send couldn't be done in a matter of a second per say, we have to just log it and finish.

If I set the socket to non-blocking mode (I think with fnctl), we are not fully understanding how to proceed, and can't find any useful documentation with examples for it.

I think if I do connect to a non-blocking socket I have to do select(..., timeout) which tells us if the connect succeed and/ we are able to send(bytes). But, if we send(bytes) afterwards, as it is now a non-blocking socket (which will immediately return after the call), how do I know that send() did the actual sending of the bytes to the server before closing the socket ?

I can fall back to have the client socket in AS400 as a Java or C procedure, but I really want to just keep it in a simple RPG program.

Would somebody help me understand how to do that please ? Thanks !

+2  A: 

In my opinion, that RPG tutorial you mention has a slight defect. What I believe is causing your confusion is the following section's code:

...
Consequently, we typically call the send() API like this:

D miscdata        S             25A
D rc              S             10I 0

C                   eval      miscdata = 'The data to send goes here'
C                   eval      rc = send(s: %addr(miscdata): 25: 0)
c                   if        rc < 25
C*  for some reason we weren't able to send all 25 bytes!
C                   endif

...

If you read the documentation of send() you will see that the return value does not indicate an error if it is greater than -1 yet in the code above it seems as if an error has occurred. In fact, the sum of the return values must equal the size of the buffer assuming that you keep moving the pointer into the buffer to reflect what has been sent. Look here in Beej's Guide to Network Programming. You might also like to look at Richard Stevens' book UNIX Network Programming, Volume 1 for really detailed explanations.

As to the problem of determining if the last send before close() did the actual send ... well the paragraph above explains how to determine what portion of the data was sent. However, calling close() will attempt to send all unsent data unless SO_LINGER is set.

fnctl() is used to control blocking while setsockopt() is used to set SO_LINGER.

The abstraction of network communications being used is BSD sockets. There are some slight differences in implementations across OS's but it is generally quite homogeneous. This means that one can generally use documentation written for other OS's for the broad overview. Most of the time.

Allen