UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2002-07-16 12:07 AM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: Ejemplos de uso de "select()"

int socket_fd, result;
fd_set readset;
...
/* Socket has been created and connected to the other party */
...

/* Call select() */
do {
   FD_ZERO(&readset);
   FD_SET(socket_fd, &readset);
   result = select(socket_fd + 1, &readset, NULL, NULL, NULL);
} while (result == -1 && errno == EINTR);

if (result > 0) {
   if (FD_ISSET(socket_fd, &readset)) {
      /* The socket_fd has data available to be read */
      result = recv(socket_fd, some_buffer, some_length, 0);
      if (result == 0) {
         /* This means the other side closed the socket */
         close(socket_fd);
      }
      else {
         /* I leave this part to your own implementation */
      }
   }
}
else if (result < 0) {
   /* An error ocurred, just print it to stdout */
   printf("Error on select(): %s\", strerror(errno));
}

Offline

#2 2002-07-17 11:22 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Ejemplos de uso de &quot;select()&quot;

Smartass...  trying to give us a taste of what it is like for you to read the FAQ in English, eh?!  ;-)

At least I could read enough to understand you put a comment in about the code comments using both C and C++ styles ...

Btw, just a quick glance but the second example has one minor typo that needs to be changed...  in the area where you are turning blocking back on...  the current is:

      if (iof = fcntl(fd, F_GETFL, 0) != -1)
         fcntl(fd, F_SETFL, iof);

The second line should be:

         fcntl(fd, F_SETFL, iof &amp; (~O_NONBLOCK));

I will be out of town for the next couple days...  going to  (Chelmsford,) Massachussetts for a couple of interviews... enough of this doing contract work and having companies self destruct / go bankrupt on me...   Think of it Rob, we COULD end up being almost neighbors...  scary, huh?!   lol.   

See you guys when I return... and have fun with EM...  .  ;-)

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2002-07-17 09:06 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: Ejemplos de uso de &quot;select()&quot;

Michael: I thought you said it would be good to have a multilingual FAQ? I see you understand a great deal of spanish...

About the typo you got, it has been corrected. In fact, I copied the first two lines of code ad forgot to remove the "= fcntl(...)", the idea is to restore the original socket options:

if &#40; &#40;iof = fcntl&#40;fd, F_GETFL, 0&#41;&#41; != -1&#41;
         fcntl&#40;fd, F_SETFL, iof | O_NONBLOCK&#41;; 
      recv&#40;&#41;;
      if &#40;iof != -1&#41;
         fcntl&#40;fd, F_SETFL, iof&#41;;


I am trying to be as similar to recv() as possible, so, if the caller has a non-blocking socket, after the call the socket should keep the same options...

Good luck in Massachussetts Michael, and may the force be with you.
I hope to hear good news soon.

I don't want to talk about EM right now!!!

Offline

#4 2002-07-19 07:00 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Ejemplos de uso de &quot;select()&quot;

Sheez, I was trying to be funny Loco... in regard to the comment about making us suffer by reading the Spanish version... ;-)

On the correction...  I conceed that I actually think your way is faster...  i.e. holding the old option value in a local variable so you only have to call the fcntl(...) function once instead of twice...

On the speaking Spanish side of things...  I have taken about 6 classes / courses for the language over the years... but 1) I have always been told I had the world's WORSE accent... lol and 2) I've never been able to really use it in day to day life so I think I have forgotten about 95% (or more)  of it... 

Very very sad :-(

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#5 2002-07-19 09:22 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: Ejemplos de uso de &quot;select()&quot;

Michael,

Sorry if I wrote it incorrectly. I never meant that I misunderstood your phrase. I was just trying to follow the same line (being funny), I'll be more careful next time. No hard feelings ;)

About our languages, Spanish is very, very difficult for others to learn because it is grammatically rich, however, it is easier to pronounce. The vowel sounds are plain A, E, I, O, U. They don't sound different like in english:

A = A big open A like in fAther
E = A big open E like in fEAther
I  = A close I like in fEEt
O = A flat open O, like in OUght
U = A flat closed U, like in fOOt

I hope this helps you remember.
The difficult part is reading or writting it, each verb can be conjugated in so different ways (unlike english that has 3 o 4 ways, and only related to time)

For example, to swim in spanish is NADAR:

I swim  --> YO NADO
You swim --> TU NADAS
He/She/It swims --> EL/ELLA/ELLO NADA
We swim --> NOSOTROS NADAMOS
You swim --> VOSOTROS NADAIS
They swim --> ELLOS NADAN

Quite funny, eh?

Well, enough for the spanish course. Thank you for your comments.

Offline

Board footer

Powered by FluxBB