-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_tcp_server.rb
54 lines (43 loc) · 1.46 KB
/
simple_tcp_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'socket'
class SimpleTCPServer
def initialize(host, port)
@server = TCPServer.new(host, port)
puts "Server started on #{host}:#{port}"
end
def start
loop do
client = @server.accept
Thread.new(client) do |client_socket|
handle_client(client_socket)
end
end
end
private
def handle_client(client)
loop do
request = client.gets&.chomp
request = request.gsub("-e", "").gsub(" ", "") if request
puts "handle_client request:#{request.inspect}"
break if request.nil? || request.empty?
puts "Received request: #{request.inspect}"
response = case request
when 'S' then "Weight: 75kg, You look awesome, don't worry about the weight"
when 'TIM' then "Time: #{Time.now}"
when 'SI' then "SI Response, You look awesome, don't worry about the weight"
when 'S' then "S response, You look awesome, don't worry about the weight"
else "Unknown command"
end
client.puts response
puts "Sent response: #{response}"
client.close if request == 'exit' # Close the connection if 'exit' command is received
end
rescue StandardError => e
puts "Error handling client: #{e.message}"
ensure
client.close if request == 'exit'
# client.close unless client.closed?
end
end
# Start the server on localhost and port 8080
server = SimpleTCPServer.new('localhost', 8082)
server.start