Networking Forums

Networking Forums > Computer Networking > Linux Networking > Strange problem with select (or maybe me :-))

Reply
Thread Tools Display Modes

Strange problem with select (or maybe me :-))

 
 
pistmaster@googlemail.com
Guest
Posts: n/a

 
      09-17-2008, 06:56 PM
EDIT: not sure if this got through the first time!

I have written a server which accepts a connect and then reads ad
nauseum until the other end closes the connection. This works fine
when using netcat or telnet normally, except when I echo a value into
the telnet program using a pipe, like so:
echo Hello | telnet 127.0.0.1 8080

The program reads the first input ok, but then when I type in
subsequent input normally in the telnet session does not get read.
I'm probably doing something stupid so I post a short example program
for people to have a look at:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <errno.h>

static char buffer[512];

int main()
{
struct sockaddr_in sn;
struct timeval tm;
int r, _1, client, server = socket(AF_INET, SOCK_STREAM, 0);
if (server < 0) {printf("errno = %s\n", strerror(errno)); exit(1);}

_1 = 1;

r = setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &_1, sizeof(_1));
if (r != 0) { close(server); printf("errno = %s\n", strerror(errno));
exit(1); }

memset(&sn, 0, sizeof(sn));
sn.sin_family = AF_INET;
sn.sin_addr.s_addr = inet_addr("0.0.0.0");
sn.sin_port = htons(8080);

r = bind(server, (struct sockaddr*) &sn, sizeof(sn));
if (r != 0) { close(server); printf("errno = %s\n", strerror(errno));
exit(1); }

r = listen(server, 5);
if (r != 0) { close(server); printf("errno = %s\n", strerror(errno));
exit(1); }

tm.tv_usec = 1000;

client = accept(server, NULL, NULL);
if (client < 0) { close(server); printf("errno = %s\n",
strerror(errno)); exit(1); }

{
fd_set in, out, err;
FD_ZERO(&in);
FD_ZERO(&out);
FD_ZERO(&err);
for(;
{
FD_SET(client, &in);
FD_SET(client, &err);

r = select(client+1, &in, &out, &err, NULL);

printf("select returned %d\n", r);

if (r > 0)
{
if (FD_ISSET(client, &err))
{
printf("errno = %s\n", strerror(errno));
close(client);
close(server);
exit(5);
}

if (FD_ISSET(client, &in))
{
ssize_t bytes_transfered = read(client, buffer, 512);
if (bytes_transfered == 0) { printf("errno = %s\n",
strerror(errno)); close(client); close(server); exit(3); }
if (bytes_transfered < 0) { printf("errno = %s\n",
strerror(errno)); close(client); close(server); exit(4); }
printf("received %d bytes\n", bytes_transfered);
}

} else if (r < 0)
{
printf("errno = %s\n", strerror(errno));
close(client);
close(server);
exit(2);
}
}
}

return 0;
}

 
Reply With Quote
 
 
 
 
h.stroph
Guest
Posts: n/a

 
      09-17-2008, 08:59 PM
In news:9fd3cdd0-e942-426f-b4f6-(E-Mail Removed),
(E-Mail Removed) <(E-Mail Removed)> typed:

> echo Hello | telnet 127.0.0.1 8080
>
> The program reads the first input ok, but then when I type in
> subsequent input normally in the telnet session does not get read.


Of course not; you instructed telnet to accept stdin from the pipe, not the
keyboard.


 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange MTU Problem Geoff Lane Home Networking 0 05-24-2008 09:04 AM
Strange problem: no problem with Linux, when I boot windows 2K network is down... Santa Linux Networking 11 11-29-2004 06:46 AM
Unable to select a static IP address - Bizarre problem Vinnie Windows Networking 2 02-27-2004 10:16 AM
strange problem puzzled Windows Networking 1 01-16-2004 06:37 PM
Strange 802.11b problem Bob Wireless Internet 7 07-01-2003 04:45 AM



1 2 3 4 5 6 7 8 9 10 11