Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket without endlines

Status
Not open for further replies.

piderman

Programmer
Jan 18, 2005
2
NL
Hi,

I'm trying to make a client for the DC protocol using Tcl/Tk. The major problem is that this protocol doesn't send endlines, but rather the | character to end a command.
This poses great problems. For one, 'gets' will always return an empty string, and 'read' will block when reading more characters than are waiting, even when in nonblocking mode.
Secondly I cannot use the 'fileevent readable' command, because the socket doesn't become unreadable as that is done after reading and endline character (so I gather), so the script is executed only once, at the initiation of the 'fileevent'.

How can I solve this?
 
I suppose you have tried fconfigure <socketID> -translation binary?

Bob Rashkin
rrashkin@csc.com
 
Okay, a couple of things.
Fileevent is not waiting on newlines.
Either SIGIO or polling determine when
data is available, depending on how the
socket is fconfigured.

Bong is right on with the fconfigure advice.
You will end up reading the input as raw bytes
and using the binary command you can translate
your input if necessary.

You can also look at pktsrc if you are developing
for *nix. This package adds support for raw packets
in tcl and is not hard to use.
 
Okay, thanks for the info. I see now I made an error in my program :$. This is the solution I have conjured and it works:

Code:
proc readcommand {s} {
   set command ""
   # just need a character other than | :)
   set char c
   while {$char != "|"} {
      set char [read $s 1]
      append command $char
   }
   #process $command
}

set s [socket $host $port]
fconfigure -blocking 0 -buffering none
fileevent $s readable "readcommand $s"

It appears the '-translation binary' is not necessary. Anyway, my manual says that the translation option does exactly that, it translates but commands like 'gets' still need the endline (\n or \r\n etc) to return the input.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top