You are on page 1of 3

Server:

require 'socket' # Get sockets from stdlib

server = TCPServer.open(8080) # Socket to listen on port 8080

puts "Waiting for the Client to connect...."

client = server.accept # Wait for a client to connect

loop do # Servers run forever

s2 = gets.to_s

client.puts(s2) # Send the time to the client

line = client.gets

puts line.chop

break while line == "end"

end

puts "Client Terminated....."

server.close

Client:
require 'socket' # Sockets are in standard library

hostname = 'localhost'

port = 8080

s = TCPSocket.open(hostname, port)

while line = s.gets # Read lines from the socket

puts line.chop # And print with platform line terminator

s1 = gets.to_s

s.puts(s1)

end

s.close
ClientAdd:

require 'socket' # Sockets are in standard library

hostname = 'localhost'

port = 8080

s = TCPSocket.open(hostname, port)

for i in 0..3 # Servers run forever

s2 = gets.to_s

s.puts(s2)

end

puts "1. Add "

puts "2. Multiply "

puts "Enter your choice : "

ch = gets.to_i

s.puts(ch)

line = s.gets # Read lines from the socket

puts line.chop # And print with platform line terminator

s.close

ServerAdd:
require 'socket' # Get sockets from stdlib

server = TCPServer.open(8080) # Socket to listen on port 8080

puts "Waiting for the Client to connect...."

client = server.accept # Wait for a client to connect

sum=0
mul=1

for i in 0..3

line = client.gets # Read lines from the socket

sum = sum + line.to_i

mul=mul*line.to_i

puts line # And print with platform line terminator

end

line=client.gets

if line.to_i == 1

client.puts(sum)

else

client.puts(mul)

end

client.puts("000")

puts "Client Terminated....."

server.close

You might also like