
















import java.net.InetAddress;
import java.net.UnknownHostException;/*** 演示InetAddress 类的使用*/
public class API_ {public static void main(String[] args) throws UnknownHostException {//1. 获取本机的InetAddress 对象InetAddress localHost = InetAddress.getLocalHost();System.out.println(localHost);//DESKTOP-S4MP84S/192.168.12.1//2. 根据指定主机名 获取 InetAddress对象InetAddress host1 = InetAddress.getByName("DESKTOP-S4MP84S");System.out.println("host1=" + host1);//DESKTOP-S4MP84S/192.168.12.1//3. 根据域名返回 InetAddress对象, 比如 www.baidu.com 对应InetAddress host2 = InetAddress.getByName("www.baidu.com");System.out.println("host2=" + host2);//www.baidu.com / 110.242.68.4//4. 通过 InetAddress 对象,获取对应的地址String hostAddress = host2.getHostAddress();//IP 110.242.68.4System.out.println("host2 对应的ip = " + hostAddress);//110.242.68.4//5. 通过 InetAddress 对象,获取对应的主机名/或者的域名String hostName = host2.getHostName();System.out.println("host2对应的主机名/域名=" + hostName); // www.baidu.com}
}
主要用于获取本机的一些配置信息,以便将来实现动态连接应用,例如将来我和别人进行连接时,不需要每次都去手动查询自己的ip地址,只需要书写相关类获取地址即可!




服务器端
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;/*** 服务端*/
public class SocketTCP01Server {public static void main(String[] args) throws IOException {//思路//1. 在本机 的9999端口监听, 等待连接// 细节: 要求在本机没有其它服务在监听9999// 细节:这个 ServerSocket 可以通过 accept() 返回多个Socket[多个客户端连接服务器的并发]ServerSocket serverSocket = new ServerSocket(9999);System.out.println("服务端,在9999端口监听,等待连接..");//2. 当没有客户端连接9999端口时,程序会 阻塞, 等待连接// 如果有客户端连接,则会返回Socket对象,程序继续Socket socket = serverSocket.accept();System.out.println("服务端 socket =" + socket.getClass());////3. 通过socket.getInputStream() 读取客户端写入到数据通道的数据, 显示InputStream inputStream = socket.getInputStream();//4. IO读取byte[] buf = new byte[1024];int readLen = 0;while ((readLen = inputStream.read(buf)) != -1) {System.out.println(new String(buf, 0, readLen));//根据读取到的实际长度,显示内容.}//5.关闭流和socketinputStream.close();socket.close();serverSocket.close();//关闭}
}
客户端
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;/*** 客户端,发送 "hello, server" 给服务端*/
public class SocketTCP01Client {public static void main(String[] args) throws IOException {//思路//1. 连接服务端 (ip , 端口)//解读: 连接本机的 9999端口, 如果连接成功,返回Socket对象Socket socket = new Socket(InetAddress.getLocalHost(), 9999);System.out.println("客户端 socket返回=" + socket.getClass());//2. 连接上后,生成Socket, 通过socket.getOutputStream()// 得到 和 socket对象关联的输出流对象OutputStream outputStream = socket.getOutputStream();//3. 通过输出流,写入数据到 数据通道outputStream.write("hello, server".getBytes());//4. 关闭流对象和socket, 必须关闭outputStream.close();socket.close();System.out.println("客户端退出.....");}
}
拓展

此问题是由于没有释放写出操作资源时导致的,注意关流


服务器端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;/*** 服务端*/
@SuppressWarnings({"all"})
public class SocketTCP02Server {public static void main(String[] args) throws IOException {//思路//1. 在本机 的9999端口监听, 等待连接// 细节: 要求在本机没有其它服务在监听9999// 细节:这个 ServerSocket 可以通过 accept() 返回多个Socket[多个客户端连接服务器的并发]ServerSocket serverSocket = new ServerSocket(9999);System.out.println("服务端,在9999端口监听,等待连接..");//2. 当没有客户端连接9999端口时,程序会 阻塞, 等待连接// 如果有客户端连接,则会返回Socket对象,程序继续Socket socket = serverSocket.accept();System.out.println("服务端 socket =" + socket.getClass());////3. 通过socket.getInputStream() 读取客户端写入到数据通道的数据, 显示InputStream inputStream = socket.getInputStream();//4. IO读取byte[] buf = new byte[1024];int readLen = 0;while ((readLen = inputStream.read(buf)) != -1) {System.out.println(new String(buf, 0, readLen));//根据读取到的实际长度,显示内容.}//5. 获取socket相关联的输出流OutputStream outputStream = socket.getOutputStream();outputStream.write("hello, client".getBytes());// 设置结束标记socket.shutdownOutput();//6.关闭流和socketoutputStream.close();inputStream.close();socket.close();serverSocket.close();//关闭}
}
客户端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;/*** 客户端,发送 "hello, server" 给服务端*/
@SuppressWarnings({"all"})
public class SocketTCP02Client {public static void main(String[] args) throws IOException {//思路//1. 连接服务端 (ip , 端口)//解读: 连接本机的 9999端口, 如果连接成功,返回Socket对象Socket socket = new Socket(InetAddress.getLocalHost(), 9999);System.out.println("客户端 socket返回=" + socket.getClass());//2. 连接上后,生成Socket, 通过socket.getOutputStream()// 得到 和 socket对象关联的输出流对象OutputStream outputStream = socket.getOutputStream();//3. 通过输出流,写入数据到 数据通道outputStream.write("hello, server".getBytes());// 设置结束标记socket.shutdownOutput();//4. 获取和socket关联的输入流. 读取数据(字节),并显示InputStream inputStream = socket.getInputStream();byte[] buf = new byte[1024];int readLen = 0;while ((readLen = inputStream.read(buf)) != -1) {System.out.println(new String(buf, 0, readLen));}//5. 关闭流对象和socket, 必须关闭inputStream.close();outputStream.close();socket.close();System.out.println("客户端退出.....");}
}
如果两个用户之间出现双向读写,会出现阻塞现象,需要手动调用shutDownOutput方法,只是写操作的时候需要标记个结束