Programming Freaks  | دورات ومقالات برمجيه

Please login or register.

Login with username, password and session length
Advanced search  

News:

Please Read our FAQ

Author Topic: Simple fortune server-client  (Read 651 times)

abom

  • Ruby Fr34k
  • Moderator
  • Just Joined
  • *****
  • Posts: 20
    • View Profile
    • Email
Simple fortune server-client
« on: April 24, 2009, 11:51:07 AM »
Hi all,  :)
This is a very simple cmd fortune server-client (runs locally) using 'socket'.
I got some fortunes form the example of fortune server at Qt4 docs.

Here you are (change the port as you want!  ???).

Server:

Code: [Select]
# a simple fortune server
# author: abdelrahman ghanem
## i got some fortunes form Qt4 docs
## and some form the net
require 'socket'

fortunes = ["You've been leading a dog's life. Stay off the furniture.",
            "You've got to think about tomorrow.",
    "You will be surprised by a loud noise.",
    "You will feel hungry again in another hour.",
    "You might have mail.",
    "You cannot kill time without injuring eternity.",
    "Computers are not intelligent. They only think they are.",
    "Behind every able man is an able woman.",
    "Speak only well of people and you need never whisper",
    "Promotion coming soon",
    "Every man is a volum if you know how to read him",
    "A good time to start something new",
    "Never trouble trouble till trouble troubles you",
    "You never hesitate to tackle the most difficult problems",
    "The road to knowledge begins with the turn of the page",
    "Answer just what yor heart prompts you",]
begin
  idx = 0
  server = TCPServer.open 5555
  puts "Server is running at port ----> #{5555}, Now run the client."
  cl=server.accept
  loop do
    cl.puts fortunes[idx]
    if idx < fortunes.length-1
      idx += 1
    elsif idx == fortunes.length-1
      idx = 0
    end
  end
rescue Exception => e
    puts e.message
end

Client:

Code: [Select]
# a simple fortune client
# author: abdelrahman ghanem
require 'socket'

begin
  cl = TCPSocket.open("localhost", 5555)
  loop do
    print "Get furtune? [Hit Enter=Get/Q=Quit]: "
    rep = gets.downcase
    if rep == "\n"
      puts cl.gets
    elsif rep == "q\n"
      exit 0
    else
      puts "unknown answer!"
    end
  end
rescue Exception => e
  puts e.message
end

See U  :)
Attached
« Last Edit: April 24, 2009, 11:57:54 AM by abom »
Logged

Dz_V!rUs

  • Just Joined
  • *
  • Posts: 2
    • View Profile
Re: Simple fortune server-client
« Reply #1 on: May 28, 2009, 05:26:46 PM »
thank uu for this
your freind Dz_V!rUs
Logged