Archive for Oktober, 2008
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.


**** Info: In Kürze gibt es ein erstes Update der n-tv mobil Applikation. Der Startvorgang wird durch eine überarbeitete Startseite erheblich beschleunig...
Finde heraus, ob du über das Thema Sex wirklich Bescheid weißt.
Anhand dieses Tests kannst du überprüfen, ob du tatsächlich so viel über Sex weißt,...
Du kannst den tollsten virtuellen Hund entwickeln und ihn deinen Freunden und der ganzen Welt zeigen!
Adoptiere deinen eigenen streichelbaren, liebenswer...
PLEASE NOTE: This is the free version of the Unblock Me game with a limited puzzles and difficult levels. The following features are available only in the ...
Mit MobileTV von T-Mobile haben Sie beste TV-Unterhaltung jederzeit auf Ihrem iPhone 3G/3GS.
T-Mobile bietet Ihnen eine breite Auswahl an Kanälen an:
? d...
Unter dem Motto ?Entdecke Deinen Ort? bietet DasÖrtliche nun auch auf dem iPhone die gewerblichen und privaten Verzeichniseinträge Deutschlands in gewohn...
Sarkasmus pur...
Deutschland steckt in der Krise und das iPhone verkauft sich wie geschnitten Brot.
Da liegt es doch nahe, sich auf dem iPhone anzusehen,...
Roll-out with the FREE TRANSFORMERS digital BUMBLEBEE action figure! Touch, swipe and drag Bumblebee to change from vehicle to robot mode and back again. P...
Connect two iPhones 3G via Bluetooth to build an endless racing track.
The Endless Racing Game is one of the first multiplayer games, where the game acti...
Think you have a good vocabulary? Take on your iPhone or a friend in this chalkboard classic and find out how good your vocabulary really is.
Hangman Fre...