pub struct Connection {
pub stream: TcpStream,
pub addr: (String, u16),
}Expand description
Represents a TCP connection to a Minecraft server.
§Examples
use std::time::Duration;
use mc_ping::connection::Connection;
let addr = ("play.example.com".to_string(), 25565);
let mut conn = Connection::connect(addr).await?;
let status = conn.ping().await?;
println!("Server status: {:?}", status);Fields§
§stream: TcpStreamUnderlying TCP stream.
addr: (String, u16)Server address as (IP/domain, port).
Implementations§
Source§impl Connection
impl Connection
Sourcepub async fn connect(addr: (String, u16)) -> Result<Self>
pub async fn connect(addr: (String, u16)) -> Result<Self>
Connects to a Minecraft server at the specified address.
If the resolve feature is enabled, attempts to resolve domain names to IP addresses before connecting.
§Errors
Returns an error if the connection or DNS resolution fails.
§Examples
use mc_ping::connection::Connection;
let addr = ("localhost".to_string(), 25565);
let conn = Connection::connect(addr).await?;Sourcepub async fn connect_timeout(
addr: (String, u16),
_timeout: Duration,
) -> Result<Self>
pub async fn connect_timeout( addr: (String, u16), _timeout: Duration, ) -> Result<Self>
Connects to a Minecraft server with a timeout.
Attempts to connect and returns an error if the timeout elapses.
§Errors
Returns an error if the connection fails or times out.
§Examples
use mc_ping::connection::Connection;
let addr = ("example.com".to_string(), 25565);
match Connection::connect_timeout(addr, Duration::from_secs(5)).await {
Ok(conn) => println!("Connected!"),
Err(e) => println!("Failed to connect: {}", e),
}Sourcepub async fn send_handshake(&mut self) -> Result<()>
pub async fn send_handshake(&mut self) -> Result<()>
Sends the Minecraft handshake packet.
This prepares the connection for further communication such as status query or login.
§Errors
Returns an error if writing to the TCP stream fails.
§Examples
use mc_ping::connection::Connection;
let addr = ("127.0.0.1".to_string(), 25565);
let mut conn = Connection::connect(addr).await?;
conn.send_handshake().await?;Sourcepub async fn get_status(&mut self) -> Result<ServerStatus>
pub async fn get_status(&mut self) -> Result<ServerStatus>
Queries the server status.
Sends a status query and reads the response, returning a parsed ServerStatus. Assumes handshake has been sent beforehand.
§Errors
Returns an error if sending or receiving fails, or parsing fails.
§Examples
use mc_ping::connection::Connection;
let addr = ("localhost".to_string(), 25565);
let mut conn = Connection::connect(addr).await?;
conn.send_handshake().await?;
let status = conn.get_status().await?;
println!("Status: {:?}", status);Sourcepub async fn ping(&mut self) -> Result<ServerStatus>
pub async fn ping(&mut self) -> Result<ServerStatus>
Performs a full ping: sends handshake + status query and returns server status.
Convenient for a single-step status check.
§Errors
Returns an error if any network or parsing step fails.
§Examples
use mc_ping::connection::Connection;
let addr = ("play.example.com".to_string(), 25565);
let mut conn = Connection::connect(addr).await?;
let status = conn.ping().await?;
println!("Server status: {:?}", status);