计算机网络简介

网络编程的目的:

无限电台。。。。传播交流信息,数据交换。通信

想要达到这个效果需要什么:

1.如何准确的定位网络上的一台主机 ip地址192.168.16.124: 端口,定位到这个计算机上的某个资源

2.找到了这个主机,如何传输数据

Javaweb: 网页编程 B/s Brower/Server(浏览器/服务器)架构

网络编程:TCP/IP C/S Client/Server(客户端/服务器)

网络通信的要素

如何实现网络的通信?

通信双方地址:


  • IP
  • 端口号
  • 192.168.16.124::5900
  • 规则:网络通信的协议TCP/IP[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3bCCN96B-1608736331564)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20201223163659380.png)]小结:1.网络编程中有两个主要的问题

  • 如何准确的定位到网络上的一台或多台主机
  • 找到主机之后如何进行通信


2.网络编程中的要素

IP和端口号 IP

网络通信协议 udp tcp

万物皆对象

IP地址

ip地址:inteAddress 查看本地cmd ipconfig


  • 唯一定位一台网络上计算机
  • 127.0.0.1:本机localhost
  • ip地址的分类
  • ​package com.kuang.demo1; import java.net.InetAddress; import java.net.UnknownHostException; public class TestInetAddress { public static void main(String[] args) { 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")); InetAddress inetAddress = InetAddress.getByName("www.baidu.com"); System.out.println(inetAddress); } catch (UnknownHostException e) { e.printStackTrace(); } } } ​


ipv4/Ipv6


  • ipv4 127.0.0.1 4个字节组成。 0~255 42亿个
  • ipv6 fe80::60f8:dddd:6bea:17a7%5 ,128位。8个无符号整数!



公网(互联网)-私网(局域网)



ABCD类地址



192.168.xx.xx,专门给组织内部使用的






端口

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Cs9DwFkt-1608736331566)(D:\image-20201223185145889.png)]

etstat -ano #查看所有的窗口
netstat -ano|findstr"5900" #查看指定的端口
taskList|findstr "8696" #查看指定端口的进程
ctrl+shift+Esc

通信协议

网络通信协议:速率,传输码率,代码结构,传输控制…

TCP/IP协议簇:实际上是一组协议

重要:


  • TCP:用户传输协议
  • UDP:用户数据报协议

出名的协议:


  • TCP:
  • IP:网络互连协议

TCP和UDP对比

TCP

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MedDcwVh-1608736331567)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20201223192731408.png)]

UDP

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aVKxdpRv-1608736331569)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20201223192810022.png)]

TCP实现聊天

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ssdtBAu3-1608736331571)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20201223202354809.png)]

package com.kuang.demo02;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpClientDemo01 {
public static void main(String[] args) throws Exception {
Socket socket=null;
OutputStream os=null;
try {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port=9999;
socket = new Socket(serverIP, port);
os = socket.getOutputStream();
os.write("asdfdasfdsa".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
os.close();
socket.close();


}


}

}
package com.kuang.demo02;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo01 {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket=null;
Socket socket=null;
InputStream is=null;
ByteArrayOutputStream baos=null;
//1.创建服务
try {
serverSocket = new ServerSocket(9999);
//2.监听客户端的连接
socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
//3.获取输入流
is = socket.getInputStream();
//管道流
baos=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
baos.close();
is.close();
socket.close();
serverSocket.close();
}
}

}

TCP实现文件上传

package com.kuang.demo02;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpClientDemo01 {
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("1.png"));
//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();
}
}
package com.kuang.demo02;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo01 {
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(new File("receive.png"));
byte[] buffer=new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}

初始Tomacat

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bfRcXMKH-1608736331573)(D:\image-20201223211354080.png)]

tomacat乱码问题解决

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fnBL00Ta-1608736331573)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20201223212231847.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xAcUE7Nb-1608736331574)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20201223212203132.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qi1waKcv-1608736331574)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20201223212613753.png)]

UDP消息发送

byte转化为string用构造器

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-l5w02V5O-1608736331575)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20201223214550066.png)]

发送信息

package com.kuang.demo03;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
//建立Socket
DatagramSocket socket=new DatagramSocket();

//打包数据包
String msg="hello";
InetAddress address=InetAddress.getByName("localhost");
int port=9090;

DatagramPacket packet=new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,address,port);

socket.send(packet);

socket.close();
}
}
package com.kuang.demo03;

import java.net.DatagramPacket;
import java.net.DatagramSocket;


public class UdpServerDemo01 {
public static void main(String[] args) throws Exception {
//开放端口
DatagramSocket socket=new DatagramSocket(9090);
//接受数据包
byte[] buffer=new byte[1024];
DatagramPacket packet=new DatagramPacket(buffer,0,buffer.length);

socket.receive(packet);//阻塞接收
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
//关闭连接
socket.close();
}
}

在线咨询

{
DatagramSocket socket=null;
private int port;
private String msgFrom;

public TalkReceive(int port,String msgFrom) {
this.port=port;
this.msgFrom=msgFrom;

try {
socket=new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}

@Override
public void run() {
while (true) {
try {
byte[] container = new byte[1024];
DatagramPacket packet= new DatagramPacket(container, 0,container.length);
socket.receive(packet);

byte[] data=packet.getData();
String receiveData=new String(data,0,data.length);

System.out.println(msgFrom+ ":" +receiveData);

if (receiveData.equals("bye") ) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
package com.kuang.demo03;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;

public class TalkSend implements Runnable{
DatagramSocket socket=null;
BufferedReader reader=null;


private int fromPort;
private String toIP;
private int toport;

public TalkSend(int fromPort, String toIP, int toport) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toport = toport;
try {
socket=new DatagramSocket(fromPort);
reader=new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void run() {
while(true){
try {
String data=reader.readLine();
byte[] datas=data.getBytes();
DatagramPacket packet= new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toport));

socket.send(packet);

if (data.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
package com.kuang.demo03;
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}

}
package com.kuang.demo03;

public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}

URL下载网络资源

统一资源定位符:定位资源,定位互联网上某一个资源

DNS域名解析 WWW.baidu 解析成xxx.xx.x.x

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PULcLLfK-1608736331576)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20201223224441775.png)]


package com.kuang.demo04;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLDemo01 {
public static void main(String[] args) throws IOException {
URL url = new URL("https://www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

InputStream in = urlConnection.getInputStream();
FileOutputStream Fout = new FileOutputStream("1.svg");

byte[] buff = new byte[1024];
int len;
while ((len = in.read(buff)) != -1) {
Fout.write(buff, 0, len);
}

Fout.close();
in.close();
urlConnection.disconnect();
}
}