For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > March 2006 > Need help modifying a class.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Need help modifying a class.
KraftDiner

2006-03-28, 10:00 pm

I have a c++ class for sockets and I want to add support for read
timeouts...
This is what the class looks like now...
I would like to keep the api the same and modify the class to support
the timeout.
When a user calls Read if a timeout occurs an exception is thrown.

Suggestions please?

CSocket(void);
CSocket(int sfd);
~CSocket(void);

void Open(int port);
int Listen(void);
void Connect(char *host, unsigned short port);
int Read(unsigned char *dest, int nbytes);
int Write(const void *source, int nbytes);
void Close(void);

KraftDiner

2006-03-28, 10:00 pm

Adding a setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &new_tv,
sizeof(new_tv));
to the Read method seems to work well...
Simple...
Reading more I've found that you can put a write timeout on the data as
well....
Hmm... Is that in case the peer doesn't read the data you write, you'll
be blocked forever?
So I should put one in the Write method as well?

root

2006-03-28, 10:00 pm

setsockopt() is not a portable way to do this.
Some versions of linux/unix ignore this completely.
I think the correct way is to use select() or poll() to test the status
of the socket fd prior to attempting connect, read, or write.
You could also try doing with SIGALRM, but select() is at least
implemented on Windows.

KraftDiner wrote:
> I have a c++ class for sockets and I want to add support for read
> timeouts...
> This is what the class looks like now...
> I would like to keep the api the same and modify the class to support
> the timeout.
> When a user calls Read if a timeout occurs an exception is thrown.
>
> Suggestions please?
>
> CSocket(void);
> CSocket(int sfd);
> ~CSocket(void);
>
> void Open(int port);
> int Listen(void);
> void Connect(char *host, unsigned short port);
> int Read(unsigned char *dest, int nbytes);
> int Write(const void *source, int nbytes);
> void Close(void);
>

KraftDiner

2006-03-28, 10:00 pm

Well my targets are MAC OS X and Win2k and above...
Compiled and worked first time... on os x.
Do I need to do the setsockopt for each call to read?
Can it be done once after the open and is it good for
every call to read/write. I guess I need to test this..

Do you think the socket class should be implemented as
a thread? Where Read and Write methods work on queued
data? Open, Close start and stop the threads? What
would Listen do? Block?

KraftDiner

2006-03-28, 10:00 pm

I found this on the select call...
and recvfrom...

My question is... Must I use recvfrom or should i stick with read?
By the way I don't have a pointer to good documentation on the
paramters of recvfrom...

The select adds one to its first parameter.. Whats with that?

fd_set fds;
struct timeval tv;

// sock is an intialized socket handle
tv.tv_sec = 2;
tv.tv_usec = 500000;

// tv now represents 2.5 seconds
FD_ZERO(&fds);
FD_SET(sock, &fds); // adds sock to the file descriptor set

// wait 2.5 seconds for any data to be read from any single socket
select(sock+1, &fds, NULL, NULL, &tv);

if (FD_ISSET(sock, &fds))
recvfrom(s, buffer, buffer_len, 0, &sa, &sa_len);
else

Barry Margolin

2006-03-29, 4:04 am

In article <1143582701.997505.135080@j33g2000cwa.googlegroups.com>,
"KraftDiner" <bobrien18@yahoo.com> wrote:

> I found this on the select call...
> and recvfrom...
>
> My question is... Must I use recvfrom or should i stick with read?


If you don't need any of the additional features that recv() or
recvfrom() provide, you can use read().

> By the way I don't have a pointer to good documentation on the
> paramters of recvfrom...


man recvfrom ?

But if you're going to be doing any significant amount of network
programming, you really should buy the book "Unix Network Programming,
Volume 1".

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com