Chat client

D

Daniel Flores

hello i was learning about ruby and on the book i am reading it shows
the part where i can create a chatserver with Gserver

well lookingon the internet shows the server code everywhere.

but when i am trying to do a chat client it always failed on me.
i wouldl ike that it works like a conection with telnet or with netcat.

here is the failling code:

require 'socket' # Sockets are in standard library

host = 'localhost'
port = 1234

while line = s.gets # Read lines from the socket
puts line # And print with platform line terminator
s.puts gets
end
s.puts "Connection close!!"
s.close


THANK YOU in advance
 
J

Jesús Gabriel y Galán

hello i was learning about ruby and on the book =A0i am reading it shows
the part where i can create a chatserver with Gserver

well lookingon the internet shows the server code everywhere.

but when i am trying to do a chat client it always failed on me.
i wouldl ike that it works like a conection with telnet or with netcat.

here is the failling code:

require 'socket' =A0 =A0 =A0# Sockets are in standard library

host =3D 'localhost'
port =3D 1234

while line =3D s.gets =A0 # Read lines from the socket
=A0puts line =A0 =A0 =A0# And print with platform line terminator
=A0s.puts gets
end
s.puts "Connection close!!"
s.close

For me this code fails with this error:
test_socket.rb:6: undefined local variable or method `s' for
main:Object (NameError)

obviously, because you haven't assign anything to s. Do you have a
different version of your program, and what are the errors you are
getting?

Jesus.
 
D

Daniel Flores

Fabian said:
Daniel, you need to include more info.

What errors are you getting?

I am sorry!!!

here is the failling code:

require 'socket' # Sockets are in standard library

host = 'localhost'
port = 1234

s = TCPSocket.open(host, port)

while line = s.gets # Read lines from the socket
puts line # And print with platform line terminator
s.puts gets
end
s.puts "Connection close!!"
s.close

Well i guess it works now, but there is a problem the code becuase the
cliente waits for the server to reply in order to place more data and
the other way around.

There is a way to be waiting both I/O and react depending on which was
activated with data?

I saw that Select might be an alternative but i havent found an example
that guide me through the SELECT options.

Thank you in advance!
 
M

mike coudrain

sorry about necro posting, but just saw this thread ^.^'
and thought I'd post a possible answer, tho im sure you found this out by now but to help anyone else who happens by it like I did..

not sure ur OS but ive read that in windows 'gets' seems to block the code. So you'd have to find an alternate I/O. You could try threading 'gets' tho, Im having trouble with threads myself and cant be too much help with it lol
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top