Hello Victor:

I think you don't have well the sequence, in your code:

Server :    Client
Write  ---> Send
Gets   ---> Recv
Write  ---> Recv

You must do:

Server :    Client
Write  ---> Recv
Gets   ---> Puts
Write  ---> Recv

Take on account too, if you use Write you must use Recv in the another side.
If use Puts you needs to use Gets.


This code works on my pc:

---Server---

require 'socket'

port = 50

server = TCPServer.new("", port)

while (session = server.accept)
        session.write("Communication Established!!!")
        input = session.gets
        system("#{input}")
        session.write("Request Received From Client:#{input}")
        session.close
end

---Client---


require 'socket'


streamSock = TCPSocket.new( '127.0.0.1', 50 ) 

str = streamSock.recv( 100 ) 
print str 
puts "\n"
streamSock.puts("dir\n")
str = streamSock.recv( 100 )
print str
puts "\n"

streamSock.close

I need to change the port, I think this is for use with UDP on windows, but
I'm not sure.

Regards,

Jovino Rguez

P.D.: Eres hispano-parlante?

-----Mensaje original-----
De: Victor Reyes [mailto:victor.reyes / gmail.com] 
Enviado el: jueves, 20 de septiembre de 2007 19:22
Para: ruby-talk ML
Asunto: Couple of questions about TCPServer.new and TCPSocket.new

Hello Team,

In an attempt to learn about TCP Socket programming, I wrote the following
mini tiny ruby pgms.
Although they kind of work, I am having some problems. Here they are:

This is under AIX 5.3.

ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [powerpc-aix5.3.0.0]

1 - I start the server side first.
2 - Start client side.
3 - The client gets the msg session.write("Communication Established!!!").
4 - The server just "sits" there, when I expect the output of the date
command on the server console.
5 - If kill the client side, the server now executes the date command and
the actual output is displayed.
6 - The client side never gets the last msg from the server side.
7 - I kill the server side.

Your help will be appreciated.

SERVER SIDE

require 'socket'

port = 19557

server = TCPServer.new("", port)

while (session = server.accept)
        session.write("Communication Established!!!")
        input = session.gets
        system("#{input}")
        session.write("Request Received From Client:#{input}")
        session.close
end


CLIENT SIDE

require 'socket'

streamSock = TCPSocket.new( 'nycserv-deva-app1nim', 19557 )
streamSock.send( "date\n", 1937 )
str = streamSock.recv( 100 )
print str
puts "\n"
str = streamSock.recv( 100 )
print str
puts "\n"
streamSock.close


ACTUAL RUN:


Server:
root@app1nim:/u/victor/executables/ruby>ruby ts5  (Here it just waits)


Client:
root@app2nim # ruby tc5
Communication Established!!!

NOW I KILL THE CLIENT SINCE NOTHING IS GOING ON ON THE SERVER:

tc5:8:in `recv': Interrupt
        from tc5:8
===> /u/victor/executables/ruby
root@app2nim #


NOW THE SERVER DISPLAY THESE MSGS:

ts5:10: warning: Insecure world writable dir /usr/local in PATH, mode
0240777
Thu Sep 20 13:18:56 EDT 2007


Thank you

Victor