Try to parse TcpStream::connect 'host' parameter as an IP.

Fall back to get_host_addresses to try a DNS lookup if we can't
parse it as an IP address.
This commit is contained in:
Tom Lee 2014-05-10 21:30:36 -07:00
parent a57889a580
commit 611c2ae4f1
2 changed files with 7 additions and 6 deletions

View File

@ -63,7 +63,10 @@ impl TcpStream {
/// `host` can be a hostname or IP address string. If no error is
/// encountered, then `Ok(stream)` is returned.
pub fn connect(host: &str, port: u16) -> IoResult<TcpStream> {
let addresses = try!(get_host_addresses(host));
let addresses = match FromStr::from_str(host) {
Some(addr) => vec!(addr),
None => try!(get_host_addresses(host))
};
let mut err = IoError{
kind: ConnectionFailed,
desc: "no addresses found for hostname",

View File

@ -37,11 +37,9 @@ fn main() {
unsafe { libc::exit(1) }
});
let host = "127.0.0.1";
let port = 0;
let (tx, rx) = channel();
spawn(proc() {
let mut listener = TcpListener::bind(host, port).unwrap();
let mut listener = TcpListener::bind("127.0.0.1", 0).unwrap();
tx.send(listener.socket_name().unwrap());
let mut acceptor = listener.listen();
loop {
@ -57,8 +55,6 @@ fn main() {
}
});
let addr = rx.recv();
let host = addr.ip.to_str();
let port = addr.port;
let (tx, rx) = channel();
for _ in range(0, 1000) {
@ -66,6 +62,8 @@ fn main() {
let mut builder = TaskBuilder::new();
builder.opts.stack_size = Some(32 * 1024);
builder.spawn(proc() {
let host = addr.ip.to_str();
let port = addr.port;
match TcpStream::connect(host, port) {
Ok(stream) => {
let mut stream = stream;