socket io problem

Paul Whitfield paulw at omnitronics.com.au
Wed Jan 30 03:58:57 UTC 2008


Ian Caddy wrote:
> Hi Chris,
> 
> 
> CWolfe at motioncontrol.org wrote:
>> Hi all,
>> I'm having a problem using sockets, and I hope someone might know what i'm doing wrong.
>> In a previous email, I mentioned i'm trying to create a telnet server which directly manipulates 
>> the rs-232 buffers (as opposed to using ptys which wouldn't be compatible with our 
>> application.) I can read and write to and from the socket, but only in a blocking way, which 
>> doesn't fit our needs. I've tried to register handlers for the sopcket, but they never get called. I 
>> have modeled my socket initialization on the telnetd.c and also GoAhead's initialization code.
>> Could someone possibly explain how i can set up a telnet socket which is non-blocking at 
>> minimum, but even better, one that actually calls the handlers on reception/transmission?
>> I assume it's something small that I'm missing, but nothing i've tried has worked in triggering 
>> execution of the  socketHandler function. Thank you for your time.
> 
> We use telnet in our applications, but we setup a timeout on the receive 
> of 500ms.  I don't know how to make the socket truly non-blocking, but 
> one option would be to setup the receive timeout to only 1 or 2 ms. 
> Here is the code snippet if interested:
> 
> static int SetSocketTimeout(int connectSocket, int milliseconds)
> {
>     struct timeval tv;
> 
>        tv.tv_sec = milliseconds / 1000 ;
>        tv.tv_usec = ( milliseconds % 1000) * 1000  ;
> 
>     return setsockopt (connectSocket, SOL_SOCKET, SO_RCVTIMEO, (char 
> *)&tv, sizeof tv);
> }
> 
> I had a quick look at the wakeup functionality as I was curious and it 
> seems to look OK from a quick code walk through.  If no-one has any 
> better suggestions than mine, maybe you could post your code example of 
> how you set it up and we could look at it?

I have set up non-blocking sockets in the past, this code snippet is
for a UDP socket, but I believe it will work with TCP sockets as well

     status == fcntl( udp_fd(passthru->socket), F_SETFL, O_NONBLOCK);

     if ( status == 0 )
     {
         wakeup.sw_pfn = network_receive_callback;
         wakeup.sw_arg = (caddr_t ) passthru->task_id;

         status = setsockopt( udp_fd(passthru->socket), SOL_SOCKET, 
SO_RCVWAKEUP, &wakeup, sizeof(wakeup));

         if ( status != 0 )
         {
             syslog(LOG_ERR, "Error Installing call backs\n");
         }
     }

     return passthru->socket;


Regards

Paul



More information about the users mailing list