Here's a simple example using select:
(in this example, since the sockets are all blocking, select is completely
superfluous, but perhaps it will clear something up?)
int main()
{
int Ear, S;
struct sockaddr_in sain;
int len;
/* Set up server socket */
Ear = socket(AF_INET, SOCK_STREAM, 0);
sain.sin_port = htons(5012);
sain.sin_addr.s_addr = htonl(INADDR_ANY);
sain.sin_family = AF_INET;
bind(Ear, (struct sockaddr *) &sain, sizeof(sain));
/* Get the client */
listen(Ear, 1);
S = accept(Ear, (struct sockaddr *)&sain, &len);
close(Ear);
printf("Connected.\n");
/* echo data from the client */
do
{
char st[256];
fd_set RSet;
struct timeval tv;
/* Block using select--
RSet and tv will BOTH be clobbered by select */
FD_ZERO(&RSet);
FD_SET(S, &RSet);
tv.tv_sec = 5;
tv.tv_usec = 0;
select(S + 1, &RSet, NULL, NULL, &tv);
printf("Done select, RSet contains S? %d\n", FD_ISSET(S, &RSet));
/* Read the available data or discover the closed connection*/
len = recv(S, st, 256, 0);
if (len > 0)
send(S, st, len, 0);
} while (len > 0);
printf("Disconnected.\n");
close(S);
}
"...the simple solution is to not do anything stupid as root." - Linus Torvalds
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to [EMAIL PROTECTED]