# Couple of questions about TCPServer.new and TCPSocket.new

**URL:** <https://www.ruby-forum.com/t/couple-of-questions-about-tcpserver-new-and-tcpsocket-new/115118>\
**Category:** Ruby\
**Created:** [September 20, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/couple-of-questions-about-tcpserver-new-and-tcpsocket-new/115118 "2007-09-20T19:23:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [September 20, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/couple-of-questions-about-tcpserver-new-and-tcpsocket-new/115118/1 "2007-09-20T19:23:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [September 20, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/couple-of-questions-about-tcpserver-new-and-tcpsocket-new/115118/2 "2007-09-20T20:46:22Z")

</div>

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 R. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
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

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [September 21, 2007, 12:31pm UTC](https://www.ruby-forum.com/t/couple-of-questions-about-tcpserver-new-and-tcpsocket-new/115118/3 "2007-09-21T12:31:24Z")

</div>

Jovino,

Thank you for your help. It works as explained.  
By the way, do you know of a good reading source where I can get more  
info  
about ruby network programming/support?

Also, SÃ­, soy latino, especÃ­ficamente soy de la tierra del Merengue y la  
Bachata, la RepÃºblica Dominicana. Pero Vivo en Estados Unidos (Brooklyn)  
desde 1974.

Gracias de nuevo por tu ayuda!

VÃ­ctor
