Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thomaslee committed May 13, 2014
1 parent a57889a commit 611c2ae
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/libstd/io/net/tcp.rs
Expand Up @@ -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",
Expand Down
8 changes: 3 additions & 5 deletions src/test/run-pass/tcp-stress.rs
Expand Up @@ -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 {
Expand All @@ -57,15 +55,15 @@ 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) {
let tx = tx.clone();
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;
Expand Down

0 comments on commit 611c2ae

Please sign in to comment.