Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
TCL8.4 Sockets not passing data until closed

Posted by Micah_Death (Micah_Death), 2 March 2005
OS = Windows 95

Now something odd is happening...

I create 2 VB6 Apps to test this...
VBApp#1 - Server
 Listen on Port 1001
 Allow incoming connections
 Display data sent to it

VBApp#2 - Client
 Connect to 127.0.0.1:1000
 Send a string

TCL Application
  Accept Connection on port 1000
  Create ConnectionAccept Callback
  On ConnectionAccept:
      Create socket to 127.0.0.1:1001
      Create Data Callback, pass source, destination
   On Data:
       Get Data from source
       send data to destination

Now the problem...
 The client connects fine AND does not error when I pass data to the TCL Script... It does not how ever pass it to the server UNTIL I close the clients connection... When I close the clients connection it pushes everything to the server... How do I make it send to server instantly so that I can make the server send data back to the client?

Script:
Code:
proc Proxy_Init { port } {
  global Proxy
  set Proxy(listen) [socket -server Proxy_Accept $port]
  LogMe "Server Started"
  vwait forever
}

proc Proxy_Accept { sock addr port} {
  global Proxy
  LogMe "Connection Attempt from $addr:$port"
  set Proxy(client) [socket "127.0.0.1" 1001]
  fileevent $sock readable [list Proxy_Client $sock $Proxy(client)]
}

proc Proxy_Client { sock toserver } {
  global Proxy
  if {![eof $sock]} {
     LogMe "Data Received from client"
     set Data [read $sock]
     if {![string length $Data] == 0} {
        LogMe "Sending ($Data) to server"
        puts $toserver $Data
     }
  }
#Added flush but didn't solve problem
  flush $toserver
  flush $sock
}

proc LogMe { text } {
  set fin [open "log.txt" a+]
  puts $fin "$text"
  close $fin
}

set gogo [Proxy_Init 1000]

 
I've recoded this twice from scratch with same problem... The TCL Script won't send the data to the second connection until I close the first connection....

Micah

Posted by admin (Graham Ellis), 3 March 2005
Not an obvious one (you know this, I'm sure   ) but three things strike me:

a) Have you tried using fconfigure and switching buffering mode to none?   (Yes, I see that flush didn't help, but pre-empting it before things get stopped in the buffer may help)

b) Are you certain that the problem is in your Tcl and not in the receiving application?   Is it possible that the receiving application buffers information and only acts on it when it gets a trigger such as a close, or a \r (rather than a \n) or something like that?

c) Earlier Windows operating systems had a very clunky switch from one concurrent application to another - they were described to me as "not really multitasking" by folks but I'm more of a Unix / Linux person and didn't do a deep study myself.  Bearing in mind this reputation, I wonder if your computer is stuck in your Tcl and won't swap to the next application until the Tcl is done in some way / kickc out.  It's possible that action (a) may cure this if it's the problem.   For further invertigation, can you tranfer you code for test purposes to another OS and see if it works there?

Posted by Micah_Death (Micah_Death), 3 March 2005
for a)
I did try fconfigure -buffering none

Didn't seem to help... I'll do it again and may post a little log file to show what it does... (I had this code in place before I scaled down the code looking for the bug... will try again with it to be sure.)

for b)
  \r and \n should have no effect
  (I built the Proxy in VB6 First and then converted to TCL... VB6 version works perfect with the VB test clients and with a real world client (Don't wish to release that information at this time but will if you request it.)
   VB6 Code
Code:
'On DataArrival event
 Dim Buffer as string
 Winsock1.Getdata buffer 'Receive from Client
 Winsock2.Senddata Buffer 'Sends to Server


on c)
I need to test on Win98 or WinXP; so far I have only used Win95...

I'll post back again after I get to test a couple of things.
btw... When I'm done, I'll be redoing it to send Binary Data instead of strings, but strings are WAY easier to test with.[b][/b]

Posted by Micah_Death (Micah_Death), 4 March 2005
as far as A: Goes...
Added:
 fconfigure $sock -buffering none
Effect:
 no effect

Added (as well as the above):
 fconfigure $Proxy(client) -buffering none
Effect:
 no effect

As far as B: goes... VB6 is based only on events so if I do a "Winsock1.Senddata Data" it will send the data, process other commands directly after it and then it's done... No buffering takes place. (Adding buffering to VB is sometimes tidious) -- This is why I don't think it is VB related... [And I made a VB6 Proxy first and everything worked with the app I was using it with]

As far as C: Goes...
 Installed TCL on a Windows 2003 Server Box and copied the TCL Script. Change all references from local host to Windows 95 Machine IP (VB6 Apps still on the Win95 Box, but TCL Script is now on 2003 Server)...

Same Problem... TCL Doesn't send anything to the server until after I close the client...

 Next Test: Run all Apps on 2003 Server
Same Problem......

This leads me to believe its how I'm running the code...
Ways to run code I have tried with same results:
  • Double Clicking on tcl file
  • running tclsh84 and then tclsh scriptname
  • running wish84 and then wish scriptname


Hmm... I'm at a loss.

I noticed something when I first installed TCL on Windows... When I ran a script that did puts <args> I noticed that TCL didn't print anything to the screen until the program exited (Thats Why I use a logfile in above code)

It seemed TCL processed at least some of the data, but output was stopped until I close the TCL App.  




Posted by Micah_Death (Micah_Death), 4 March 2005
Woot! I think I got it!!!

added to fconfigure:
 -blocking 0

And it would push data instantly!!! =)

Thank you for your help =) -- you helped me troubleshoot and sent me in the right direction! =)

Micah

Posted by admin (Graham Ellis), 6 March 2005
on 03/04/05 at 20:00:22, Micah_Death wrote:
Woot! I think I got it!!!

[snip]

Thank you for your help =) -- you helped me troubleshoot and sent me in the right direction! =)

Micah


Micah, so glad that we got it sorted, and many thanks for following up.

As a trainer, I'm always delighted when suggestions and ideas that I make here lead to people finding a solution even when I can't put my finger on it straight away - on of a good trainer's objectives is to help folks learn enough background so that the can go on and solve other issues more easily

Posted by Micah_Death (Micah_Death), 7 March 2005
=)



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho