小白学Java21:网络编程

InetAddress

表示IP地址对象,封装了与该IP地址相关的所有信息

创建本机的IP地址对象

		InetAddress ia = InetAddress.getLocalHost();
		System.out.println("本机的IP地址是:" + ia.getHostAddress() + "主机名是:" + ia.getHostName());
		
		InetAddress ia2 = InetAddress.getByName("192.168.137.1");
		System.out.println("本机的IP地址是:" + ia2.getHostAddress() + "主机名是:" + ia2.getHostName());
		
		InetAddress ia3 = InetAddress.getByName("127.0.0.1");
		System.out.println("本机的IP地址是:" + ia3.getHostAddress() + "主机名是:" + ia3.getHostName());
		
		InetAddress ia4 = InetAddress.getByName("localhost");
		System.out.println("本机的IP地址是:" + ia4.getHostAddress() + "主机名是:" + ia4.getHostName());

创建局域网的IP地址对象

		InetAddress ia5 = InetAddress.getByName("192.168.0.1");
		System.out.println("IP地址是:" + ia5.getHostAddress() + "主机名是:" + ia5.getHostName());
		System.out.println("一秒钟内是否可达"+ia5.isReachable(1000));

创建外网的IP地址对象

		InetAddress ia6 = InetAddress.getByName("www.baidu.com");
		System.out.println("IP地址是:" + ia6.getHostAddress() + "主机名是:" + ia6.getHostName());

		InetAddress[] ia7 = InetAddress.getAllByName("www.baidu.com");
		for (InetAddress inetAddress : ia7) {
			System.out.println("IP地址为:" + inetAddress.getHostAddress() );
		}

基于TCP的网络编程

Socket编程

Socket(套接字)是网络中的一个通信节点
分为Socket与服务器ServerSocket
通信要求;IP地址+端口号

创建服务器


		// 1创建服务器套接字,并制定端口号
		ServerSocket ss = new ServerSocket(8899);
		// 调用accept()接收客户端请求,注:这是一个阻塞方法,如果没有客户端了解,将等待
		Socket socket = ss.accept();
		// 获取输入流,接收客户端发送的数据

		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		System.out.println("客户端:" + br.readLine());
		
		br.close();
		socket.close();
		ss.close();






	try (ServerSocket ss = new ServerSocket(8899);
				Socket socket = ss.accept();
				BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));) {
			System.out.println("客户端:" + br.readLine());
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

创建客户端

		//创建客户端套接字,并制定连接到的服务器的IP地址和端口号
		Socket socket = new Socket("localhost",8899);
		//获取输出流,向服务器发送数据
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
		bw.write("好久不见");
		
		bw.close();
		socket.close();



try (Socket socket = new Socket("localhost", 8899);
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));) {
			bw.write("好久不见");

		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

使用TCP实现多个客户端连接到一个服务器

服务器端:

public class TestServerSockets {
	// 设计一个可以接收多个客户端连接的服务器端
	public static void main(String[] args) throws IOException {
		ServerSocket s = new ServerSocket(10086);
		System.out.println("服务器已启动...........");
		while (true) {
			Socket socket = s.accept();
			System.out.println(socket.getInetAddress() + "进来了");
			new SocketThread(socket).start();

		}

	}
}

class SocketThread extends Thread {
	private Socket socket;

	public SocketThread(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		if (socket != null) {
			try (BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
				while (true) {
					String readLine = br.readLine();
					if (readLine == null) {
						break;
					}
					System.out.println(socket.getInetAddress() + ":" + readLine);
					if (readLine.equals("886") || readLine.equals("byebye")) {
						System.out.println(socket.getInetAddress() + "退出了");
						socket.close();
						break;
					}
				}
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

客户端

public class TestSockets {
	public static void main(String[] args) {
		try (Socket socket = new Socket("localhost", 10086);
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
				Scanner sc = new Scanner(System.in);) {
			while (true) {
				String nextLine = sc.nextLine();
				bw.write(nextLine);
				bw.newLine();
				bw.flush();
				if (nextLine.equals("886") || nextLine.equals("byebye")) {
					break;
				}
			}
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值