Skip to content

Commit a57889a

Browse files
committed
Easier interface for TCP ::connect and ::bind.
Prior to this commit, TcpStream::connect and TcpListener::bind took a single SocketAddr argument. This worked well enough, but the API felt a little too "low level" for most simple use cases. A great example is connecting to rust-lang.org on port 80. Rust users would need to: 1. resolve the IP address of rust-lang.org using io::net::addrinfo::get_host_addresses. 2. check for errors 3. if all went well, use the returned IP address and the port number to construct a SocketAddr 4. pass this SocketAddr to TcpStream::connect. I'm modifying the type signature of TcpStream::connect and TcpListener::bind so that the API is a little easier to use. TcpStream::connect now accepts two arguments: a string describing the host/IP of the host we wish to connect to, and a u16 representing the remote port number. Similarly, TcpListener::bind has been modified to take two arguments: a string describing the local interface address (e.g. "0.0.0.0" or "127.0.0.1") and a u16 port number. Here's how to port your Rust code to use the new TcpStream::connect API: // old ::connect API let addr = SocketAddr{ip: Ipv4Addr{127, 0, 0, 1}, port: 8080}; let stream = TcpStream::connect(addr).unwrap() // new ::connect API (minimal change) let addr = SocketAddr{ip: Ipv4Addr{127, 0, 0, 1}, port: 8080}; let stream = TcpStream::connect(addr.ip.to_str(), addr.port()).unwrap() // new ::connect API (more compact) let stream = TcpStream::connect("127.0.0.1", 8080).unwrap() // new ::connect API (hostname) let stream = TcpStream::connect("rust-lang.org", 80) Similarly, for TcpListener::bind: // old ::bind API let addr = SocketAddr{ip: Ipv4Addr{0, 0, 0, 0}, port: 8080}; let mut acceptor = TcpListener::bind(addr).listen(); // new ::bind API (minimal change) let addr = SocketAddr{ip: Ipv4Addr{0, 0, 0, 0}, port: 8080}; let mut acceptor = TcpListener::bind(addr.ip.to_str(), addr.port()).listen() // new ::bind API (more compact) let mut acceptor = TcpListener::bind("0.0.0.0", 8080).listen() [breaking-change]
1 parent 07d6322 commit a57889a

File tree

5 files changed

+271
-107
lines changed

5 files changed

+271
-107
lines changed

src/compiletest/runtest.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use util;
1919

2020
use std::io::File;
2121
use std::io::fs;
22-
use std::io::net::ip::{Ipv4Addr, SocketAddr};
2322
use std::io::net::tcp;
2423
use std::io::process::ProcessExit;
2524
use std::io::process;
@@ -316,10 +315,7 @@ fn run_debuginfo_gdb_test(config: &config, props: &TestProps, testfile: &Path) {
316315
//waiting 1 second for gdbserver start
317316
timer::sleep(1000);
318317
let result = task::try(proc() {
319-
tcp::TcpStream::connect(SocketAddr {
320-
ip: Ipv4Addr(127, 0, 0, 1),
321-
port: 5039,
322-
}).unwrap();
318+
tcp::TcpStream::connect("127.0.0.1", 5039).unwrap();
323319
});
324320
if result.is_err() {
325321
continue;

src/libstd/io/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ Some examples of obvious things you might want to do
8383
8484
```rust,should_fail
8585
# #![allow(unused_must_use)]
86-
use std::io::net::ip::SocketAddr;
8786
use std::io::net::tcp::TcpStream;
8887
89-
let addr = from_str::<SocketAddr>("127.0.0.1:8080").unwrap();
90-
let mut socket = TcpStream::connect(addr).unwrap();
88+
let mut socket = TcpStream::connect("127.0.0.1", 8080).unwrap();
9189
socket.write(bytes!("GET / HTTP/1.0\n\n"));
9290
let response = socket.read_to_end();
9391
```
@@ -99,11 +97,9 @@ Some examples of obvious things you might want to do
9997
# fn foo() {
10098
# #![allow(dead_code)]
10199
use std::io::{TcpListener, TcpStream};
102-
use std::io::net::ip::{Ipv4Addr, SocketAddr};
103100
use std::io::{Acceptor, Listener};
104101
105-
let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 80 };
106-
let listener = TcpListener::bind(addr);
102+
let listener = TcpListener::bind("127.0.0.1", 80);
107103
108104
// bind the listener to the specified address
109105
let mut acceptor = listener.listen();

0 commit comments

Comments
 (0)