网络编程

网络编程

实现网络编程的三个要素:

使用IP地址(准确的定位网络上的一台或多台主机);

使用端口号(定位主机上的特定的应用);

使用网络通信协议(可靠,高效的传输数据);


IPv4地址:是一个32位的二进制数,4个字节,一般表示为十进制的数,例如192.168.12.12

在2011年基本用尽

 

 

IPv6地址:128位地址,16个字节,写成8个无符号整数,

例如CCCC:1555:1666:1444:1555:ABCD:CBDA:AC45


IP地址分两类:

公网地址(万维网 www.)和私有地址(局域网 192.168.开头)


实例化方式:

try {
   //获取指定ip
   InetAddress byName = InetAddress.getByName("192.168.23.165");
   System.out.println(byName);
   InetAddress inet2 = InetAddress.getByName("www.baidu.com");
   System.out.println(inet2);
   //获取本地ip
   InetAddress localHost = InetAddress.getLocalHost();
   System.out.println(localHost);
   
} catch (UnknownHostException e) {
   e.printStackTrace();
}

TCP协议:

  • 两个通信进程:客户端,服务端

  • 传输前,需要经过”三次握手“方式,点对点通信,是可靠的;

  • 使用重发机制,就是当一个通信实体发送给另一个实体时,需要告知另一个实体确认信息,如果没有收到,还会重新发送刚才得信息

  • 在连接中可以进行大数据量的传输

  • 传输完毕,需释放已建立的连接,效率低

  • 例如:打电话

 

 

 

UDP协议:

  • 两个通信进程:发送端,接收端

  • 将数据,源,目的封装成数据包(传输的基本单位),不需要建立连接

  • 不可靠的

  • 数据大小有限制 64k内

  • 发送数据结束时,无需释放资源,开销小,效率高

  • 使用场景:音频,视频和普通数据的传输,例如视频会议

  • 例如:发短信,发电报

socket

image-20230309220910484

tcp中客户端和服务端利用socket通信:

public class tcpClient {
   @Test
   public void client(){
       OutputStream outputStream = null;
       Socket socket = null;
       try {
           InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
           int port = 8888;
           socket = new Socket(inetAddress, port);
           outputStream = socket.getOutputStream();
           outputStream.write("我是小李".getBytes());
      } catch (IOException e) {
           e.printStackTrace();
      }finally {
           try {
               outputStream.close();
          } catch (IOException e) {
               e.printStackTrace();
          }
           try {
               socket.close();
          } catch (IOException e) {
               e.printStackTrace();
          }
      }
  }

   @Test
   public void server(){
       //这些都是要close的
       Socket accept = null;
       InputStream inputStream = null;
       ServerSocket serverSocket = null;
       //端口号
       int port = 8888;
       try {
           serverSocket = new ServerSocket(port);
           accept = serverSocket.accept();
           System.out.println("我已经收到!");
           inputStream = accept.getInputStream();
           //规范这样写
           byte[] bytes = new byte[1024];
           int len;
           while ((len = inputStream.read(bytes)) != -1){
               String str = new String(bytes, 0, len);
               System.out.println(str);
          }
      } catch (IOException e) {
           e.printStackTrace();
      }finally {
           try {
               inputStream.close();
          } catch (IOException e) {
               e.printStackTrace();
          }
           try {
               accept.close();
          } catch (IOException e) {
               e.printStackTrace();
          }
           try {
               serverSocket.close();
          } catch (IOException e) {
               e.printStackTrace();
          }
      }
  }
}
//传输文件,互发信息,csdn上很多

udp是通过数据包(DatagramSocket)传输数据的

问题:

 
posted @ 2023-03-10 14:12  jessi呀  阅读(64)  评论(0编辑  收藏  举报