[plt-scheme] bind(2) on a client socket.

From: Evan Farrer (farrer at cs.utah.edu)
Date: Fri May 20 17:15:43 EDT 2005

On a project that I'm working on I need to be able to bind a *client* sockets local port.  I couldn't find a way to do this in DrScheme so I modified tcp-connect to take two additional optional parameters, a host and a port.  The host can be either a string or #f (similar to tcp-listen).  If anyone else needs this functionality the patch is at "http://www.cs.utah.edu/~farrer/patches/client-bind.patch".  If the powers that be decide to commit this into the mainline that would be great too.

To apply the patch download it to your plt directory and execute:

  patch -p0 < client-bind.patch

Example usage:
 
  (tcp-connect "www.google.com" 80) ; We're backwards compatible
  (tcp-connect "127.0.0.1" 80 "127.0.0.1" 9999) ; local socket 9999
  (tcp-connect "www.google.com" 80 "10.0.0.1" 9999) ; Specify the interface and the port
  (tcp-connect "127.0.0.1" 80 #f 9999) ; Just specify the port

Caveat:

  I've only tested it on FreeBSD(5.4) and Linux (Gentoo Linux 2.6).  I'm pretty sure it will work on Windows but I haven't tested it.  I think it will work on Macs, but I'm less confident as I'm not as familiar with them.



While making this change I discovered a simple typo in tcp-listen.  If the forth parameter is the wrong the error message reports the function name as "tcp-connect"  for example:

  (tcp-listen 9999 7 #t 444)

Outputs:
  tcp-connect: expects type <string or #f> as 4th argument, given: 444; other arguments were: 9999 7 #t

This "http://www.cs.utah.edu/~farrer/patches/tcp-listen-typo.patch" patch fixes the typo.


Thanks,


Evan




Posted on the users mailing list.