Chat server telnet client problem

Discussion in 'Ruby' started by Dhiraj Girdhar, Jul 5, 2007.

  1. Hi,
    I copied chat server code from one or Ruby site.
    Following code is working fine But I am facing very small problem, when
    i am launching any client using telnet, then I am not able to see what I
    am typing on client console.
    example :
    > telnet localhost 7666


    Please help me to find out the problem.


    Regards:
    Dhiraj

    require 'socket'
    class ChatServer

    def initialize(port) #Instantiating the server
    @sockets_in = Array.new
    @serv_sock = TCPServer.new("",port)
    @serv_sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1)
    puts "Chat server started on port #{port}."
    @sockets_in.push(@serv_sock)
    end

    def run # The runtime code for the server
    while true
    someone_there = select(@sockets_in, nil, nil, nil)
    if someone_there != nil #Someone IS there, maybe wanting to
    talk?
    for socket in someone_there[0] #Check the sockets array
    #Do we have a connect,...
    if socket == @serv_sock then
    accept_new
    else
    #...or did they leave,...
    if socket.eof? then #If it sends an end of file notice
    - client disconnected.
    str = "Client left
    #{socket.peeraddr[2]}:#{socket.peeraddr[1]}."
    broadcast_string(str,socket)
    socket.close
    @sockets_in.delete(socket)
    else
    #...or are they talking?
    str = "#{socket.peeraddr[2]}:#{socket.peeraddr[1]}
    says: #{socket.gets}"
    broadcast_string(str,socket)
    end
    end
    end
    end
    end
    end

    private

    def broadcast_string(str,omit_sock) #Broadcasts the string to all
    available receiving sockets.
    @sockets_in.each do |client_sock| #each socket in the @sockets_in
    array do this:
    if client_sock != @serv_sock && client_sock != omit_sock #if
    it's not the server socket, and not the one that sent it...
    client_sock.write(str) #...write it to the client.
    end
    end
    #and just to keep track on the server end...
    puts(str)
    end

    def accept_new #accepts a new connection.
    new_sock = @serv_sock.accept
    @sockets_in.push(new_sock) #add new socket to sockets array.
    new_sock.write("Welcome to Ruby Chat!") #let them feel welcome.
    str = "Someone joined
    (#{new_sock.peeraddr[2]}:#{new_sock.peeraddr[1]})."
    broadcast_string(str,new_sock) #let everyone know someone came in.
    end

    end

    myChat = ChatServer.new(7680)
    myChat.run

    --
    Posted via http://www.ruby-forum.com/.
    Dhiraj Girdhar, Jul 5, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    2,193
  2. Jaspreet
    Replies:
    0
    Views:
    686
    Jaspreet
    Sep 18, 2004
  3. Marlo
    Replies:
    5
    Views:
    743
    Marlo
    Aug 11, 2006
  4. Jim Isaacson
    Replies:
    5
    Views:
    603
    Default User
    Nov 5, 2004
  5. Carcarius
    Replies:
    0
    Views:
    294
    Carcarius
    Dec 6, 2007
Loading...

Share This Page