In Ruby, how to read UTF-8 from a socket?

When a server sends UTF-8 bytes, how do you read them without characters becoming pure bytes? (\ x40 etc.)

+5
source share
2 answers

I believe that read_nonblockuses read, which in turn says:

The given string always encodes ASCII-8BIT.

This means that you do not need to specify IO#set_encoding, but you can, after you read the integer string , force its encoding (using String#force_encoding!) to UTF-8.

"", , , , UTF-8, Ruby .

+3

IO # set_encoding, UTF-8.

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require 'socket'

server_socket = TCPServer.new('localhost', 0)
Thread.new do
  loop do
    session_socket = server_socket.accept
    session_socket.set_encoding 'ASCII-8BIT'  
    session_socket.puts "แš€ แš แš‚ แšƒ แš„ แš… แš† แš‡ แšˆ แš‰ แšŠ แš‹ แšŒ แš"
    session_socket.close
  end
end

client_socket = TCPSocket.new('localhost', server_socket.addr[1])
client_socket.set_encoding 'UTF-8'
p client_socket.gets
# => "|แš€ แš แš‚ แšƒ แš„ แš… แš† แš‡ แšˆ แš‰ แšŠ แš‹ แšŒ แš\n"
+4

All Articles