Version 99 of TkChat

Updated 2011-08-17 10:13:09 by Utkal

Echo Client This implements a client that opens a server connection, sends messages from stdin, receives server replies and sends them to stdout. #!/usr/local/bin/tclsh7.5

# Read data from a channel (the server socket) and put it to stdout # this implements receiving and handling (viewing) a server reply proc read_sock {sock} {

  set l [gets $sock]
  puts stdout "ServerReply:$l"

}

# Read a line of text from stdin and send it to the echoserver socket, # on eof stdin closedown the echoserver client socket connection # this implements sending a message to the Server. proc read_stdin {wsock} {

  global  eventLoop
  set l [gets stdin]
  if {[eof stdin]} {
    close $wsock             ;# close the socket client connection
    set eventLoop "done"     ;# terminate the vwait (eventloop)
  } else {
    puts $wsock $l           ;# send the data to the server
  }

}

# open the connection to the echo server... set eshost "scoda" set esport 9999

# this is a synchronous connection: # The command does not return until the server responds to the # connection request set esvrSock socket $eshost $esport

#if {eof $esvrSock} { # connection closed .. abort }

# Setup monitoring on the socket so that when there is data to be # read the proc "read_sock" is called fileevent $esvrSock readable list read_sock $esvrSock

# configure channel modes # ensure the socket is line buffered so we can get a line of text # at a time (Cos thats what the server expects)... # Depending on your needs you may also want this unbuffered so # you don't block in reading a chunk larger than has been fed # into the socket # i.e fconfigure $esvrSock -blocking off

fconfigure $esvrSock -buffering line

# set up our keyboard read event handler: # Vector stdin data to the socket fileevent stdin readable list read_stdin $esvrSock

# message indicating connection accepted and we're ready to go puts "EchoServerClient Connected to echo server" puts "...what you type should be echoed."

# wait for and handle either socket or stdin events... vwait eventLoop

puts "Client Finished"

Another option is to do an asynchronous client connection

set esvrSock socket -async $eshost $esport

# .... do whatever that we can't connect synchronously...

# resync with the connection, #Socket becomes writable when connection available fileevent $esvrSock writable { set connect 1 } vwait connect

    # will 'block' here till connection up (or eof or error)

fileevent $esvrSock writable {} ;# remove previous handler

if {eof $esvrSock} { # connection closed .. abort }

# set translation, buffering and/or blocking modes fconfigure $esvrSock -translation {auto crlf} -buffering line

    ...


Echo Server Server that reflects its client messages back to the source

#!/usr/local/bin/tclsh7.5

set svcPort 9999

# Implement the service # This example just writes the info back to the client... proc doService {sock msg} {

    # puts $sock "echosrv:$l"
     puts $sock "$l"

}

# Handles the input from the client and client shutdown proc svcHandler {sock} {

  set l [gets $sock]    ;# get the client packet
  if {[eof $sock]} {    ;# client gone or finished
     close $sock        ;# release the servers client channel
  } else {
    doService $sock $l
  }

}

# Accept-Connection handler for Server. # called When client makes a connection to the server # Its passed the channel we're to communicate with the client on, # The address of the client and the port we're using # # Setup a handler for (incoming) communication on # the client channel - send connection Reply and log connection proc accept {sock addr port} {

  # if {[badConnect $addr]} {
  #     close $sock
  #     return
  # }

  # Setup handler for future communication on client socket
  fileevent $sock readable [list svcHandler $sock]

  # Read client input in lines, disable blocking I/O
  fconfigure $sock -buffering line -blocking 0

  # Send Acceptance string to client
  puts $sock "$addr:$port, You are connected to the echo server."
  puts $sock "It is now [exec date]"

  # log the connection
  puts "Accepted connection from $addr at [exec date]"

}

# Create a server socket on port $svcPort. # Call proc accept when a client attempts a connection. socket -server accept $svcPort vwait events ;# handle events till variable events is set

I can not send/receive message from client/server