zoukankan      html  css  js  c++  java
  • 使用Ruby进行文件分发之客户端构建

    代码原理很简单,就是客户端与服务器建立连接,然后进行数据的传输,如果要说有难点的地方的估计也就是一些文件处理方式上,下面就是代码:

    客户端:

    客户端中使用加载socket库,我这里用的是tcpsocket,向服务器发送数据的方法则采用了puts,另外就是客户端有个将服务器传送过来的16进制数据进行解析的函数,我这里客户端和服务器采用十六进制进行数据的传输,相比较而言,速度还是比较快 的。

    客户端的操作流程是这样的,开启客户端,在配置文件中输入将要连接的服务器端ip以及端口号,然后运行客户端就可以了,其中客户端涉及到两个类,一个就是更新文件的操作,另一个就是读写配置文件的操作,client类中,主要的函数就是更新文件操作,其中有一个就是逆解析16进制文件,将其转换成普通的可正常阅读的文件;INIReader类中是对ini文件进行解析的操作。主要就是解析ini文件中的节点。客户端写起来还是比较顺利的,在ini文件解析的地方一开始浪费了不少脑细胞。

    另外:在做的过程中如果你不想关闭客户端,只要将断开连接的操作改掉就可以了。

    如果使用ocra进行到包操作,在打包成exe的过程中,如果有死循环可以在循环处增加一个

    unless defined? Ocra

             while 1

                       my_method

             end

    end

    即可。

    下面是源码:

    require 'socket'
    require 'fileutils'
    class Client
    def initialize(host,port)

    $client=TCPSocket.open(host,port)

    end
    def update_file
    p 'start to update'
    #record the server send info
    line=$client.gets.chop
    filename_arr=[]
    p line
    #get the file name
    if line=='1'
    directory_name=$client.gets.chop
    p directory_name
    filename=$client.gets.chop
    filename.split('|').each {|x| filename_arr<<x}
    p filename_arr

    if not File.exists?(directory_name)
    Dir.mkdir(directory_name)
    else
    for i in 0..filename_arr.size-1
    p filename_arr[i]
    f=File.new(directory_name+filename_arr[i],'w')
    end
    end
    line=$client.gets.chop
    end
    #get the filebody
    if line=='2'
    p line

    for i in 0..filename_arr.size-1
    filebodyArr=[]
    filebody=$client.gets.chop
    p filebody
    chars=filebody.scan(/../).to_a
    chars.each{|ch|filebodyArr<<ch if not (ch=="["" or ch==""]") }
    fout=File.open(directory_name+filename_arr[i],'wb')
    filebodyArr.each{|f| fout.write f.to_i(16).chr}
    fout.close
    end
    line=$client.gets.chop
    end
    if line=='Over'
    p "it's over"
    end
    if line=='There is no update'
    p 'There is no update'
    end
     
    end
    end
    #host='localhost'
    #port=3000


    #parser the inifile
    class INIReader
    def initialize(fileName)
    @sections = {}
    current_section, kv_hash = nil
    File.open(fileName).each_line do |line|
    line = line.strip
    if line != ''
    if line[0].chr == '[' and line[-1].chr == ']'
    current_section = line[1, line.length - 2]
    kv_hash = {}
    else
    kv = line.split("=")
    kv_hash[kv[0]] = kv[1]
    @sections[current_section] = kv_hash
    end
    end
    end
    end

    def get_sections
    @sections.keys
    end

    def get_keys(sectionName)
    (@sections[sectionName]).keys
    end

    def get_value(sectionName, keyName)
    (@sections[sectionName])[keyName]
    end
    end

    reader = INIReader.new('clientconfig.ini')
    host=reader.get_value('config', 'host')
    port=reader.get_value('config','port')
    cl=Client.new(host,port)
    unless defined? Ocra
    while 1
    cl.update_file
    end
    end

  • 相关阅读:
    Codeforces Round #276 (Div. 1) D. Kindergarten dp
    Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序
    hihoCoder Challenge 27 #1469 : 福字 dp
    Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 括号序列 dp+栈
    Python id() 函数
    Python divmod() 函数
    Python any() 函数
    Python next() 函数
    Python slice() 函数
    Python3 hex() 函数
  • 原文地址:https://www.cnblogs.com/zthua/p/3158200.html
Copyright © 2011-2022 走看看