Search By Google
Komentar
    Kalender
    Oktober 2008
    S S R K J S M
        Nop »
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
    Ngobrol Yuks
    Category
    Buy Your Wordpress E-commerce Theme. Buy Now



    Proactol







    UniqueHoodia



    PenisHealth



    MoreNiche



    SizeGenetics



    AppStore Charts

    Archive for Oktober, 2008

    Dengerin lagu ini, Inget Jadul

    Selasa, Oktober 28th, 2008

    Cinta kita - Amy Search and Inka Christie

    Catatan Socket di Ruby

    Selasa, Oktober 28th, 2008

    The Date Time Server and Client

    Now let us build a Ruby-based Date Time server and client that displays on the client computer the date and time at the server location, using the Ruby socket API. Here’s the code for the Date Time Server - p068dtserver.rb

    1. # p068dtserver.rb
    2. # Date Time Server - server side using thread
    3. # usage: ruby p068dtserver.rb
    4. require “socket”
    5. dts = TCPServer.new(’localhost’, 20000)
    6. loop do
    7. Thread.start(dts.accept) do |s|
    8. print(s, ” is accepted\n”)
    9. s.write(Time.now)
    10. print(s, ” is gone\n”)
    11. s.close
    12. end
    13. end

    # p068dtserver.rb # Date Time Server - server side using thread # usage: ruby p068dtserver.rb require “socket” dts = TCPServer.new(’localhost’, 20000) loop do Thread.start(dts.accept) do |s| print(s, ” is accepted\n”) s.write(Time.now) print(s, ” is gone\n”) s.close end end

    Explanation of the p068dtserver.rb code:

    You first load the socket library with the require command. The TCPServer class is a helper class for building TCP socket servers. The TCPServer.new(’localhost’, 20000) statement creates a new socket identified by localhost and port number. The Thread.start creates and runs a new thread to execute instructions given in block. Any arguments passed to Thread.start are passed into the block. The dts.accept method waits for a connection on dts, and returns a new TCPSocket object connected to the caller. The Kernel.loop iterator calls the associated block do..end) forever (or at least until you break out of the loop). We use s.write(Time.now) to write the current date and time on the server to the socket. Finally, we write s.close to close a socket using the close method. This method is inherited from the IO class, but it’s available for each of the socket types.

    Running the server

    Since we are testing these programs on our local machine, open a new command window, go to the folder containing your p068dtserver.rb program and type ruby p068dtserver.rb. The program runs and waits for a client to connect at port 20000.

    Here’s the code for the Date Time Client - p069dtclient.rb

    1. # p069dtclient.rb
    2. require ’socket’
    3. streamSock = TCPSocket.new( “127.0.0.1″, 20000 )
    4. #streamSock.send( “Hello\n” )
    5. str = streamSock.recv( 100 )
    6. print str
    7. streamSock.close

    # p069dtclient.rb require ’socket’ streamSock = TCPSocket.new( “127.0.0.1″, 20000 ) #streamSock.send( “Hello\n” ) str = streamSock.recv( 100 ) print str streamSock.close

    Explanation of p069dtclient.rb code:

    You first load the socket library with the require command. The statement sock = TCPSocket.new(’127.0.0.1′, 20000) opens a TCP connection to localhost on port 20000. The statement str = sock.recv(100) receives up to 100 bytes from sock. We display the date-time string received from the server and finally we close the socket.