Java 网络编程
Socket 编程模型、ServerSocket 与 Socket、InetAddress 的用法。
Socket 编程
The TCP model: the server binds a ServerSocket and blocks in accept(); the client connects with a Socket, then both sides exchange data through InputStream / OutputStream.
// 服务端骨架
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
try (ServerSocket server = new ServerSocket(9000)) {
System.out.println("listening on 9000");
try (Socket s = server.accept();
PrintWriter out = new PrintWriter(s.getOutputStream(), true)) {
out.println("hello client");
}
}
}
}listening on 9000Socket 客户端
The client calls new Socket(host, port); streams then behave like file streams. A failed connect throws ConnectException, and a closed peer yields -1 or an exception.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
try (Socket s = new Socket("127.0.0.1", 9000);
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()))) {
System.out.println(in.readLine());
}
}
}hello clientConnectException: Connection refused — expected, since no connection exists.InetAddress 类
InetAddress represents an IP address: getByName resolves a host, getLocalHost returns this machine, and getHostName / getHostAddress give the name and IP text.
import java.net.InetAddress;
public class AddressDemo {
public static void main(String[] args) throws Exception {
InetAddress local = InetAddress.getLocalHost();
System.out.println(local.isLoopbackAddress() || !local.isLoopbackAddress());
System.out.println(local.getHostAddress().length() > 0);
InetAddress loop = InetAddress.getByName("127.0.0.1");
System.out.println(loop.isLoopbackAddress());
System.out.println(loop.getHostAddress());
}
}true
true
true
127.0.0.1Socket 编程
TCP 通信的基本模型:服务端 ServerSocket 绑定端口并 accept() 等待;客户端 Socket 连接后,双方各自拿到 InputStream / OutputStream 收发数据。
// 服务端骨架
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
try (ServerSocket server = new ServerSocket(9000)) {
System.out.println("listening on 9000");
try (Socket s = server.accept();
PrintWriter out = new PrintWriter(s.getOutputStream(), true)) {
out.println("hello client");
}
}
}
}listening on 9000Socket 客户端
客户端用 new Socket(host, port) 建立连接,之后读写方式和文件流一样。连接失败会抛 ConnectException,读数据时对端关闭会返回 -1 或抛异常。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
try (Socket s = new Socket("127.0.0.1", 9000);
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()))) {
System.out.println(in.readLine());
}
}
}hello clientConnectException: Connection refused —— 这是正常的,说明连接没建立。InetAddress 类
InetAddress 表示 IP 地址,getByName 做域名解析,getLocalHost 取本机地址,getHostName / getHostAddress 分别取主机名和 IP 字符串。
import java.net.InetAddress;
public class AddressDemo {
public static void main(String[] args) throws Exception {
InetAddress local = InetAddress.getLocalHost();
System.out.println(local.isLoopbackAddress() || !local.isLoopbackAddress());
System.out.println(local.getHostAddress().length() > 0);
InetAddress loop = InetAddress.getByName("127.0.0.1");
System.out.println(loop.isLoopbackAddress());
System.out.println(loop.getHostAddress());
}
}true
true
true
127.0.0.1