java-网络编程

一、概述

1、两个主要问题

(1)如何准确定位网络上一台或多台主机;定位主机上的特定应用

(2)找到主机后如何可靠高效的进行数据传输

2、两个要素

(1)IP和端口号(2)网络通信协议(OSI参考模型,TCP/IP参考模型)

二、IP和端口

InetAddress类

java中使用该类表示IP,没有暴露构造方法,有2个实例化方法

InetAdress inet = InetAddress.getByName("192.168.0.1");

InetAdress inet = InetAddress.getByName("www.baidu.com");

InetAdress inet = InetAddress.getLocalHost();

实例化后有两个常用方法:getHostName();getHostAddress()

端口号:标识正在计算机上运行的进程(程序)

网络套接字:IP地址与端口号的组合

三、网络通信协议

TCP/IP参考模型:

应用层:HTTP,FTP,Telnet,DNS

传输层:TCP,UDP----可靠的和不可靠的

网络层:IP,ICMP,ARP

物理+数据链路层:Link

四、TCP网络编程

client端:

Socket clientSocket = new Socket(InetAddress,8888);

socket.getOutputStream().write(.....)

server端:

ServerSocket ss  = new ServerSocket(8888)

Socket socket = ss.accept();

socket.getInputStream().read();

五、URL编程

java.net.URL

URL url = new URL(.....);

HttpUrlConnection conn = url.openConnection();

conn.connect();

conn.getInputStream();

posted @ 2022-11-20 23:15  鼠标的博客  阅读(22)  评论(0编辑  收藏  举报