我经历了很多谷歌搜索,我发现很多例子都不适用于我.这是一个简单的问题,我觉得有一个简单的答案,没有定义新的类\ modules等...
我的代码是这样的:
Console.WriteLine ("Please enter an IP address or hostname");
string host = Console.ReadLine ();
***IP = resolved "host"***
Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
s.Connect (IP, 80);
s.close();
Run Code Online (Sandbox Code Playgroud)
我如何实际解析IP变量?
这是我的代码:
if ARGV[0] == false
puts "Usage: ./script.rb argument"
exit
end
print "Yey we got an argument: " ARGV[0]
Run Code Online (Sandbox Code Playgroud)
但我不能让代码检查是否给出了ARGV [0],我应该怎么做?
我知道您可以使用HTTP协议标准telnet并与HTTP服务器协商 - 例如:
telnet google.com 80
Trying 173.194.70.139...
Connected to google.com.
Escape character is '^]'.
GET / HTTP.1.1
HOST: my.com
Run Code Online (Sandbox Code Playgroud)
我得到回应:
HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Content-Length: 925
Date: Wed, 18 Jul 2012 19:17:26 GMT
Server: GFE/2.0
Run Code Online (Sandbox Code Playgroud)
但我的问题是我可以用SSH协议做同样的事情吗?
我很难学习Fibers\coroutines背后的想法和Crystal中的实现.
我希望这是一个正确的问题,我完全接受"不在这里"的答案:)
这是我在Ruby中处理多线程的常用方法:
threads = []
max_threads = 10
loop do
begin
threads << Thread.new do
helper_method(1,2,3,4)
end
rescue Exception => e
puts "Error Starting thread"
end
begin
threads = threads.select { |t| t.alive? ? true : (t.join; false) }
while threads.size >= max_threads
puts 'Got Maximum threads'
sleep 1
threads = threads.select { |t| t.alive? ? true : (t.join; false) }
end
rescue Exception => e
puts e
end
end
Run Code Online (Sandbox Code Playgroud)
这样我打开一个新的线程,通常是传入的连接或其他东西,将线程添加到线程数组,然后检查我没有更多的线程,然后我想要的.
使用spawn\channels\fiber等在Crystal中实现类似功能的好方法是什么?
这是我的代码:
use std::net;
use std::thread;
extern crate argparse;
use argparse::{ArgumentParser, StoreTrue, Store};
fn scan_port(host: &str, port: u16) -> bool {
let host = host.to_string();
let port = port;
let t = thread::spawn(move || net::TcpStream::connect((host.as_str(), port)).is_ok());
t.join().unwrap()
}
Run Code Online (Sandbox Code Playgroud)
如果连接未在N秒内完成,如何创建线程将被终止或终止的情况?
所有这一切的原因是Rust无法设置套接字连接超时,因此我无法确保程序不会卡住.
我需要在bash中只获得前两个"uname -r"命令
常规输出的例子:
uname -r
3.5.0-18-generic
Run Code Online (Sandbox Code Playgroud)
我期望使用魔法bash选项:
3.5
Run Code Online (Sandbox Code Playgroud) 这是我不工作的脚本(只是挂起......)
require 'socket'
server = TCPServer.new 2000
loop do
Thread.start(server.accept) do |client|
sock_domain, remote_port, remote_hostname, remote_ip = server.peeraddr
client.puts "Hello !"
client.puts "Time is #{Time.now}"
puts "connection coming from #{remote_ip} and port #{remote_port}"
client.close
end
end
Run Code Online (Sandbox Code Playgroud)
我希望有一个连接客户端IP的输出,但连接已建立,没有任何反应.
我有这个代码:
a = File.open("/dev/urandom")
b = a.read(1024)
a.close
puts b
Run Code Online (Sandbox Code Playgroud)
我希望从/ dev/urandom device\file获取前1024个字节,而不是我得到一个错误,表示read只接受slice而不是Integer.
所以我试着这样做:
b = a.read(("a" * 1000).to_slice)
但后来我在输出中找回了"1000".
从Crystal中的文件读取x字节的正确方法是什么?
使用这个Hash对象
{"foo" => {"bar" => 1, "baz" => 2}, "bla" => [1,2,3]}
Run Code Online (Sandbox Code Playgroud)
我想生成这个Hash对象数组
[
{"foo" => "*", "bla" => [1,2,3]},
{"foo" => {"bar" => "*", "baz" => 2}, "bla" => [1,2,3]},
{"foo" => {"bar" => "1", "baz" => "*"}, "bla" => [1,2,3]},
{"foo" => {"bar" => "*", "baz" => 2}, "bla" => "*"},
]
Run Code Online (Sandbox Code Playgroud)
我基本上遍历了每个键并将其值更改为"*",同时保留了散列的整体结构并保存了在某个数组中生成的新散列.
我已经尝试了很多想法,但大多数都不会工作,因为我之前可以猜到Array类型,我只知道这个哈希是由JSON.parse生成的,然后变成Hash(String,JSON :: Any)
我目前正在尝试它
hash = {"bar" => {"and" => "2", "br" => "1"}}
arr = [hash, {"bar" => "1"}]
arr.delete(arr.last)
arr.delete(hash)
def changer(hash, arr, …
Run Code Online (Sandbox Code Playgroud)