In this task we have to work with byte ordering and ways to send/receive them. I wouldn't tell about endianness because there are a lot of good internet sources that cover it. (e.g. wiki ).
Task binary sends a number and waits when we will provide little endian version of it. Our task is to get number from incoming message, convert it into binary form and then send it backwards. To get numbet you will probably want to use regular expression which will cut out contiguous digit string between quotation marks. Then there is a fast solution in Ruby to use pack method of the Array class which allows to convert integer from string to binary. Then we will just send it and receive congratulation message.
Code:
Task binary sends a number and waits when we will provide little endian version of it. Our task is to get number from incoming message, convert it into binary form and then send it backwards. To get numbet you will probably want to use regular expression which will cut out contiguous digit string between quotation marks. Then there is a fast solution in Ruby to use pack method of the Array class which allows to convert integer from string to binary. Then we will just send it and receive congratulation message.
Code:
#!/usr/bin/ruby1.9.1 require "socket" sock = TCPSocket.new('127.0.0.1','2999') string = sock.gets puts string result = string.match(/.*\'(\d+)\'.*/) i = [result[1].to_i].pack('l') h = result[1].to_i.to_s(16) puts "Sending #{h} which is #{i} with length #{i.length}" sock.puts i puts sock.gets sock.closeProof:
No comments:
Post a Comment