Felip Alàez Nadal | 26 Aug 17:49

Something strange with read on sockets. Help needed

Hello:

I'm in trouble with sockets. I've created a little multithreaded server class which I plan to use for games. I have problems reading from sockets. I create the server and, when It makes an accept(), returns an Stdio.File object which represents the socket, don't? Then the server can write to the socket. I know that the server works because I can do a telnet and connect to It. My problem is that I can't read anything from the socket. If I do mySocket->read(), It never returns. I write on the telnet terminal and the server never returns from the call to mySocket->read(). I also wrote a little telnet program in pike and I have the same problem: the server writes to the telnet program, but can't read anything from It.

Can somebody explain me which is the correct way to read data from a socket?

Thanks you.

--
Felip Alàez Nadal
Bill Welliver | 26 Aug 19:33
Gravatar

Re: Something strange with read on sockets. Help needed

Telnet can be a problem, as it doesn't necessarily send each character as 
you type it. You might also be asking for too much data to be read 
(depending on the system).

You might try playing with alternate arguments to read() to see if you get 
the results you require:

string read(int len, int not_all);

the second argument may be interesting: if 1, read() will return 
immediately, but you might not get any or all of the data you requested.

Bill

On Tue, 26 Aug 2008, Felip Alàez Nadal wrote:

> Hello:
>
> I'm in trouble with sockets. I've created a little multithreaded server
> class which I plan to use for games. I have problems reading from sockets. I
> create the server and, when It makes an accept(), returns an Stdio.File
> object which represents the socket, don't? Then the server can write to the
> socket. I know that the server works because I can do a telnet and connect
> to It. My problem is that I can't read anything from the socket. If I do
> mySocket->read(), It never returns. I write on the telnet terminal and the
> server never returns from the call to mySocket->read(). I also wrote a
> little telnet program in pike and I have the same problem: the server writes
> to the telnet program, but can't read anything from It.
>
> Can somebody explain me which is the correct way to read data from a socket?
>
> Thanks you.
>
> -- 
> Felip Alàez Nadal
>
Felip Alàez Nadal | 26 Aug 20:30

Re: Something strange with read on sockets. Help needed



2008/8/26 Bill Welliver <hww3 <at> riverweb.com>
You might also be asking for too much data to be read (depending on the system).

You might try playing with alternate arguments to read() to see if you get the results you require:

string read(int len, int not_all);

Thanks you! The problem was that "read" tried to read too much  data everytime. Reading 1 character at time It works.

There's any way to create a ( buffered ) Stdio.FILE object from an ( unbuffered ) Stdio.File object, like that which accept returns?

Thanks.


--
Felip Alàez Nadal
Bill Welliver | 26 Aug 21:04
Gravatar

Re: Something strange with read on sockets. Help needed

you might want to try using callback mode, where you specify a function to 
be called when data is available. The only catch is that you have to be 
running in the backend, which is probably not a problem for your 
application.

Not sure about your particular question; I've always handled it manually 
with an incoming buffer and a read callback.

Bill

On Tue, 26 Aug 2008, Felip Alàez Nadal wrote:

> Thanks you! The problem was that "read" tried to read too much  data
> everytime. Reading 1 character at time It works.
>
> There's any way to create a ( buffered ) Stdio.FILE object from an (
> unbuffered ) Stdio.File object, like that which accept returns?

Gmane