Java|Java 网络编程 - 总结概述

IP地址 【Java|Java 网络编程 - 总结概述】IP地址IntAddress

  • 唯一定位一台网络上的计算机
  • 127.0.0.1:本地localhost
  • IP地址的分类
    • ipV4/ipV6
      • ipV4:127.0.0.1,4个字节组成;0~255,42亿~;30亿都在北美,亚洲4亿;2011年就用完了
      • ipV6:128位。8个无符号整数
    • 公网(互联网)-私网(局域网)
      • ABCD类地址
      • 192.168 .xx.xx,专门给组织内部使用的
  • 域名:方面记忆,免去了记录IP的问题
1 //测试IP 2 public class TestInetAddress { 3public static void main(String[] args) { 4try { 5//查询本机地址 6InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); 7System.out.println(inetAddress); 8InetAddress localhost = InetAddress.getByName("localhost"); 9System.out.println(localhost); 10InetAddress localHost = InetAddress.getLocalHost(); 11System.out.println(localHost); 12 13//查询网站ip地址 14InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com"); 15System.out.println(inetAddress1); 16 17//常用方法 18System.out.println(inetAddress1.getHostAddress()); //ip 19System.out.println(inetAddress1.getHostName()); //域名,或者自己的名字 20} catch (UnknownHostException e) { 21e.printStackTrace(); 22} 23} 24 }

端口 ip相当于省/市/区/街/楼,端口就是门牌号;端口表示计算机上的一个程序的进程
  • 不同的进程有不同的端口号!用来区分软件!
  • 被规定0~65535
  • TCP,UDP:65535*2;tcp:80;udp:80
  • 端口分类
    • 公有端口0~1023
      • HTTP:80
      • HTTPS:443
      • FTP:21
      • Telent:23
    • 程序注册端口:1024~49151,分配用户或者程序
      • Tomcat:8080
      • MySQL:3306
      • Orcal:1521
    • 动态、私有:49152~65535
//CMD
netstat -ano #查看所有的端口
netstat -ano|findstr "5900" #查看指定的端口
tasklist|findstr "8696" #查看指定端口的进程

1 //端口 2 public class TestInetSocketAddress { 3public static void main(String[] args) { 4InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080); 5System.out.println(socketAddress); 6 7System.out.println(socketAddress.getAddress()); 8System.out.println(socketAddress.getHostName()); //地址 9System.out.println(socketAddress.getPort()); //端口 10} 11 }

通信协议 Java|Java 网络编程 - 总结概述
文章图片

协议:约定,共同遵守,都能理解
网络通信协议:速率,传输码率,代码结构,传输控制....
TCP/IP协议簇:实际上是一组协议
重要
  • TCP:用户传输协议
  • UDP:用户数据报协议
TCP UDP对比
TCP:打电话
  • 连接,稳定
  • 三次握手,四次挥手
  • 客户端、服务端
  • 传输完成,释放连接,效率低
UDP:发短信
  • 不连接,不稳定
  • 客户端、服务端:没有明确的界限
  • 不管有没有准备好,都可以发给你
TCP实现聊天
1 //服务端 2 public class TcpServerDemo01 { 3public static void main(String[] args) { 4ServerSocket serverSocket = null; 5Socket accept=null; 6InputStream is=null; 7ByteArrayOutputStream baos=null; 8try { 9//1.得有一个地址 10serverSocket = new ServerSocket(9999); 11 12while (true){ 13//2.等待客户端连接过来 14accept = serverSocket.accept(); 15//3.读取客户端得消息 16is = accept.getInputStream(); 17 18//管道流 19baos = new ByteArrayOutputStream(); 20byte[] bytes = new byte[1024]; 21int len; 22while ((len=is.read(bytes))!=-1){ 23baos.write(bytes,0,len); 24} 25System.out.println(baos.toString()); 26} 27 28} catch (IOException e) { 29e.printStackTrace(); 30}finally { 31//关闭流 32try { 33baos.close(); 34} catch (IOException e) { 35e.printStackTrace(); 36} 37try { 38is.close(); 39} catch (IOException e) { 40e.printStackTrace(); 41} 42try { 43accept.close(); 44} catch (IOException e) { 45e.printStackTrace(); 46} 47try { 48serverSocket.close(); 49} catch (IOException e) { 50e.printStackTrace(); 51} 52 53} 54} 55 }

