java网络编程

1. ip、域名、端口

  • InetAddress 封装计算机的ip和域名

    • 获得对象

      ​ - InetAddress.getLocalHost();

      ​ - InetAddress.getByName(String host); //参数host可以是 ip地址/域名

      ​ - InetAddress.getByAddress([String host,]byte[] addr)

    • getHostName() //获取主机名

    • getHostAddress() //获取ip

    • getAddress() //获取byte[] 类型的ip

    //获取本机的InetAdress实例
    InetAddress address=InetAddress.getLocalHost();
    System.out.println(address);
    System.out.println("计算机名:"+address.getHostName());
    System.out.println("IP地址:"+address.getHostAddress());
    byte[] bytes=address.getAddress();//字节数组形式的ip
    System.out.println("字节数组形式的ip:"+Arrays.toString(bytes));
    
  • InetSocketAddress 在InetSocketAddress的基础上封装了端口

    • 创建对象

      InetSocketAddress address=new InetSocketAddress(InetAddress addr,int port)

      InetSocketAddress address=new InetSocketAddress(String host,int port)

    • 获取主机名、ip、端口

    InetAddress iAddress=InetAddress.getLocalHost();
    InetSocketAddress iSAddress=new InetSocketAddress(iAddress,5418);
    System.out.println(iSAddress);
    	//iSAddress.getAddress();同上
    System.out.println("主机名:"+iSAddress.getHostName());
    	//isAddress.getAddress().getHostName(); 同上
    System.out.println("ip:"+iSAddress.getAddress().getHostAddress());
    System.out.println("端口:"+iSAddress.getPort());
    

    2.URL

    			URL url=new URL("http://news.sohu.com/index.html#a?name=ndy");
    			System.out.println("协议:"+url.getProtocol());
    			System.out.println("域名:"+url.getHost());
    			System.out.println("端口:"+url.getPort());
    			System.out.println("默认端口:"+url.getDefaultPort());
    			
    			System.out.println("资源:"+url.getFile());
    			System.out.println("相对路径资源:"+url.getPath());
    			System.out.println("锚点:"+url.getRef());
    			System.out.println("参数:"+url.getQuery());//返回参数,若有锚点返回null
    			
    

    3.UDP: DatagramSocket、 DatagramPacket

    • DatagramPacket常用方法
      • 客户端 DatagramPacket(byte buf[], int length, SocketAddress address)
      • 服务端 DatagramPacket(byte buf[], int length)
      • ​ (get|set) (Address|Data|Length|Offset|Port|SocketAddress)
    //客户端	
    		//1.创建客户端
    		DatagramSocket socket=new DatagramSocket(1452);
    		//2.准备byte[]数据
    		String msg="那好";
    		byte[] data=msg.getBytes();
    		//3.封装数据
    		DatagramPacket packet =new DatagramPacket(data, data.length,new 						InetSocketAddress("localhost",5418));
    		//4.发送数据
    		socket.send(packet);
    		//5.关闭资源
    		socket.close();
    //服务端
    		//1.创建服务端
    		DatagramSocket socket=new DatagramSocket(5418);
    		//2.准备接受数据的容器
    		byte[] container=new byte[1024];
    		//3.封装成包
    		DatagramPacket packet=new DatagramPacket(container, container.length);
    		//4.接收
    		socket.receive(packet);
    		//5.分析-解包
    		byte[] data=packet.getData();
    		int length=packet.getLength();
    		System.out.println(new String(data,0,length));
    		//6.关闭资源
    		socket.close();
    

    4.TCP: ServerSocket 、Socket

//服务端
		//1.创建ServerSocket
		ServerSocket server=new ServerSocket(8888); 
		//2.接收客户端连接	server.accept()
		Socket socket=server.accept();
		//System.out.println("一个客户端建立连接");
		//3发送数据| 接收数据 socket.getOutputStream()|socket.getInputStream
		String msg="青山隐隐水迢迢";
		BufferedWriter bw=new BufferedWriter(
				new OutputStreamWriter(
					socket.getOutputStream()
				)
			);
		bw.write(msg);
		bw.newLine();
		bw.flush();
//客户端
		//1、创建客户端,指定服务器和端口
		Socket client=new Socket("localhost",8888);
		//Socket client=new Socket();client.connent(SocketAddress);同上
		//2、接收数据
		BufferedReader br=new BufferedReader(
				new InputStreamReader(client.getInputStream())
				);
		String echo=br.readLine();
		System.out.println(echo);
		br.close();
	
posted @ 2019-12-16 22:23  赵钱富贵  阅读(123)  评论(0编辑  收藏  举报