Skip to content

Commit 611c2ae

Browse files
committed
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.
1 parent a57889a commit 611c2ae

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/libstd/io/net/tcp.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ impl TcpStream {
6363
/// `host` can be a hostname or IP address string. If no error is
6464
/// encountered, then `Ok(stream)` is returned.
6565
pub fn connect(host: &str, port: u16) -> IoResult<TcpStream> {
66-
let addresses = try!(get_host_addresses(host));
66+
let addresses = match FromStr::from_str(host) {
67+
Some(addr) => vec!(addr),
68+
None => try!(get_host_addresses(host))
69+
};
6770
let mut err = IoError{
6871
kind: ConnectionFailed,
6972
desc: "no addresses found for hostname",

src/test/run-pass/tcp-stress.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ fn main() {
3737
unsafe { libc::exit(1) }
3838
});
3939

40-
let host = "127.0.0.1";
41-
let port = 0;
4240
let (tx, rx) = channel();
4341
spawn(proc() {
44-
let mut listener = TcpListener::bind(host, port).unwrap();
42+
let mut listener = TcpListener::bind("127.0.0.1", 0).unwrap();
4543
tx.send(listener.socket_name().unwrap());
4644
let mut acceptor = listener.listen();
4745
loop {
@@ -57,15 +55,15 @@ fn main() {
5755
}
5856
});
5957
let addr = rx.recv();
60-
let host = addr.ip.to_str();
61-
let port = addr.port;
6258

6359
let (tx, rx) = channel();
6460
for _ in range(0, 1000) {
6561
let tx = tx.clone();
6662
let mut builder = TaskBuilder::new();
6763
builder.opts.stack_size = Some(32 * 1024);
6864
builder.spawn(proc() {
65+
let host = addr.ip.to_str();
66+
let port = addr.port;
6967
match TcpStream::connect(host, port) {
7068
Ok(stream) => {
7169
let mut stream = stream;

0 commit comments

Comments
 (0)