1 //客户端 2 public class TcpClientDemo01 { 3public static void main(String[] args) { 4Socket socket=null; 5OutputStream os=null; 6 7try { 8//1.要直到服务器得地址 9InetAddress serverIP= InetAddress.getByName("127.0.0.1"); 10int port=9999; 11//2.创建一个socker连接 12try { 13socket = new Socket(serverIP,port); 14//3.发送消息 IO流 15os = socket.getOutputStream(); 16os.write("Hello".getBytes()); 17} catch (IOException e) { 18e.printStackTrace(); 19} 20 21 22} catch (UnknownHostException e) { 23e.printStackTrace(); 24}finally { 25try { 26os.close(); 27} catch (IOException e) { 28e.printStackTrace(); 29} 30try { 31socket.close(); 32} catch (IOException e) { 33e.printStackTrace(); 34} 35} 36} 37 }

TCP文件上传
1 //服务端 2 public class TcpServerDemo02 { 3public static void main(String[] args) throws Exception{ 4//1.创建服务 5ServerSocket serverSocket = new ServerSocket(9000); 6//2.监听客户端得连接 7Socket accept = serverSocket.accept(); //阻塞式监听,会一直等待客户端得连接 8//3.获取输入流 9InputStream is = accept.getInputStream(); 10 11//4.文件输出 12FileOutputStream fos = new FileOutputStream("receive.jpg"); 13byte[] by = new byte[1024]; 14int len; 15while ((len=is.read(by))!=-1){ 16fos.write(by,0,len); 17} 18 19//通知客户端我接收完毕了 20OutputStream os = accept.getOutputStream(); 21os.write("接收完毕".getBytes()); 22 23os.close(); 24fos.close(); 25is.close(); 26accept.close(); 27serverSocket.close(); 28} 29 }

1 //客户端 2 public class TcpClientDemo02 { 3public static void main(String[] args) throws Exception{ 4//1.创建一个socket连接 5Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000); 6//2.创建一个输出流 7OutputStream os = socket.getOutputStream(); 8 9//3.读取文件 10FileInputStream fis = new FileInputStream("D:\\WorkSpace\\JavaSE\\基础语法\\111.png"); 11//4.写出文件 12byte[] by = new byte[1024]; 13int len; 14while ((len=fis.read(by))!=-1){ 15os.write(by,0,len); 16} 17 18//通知服务器,我已经传输结束了 19socket.shutdownOutput(); 20 21//确认服务器接收完毕,才能断开连接 22InputStream is = socket.getInputStream(); 23ByteArrayOutputStream baos = new ByteArrayOutputStream(); 24 25byte[] bytes = new byte[1024]; 26int leng; 27while ((leng=is.read(bytes))!=-1){ 28baos.write(bytes,0,leng); 29} 30System.out.println(baos.toString()); 31 32baos.close(); 33is.close(); 34os.close(); 35fis.close(); 36socket.close(); 37} 38 }

UDP消息发送
1 //发送方 2 public class UdpClientDemo01 { 3public static void main(String[] args) throws Exception{ 4//1.建立一个Socket 5DatagramSocket datagramSocket = new DatagramSocket(); 6 7//2.建个包 8String msg="你好啊,服务器!"; 9InetAddress localhost = InetAddress.getByName("localhost"); 10int port = 9090; 11 12//数据、数据的长度起始、要发给谁 13DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port); 14 15//发送包 16datagramSocket.send(datagramPacket); 17 18//4.关闭流 19datagramSocket.close(); 20} 21 }

1 //接收方 2 public class UdpServerDemo01 { 3public static void main(String[] args) throws Exception{ 4//开放端口 5DatagramSocket datagramSocket = new DatagramSocket(9090); 6//接收数据 7byte[] bytes = new byte[1024]; 8DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length); 9 10datagramSocket.receive(datagramPacket); //阻塞接收 11 12System.out.println(datagramPacket.getAddress()); 13System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength())); 14} 15 }

UDP聊天实现
1 //发送方 2 public class UdpSenderDemo01 { 3public static void main(String[] args) throws Exception{ 4 5DatagramSocket datagramSocket = new DatagramSocket(8888); 6 7//准备数据:控制台读取System.in 8BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 9 10while (true){ 11String data=https://www.it610.com/article/reader.readLine(); 12byte[] bytes = data.getBytes(); 13DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress("localhost",6666)); 14datagramSocket.send(datagramPacket); 15if(bytes.equals("byebye")){ 16break; 17} 18} 19datagramSocket.close(); 20} 21 }

1 //接收方 2 public class UdpReceiveDemo01 { 3public static void main(String[] args) throws Exception{ 4DatagramSocket datagramSocket = new DatagramSocket(6666); 5 6while (true){ 7//准备接收包裹 8byte[] bytes = new byte[1024]; 9DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length); 10 11//断开连接 byebye 12byte[] data = https://www.it610.com/article/datagramPacket.getData(); 13String string = new String(data, 0, data.length); 14System.out.println(string); 15if(string.equals("byebye")){ 16break; 17} 18} 19 20datagramSocket.close(); 21 22} 23 }


    推荐阅读