More questions on sockets

Les Hill leh at crane.cis.ufl.edu
Sat Jun 15 04:51:49 AEST 1991


In article <1991Jun14.162215.14657 at ncsu.edu>, jwb at cepmax.ncsu.edu (John W. Baugh Jr.) writes:
|>   - when trying to bind a stream socket I sometimes get an error
|>     "Address already in use", even though I've closed the socket
|>     (for example, when I run the program in succession a couple of
|>     times).  Is there something else I have to do?

I believe this is a problem due to TCP's "handshaking" on a close.  One workaround
I use is:

...
  int slin = 0; /* unset SO_LINGER */
...
  /* unset SO_LINGER */
  if (setsockopt(socket, SOL_SOCKET, SO_LINGER, (char *)&slin, sizeof(int)) < 0) {
#ifdef DEBUG
    perror("setsockopts:SO_LINGER");
#endif
    return -1;
  }
...

when I am setting up the socket for use.

|>   - assuming I'm on the right track (big assumption), is it possible
|>     to raise the level of abstraction of my send_msg/recv_msg
|>     functions.  For example, ideally one would like to do the
|>     following:
|>        send_msg(char *msg, int size, int process);
|>        recv_msg(char *msg, int size, int process);
|>      where "process" may be a process on any machine.  Okay, so that's
|>      probably asking too much.  What I currently have is:
|>        send_msg(char *msg, int size, char *hostname, int port);
|>        recv_msg(char *msg, int size, int port);
|>      Can one do better (w/o an inordinate effort)?

Probably not.  In order to achieve the functionality you want in your ideal
situation, you (IMHO) will need to implement your own "protocol" on top of TCP.
The "protocol" could get arbitrarily complex -- as an example, I wrote a "sockets
library" that allowed dynamic link management, while maintaining a fairly high
level interface (e.g.

extern int WriteConnection(); /* (char *data, int size, int user) */
extern void ReadConnection(); /* (char *data, int (*func)()) */

) -- the "protocol" I implemeted was about 3K lines worth of nicely formatted
(and probably bloated :) code.

Les
-- 
Extraordinary crimes against the people and the state have to be avenged by
agents extraordinary.  Two such people are John Steed -- top professional, and
his partner, Emma Peel -- talented amateur; otherwise known as "The Avengers."
INTERNET: leh at ufl.edu  UUCP: ...!gatech!uflorida!leh  BITNET: vishnu at UFPINE



More information about the Comp.unix.programmer mailing list