Jorgen Grahn <
grahn...@snipabacken.se> writes:
> On Thu, 2013-08-29, Ian Pilcher wrote:
>> I am writing a program that needs to read some data from a server via a
>> TCP socket every few seconds. (The server is the hddtemp program,
>> running in daemon mode.)
>>
>> Documentation is somewhat sparse, but AFAICT the server simply accepts
>> connections, writes its output to the socket, and immediately closes it.
>> So every time I want to poll the server, I need to reconnect.
>>
>> Is there any way to accomplish this without doing a close()/socket()
>> cycle every time? I've tried using shutdown(fd, SHUT_RDWR), but I get a
>> "Transport endpoint is already connected" error.
>
> And I guess this matters to you because you don't want to rework your
> select(2) fd_set or whatever when the file descriptor changes?
Often, it won't: The original descriptor returned by socket was the
lowest-numbered free file descriptor at that time. If nothing else is
closed and re-opened, the number will say the same. Otherwise, dup2
could be used to avoid changing the descripor number when having to
deal with a new socket (instead of close(fd); fd = socket(...);, do
newfd = socket(...); dup2(newfd, fd); close(newfd);).