网络编程
1.1概述
网络编程的目的:
无限电台。。。。传播交流信息,数据交换。通信
想要达到这个效果需要什么:
1.如何准确的定位网络上的一台珠玑 ip地址 端口,定位到这个计算机上的某个资源
2.找到了这个珠玑,如何传输数据
Javaweb: 网页编程 B/s
网络编程:TCP/IP C/S1.2网络通信的要素
如何实现网络的通信?
通信双方地址:
- IP
- 端口号
- 192.168.16.124::5900
- 规则:网络通信的协议
TCP/IP
小结:- 网络编程中有两个主要的问题
- 如何准确的定位到网络上的一台或多台珠玑
- 找到主机之后如何进行通信
- 网络编程中有两个主要的问题
- 网络编程中高端要素
- IP和端口号 IP
- 网络通信协议 udp tcp
- 万物皆对象1.3ip
ip地址:inteAddress 查看本地cmd ipconfig
- 唯一定位一台网络上计算机- 127.0.0.1:本机localhost- ip地址的分类- ipv4/Ipv6- ipv4 127.0.0.1 4个字节组成。 0~255 42亿个- ipv6 fe80::60f8:dddd:6bea:17a7%5 ,128位。8个无符号整数!
try {InetAddress localHost = InetAddress.getLocalHost();System.out.println(localHost);InetAddress[] inetAddress1 = InetAddress.getAllByName("127.0.0.1");System.out.println(inetAddress1);System.out.println(InetAddress.getByName("localhost"));} catch (UnknownHostException e) {e.printStackTrace();}}
1.4端口
netstat -ano #查看所有的窗口netstat -ano|findstr"5900" #查看指定的端口taskList|findstr "8696" #查看指定端口的进程ctrl+shift+Esc
public static void main(String[] args) {InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);InetSocketAddress localhost = new InetSocketAddress("localhost", 8080);System.out.println(inetSocketAddress);System.out.println(localhost);}}
1.5通信协议
网络通信协议:速率,传输码率,代码结构,传输控制….
TCP/IP协议簇:实际上是一组协议
重要:
- TCP:用户传输协议
- UDP:用户数据报协议
出名的协议:
服务端(server)
public static void main(String[] args) throws Exception {//1.创建服务ServerSocket serverSocket = new ServerSocket(9000);//2.监听客户端的连接Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接//3.获取输入流InputStream is = socket.getInputStream();//4.文件输出FileOutputStream fos = new FileOutputStream(newFile("receive"));byte[] buffer=new byte[1024];int len;while ((len=is.read(buffer))!=-1){fos.write(buffer,0,len);}//同知服务端已经传输完了socket.shutdownOutput();//传输完毕//确定服务端接收完毕,断开连接InputStream inputStream = socket.getInputStream();ByteArrayOutputStream baos = new ByteArrayOutputStream();//管道流byte[] buffer2=new byte[1024];int len2;while ((len2=inputStream.read(buffer2))!=-1){baos.write(buffer2,0,len2);}//关闭资源baos.close();inputStream.close();fos.close();is.close();socket.close();serverSocket.close();}
客户端(Client )
public static void main(String[] args) throws Exception {//1.创建一个Socket连接Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);//2.创建一个输出流OutputStream os = socket.getOutputStream();//3.读取文件FileInputStream fis = new FileInputStream(new File(""));//4.写出文件byte[] buffer=new byte[1024];int len;while ((len=fis.read(buffer))!=-1){os.write(buffer,0,len);}//通客户端已经接受完了OutputStream outputStream = socket.getOutputStream();outputStream.write("我已经接收完了,可以断开了".getBytes());//5.关闭资源fis.close();os.close();socket.close();}
UDP*
URL下载资源
public static void main(String[] args) throws Exception {//1.下载地址URL url=new URL("https://baikevideo.cdn.bcebos.com/media/mda-Of9hv9gVColin1F7/492af5f8e199a6a79e31b500cfd862f9.mp4");//2.连接到这个资源 HTTPHttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();InputStream inputStream=urlConnection.getInputStream();FileOutputStream fos=new FileOutputStream("以父之名.mp4");byte[] buffer=new byte[1024];int len;while ((len=inputStream.read(buffer))!=-1){fos.write(buffer,0,len);//写出这个数据}fos.close();inputStream.close();urlConnection.disconnect();//断开连接}}
