For Programmers: Free Programming Magazines  


Home > Archive > Tcl > December 2006 > How to use [read] command to read data from a socket?









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 How to use [read] command to read data from a socket?
prabu

2006-12-18, 8:09 am

Hi all,

When using [read] command to read data from a client through socket,
the server hangs. ( set fdata [read $sock] ) But works well while using
[gets] to read. What may be wrong?

Regards,
Prabu

peace07

2006-12-18, 8:09 am


prabu schrieb:

> Hi all,
>
> When using [read] command to read data from a client through socket,
> the server hangs. ( set fdata [read $sock] ) But works well while using
> [gets] to read. What may be wrong?
>
> Regards,
> Prabu


Hi Prabu,
more info is needed to answer your question. The most important to me
is, what
kind of server do you mean (build by your own, webserver, app-server)?
I assume, that you might mean a server, build with Tcl, using the
socket -server ...
command. In this case, you must have in mind, that the server does not
handle the request multithreaded or with multiple processes (fork) but
just in one process, using the select() mechanism "below" (the
C-language level). So it might be, that your server reads from a socket
and misses the EOF - so the read call blocks the server application.
So, a lot of maybes and assumptions - so hopefully you can bring in
some more light.
Regards,
Friedemann

Uwe Klein

2006-12-18, 8:09 am

prabu wrote:
> Hi all,
>
> When using [read] command to read data from a client through socket,
> the server hangs. ( set fdata [read $sock] ) But works well while using
> [gets] to read. What may be wrong?


assumed your socket is in blocking mode:
[read $socket buffer ] will read all input until EOF and then return.
[gets $socket linebuffer ] will read until the next <linefeed> and then return.

You can use read with number_of_bytes to read specified
[ read $socket buffer $numchar]
this will return on EOF or $numchar read.

or you can use the socket in nonblocking mode.
[ fconfigure $socket -blocking false ]
now read will return imediately having read the input currently available.
You will have to assemble the lumps of input on your own.

uwe






>
> Regards,
> Prabu
>

Sponsored Links







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

Copyright 2008 codecomments.com