1.概述
邮件:
计算机网络: 计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。 网络编程的目的:无线电台…传播交流信息,数据交换,通信。
想要达到这个效果需要什么:
- 如何让准确的定位网络上的一台主机 192.168.16.124:端口, 定位上这个计算机上的某个资源。
- 找到了这个主机,如何传输数据呢?
Javaweb:网页编程 、 B/S架构 网络编程:TCP/IP 、 C/S
2.网络通信要素
如何实现网络的通信?
- 通信双方的地址:IP+端口,才定位到一台主机的某个应用
- 规则:网络通信的协议:TCP/IP
3.IP
IP 地址:InetAddress
2406:da18:ddf:4000:67d5:b226:cad7:125b
-
公网(互联网)—私网(局域网) 公网:ABCD地址 私网:192.168.xx.xx,专门给组织内部使用。
- 域名:记忆IP问题!(由于IP地址具有不方便记忆并且不能显示地址组织的名称和性质等缺点,人们设计出了域名,并通过网域名称系统(DNS,Domain Name System)来将域名和IP地址相互映射,使人更方便地访问互联网,而不用去记住能够被机器直接读取的IP地址数串。)
IP:www.vip.com
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test01 {
public static void main(String[] args) throws UnknownHostException {
InetAddress byName = InetAddress.getByName("127.0.0.1");
InetAddress byName1 = InetAddress.getByName("localhost");
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
InetAddress byName2 = InetAddress.getByName("www.baidu.com");
System.out.println(byName2);
System.out.println("============");
System.out.println(byName2.getAddress());
System.out.println(byName2.getCanonicalHostName());
System.out.println(byName2.getHostAddress());
System.out.println(byName2.getHostName());
}
}
output:
SCSE-PC/192.168.0.107
www.baidu.com/110.242.68.4
============
[B@17c68925
110.242.68.4
110.242.68.4
www.baidu.com
4.端口
- 端口表示计算机上一个程序的进程;
- 不同的进程有不同的端口号!用来区分软件!
- 被规定0~65535
- TCP,UDP:65535*2个端口 tcp:80 udp:80 单个协议下,端口号不能冲突
- 端口的分类
- 共有端口 0~1023 内置的进程使用
HTTP:80 HTTP:443 如访问https://www.baidu.com:443 访问的还是百度 FTP:21 TELENT:23 - 程序注册端口:1014-49151,分配给用户和程序
Tomcat:8080 MySql:3306 Oracle:1521 - 动态、私有端口:49152~65535
netstat -ano #查看所有的端口 netstat -nao|findstr “7808” #查看指定的端口 tasklist|findstr “8696”#查看指定端口的进程 Ctrl + Shift +ESC //打开任务管理器的快捷键
import java.net.InetSocketAddress;
public class Test02 {
public static void main(String[] args) {
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
System.out.println(inetSocketAddress);
System.out.println(inetSocketAddress.getAddress());
System.out.println(inetSocketAddress.getHostName());
System.out.println(inetSocketAddress.getPort());
}
}
5.通信协议
协议:约定,就好比中国人交流说的是普通话
网络通信协议: 速率,传输码率,代码结构,传输控制…
问题:非常的复杂
大事化小:分层
TCP/IP协议簇:实际上是一组协议
重要的协议: TCP:用户传输协议 UDP:用户数据报协议 IP:网络互联协议
TCP UDP 对比:
- TCP:可以想象成打电话,需要连接、稳定;三次握手,四次挥手;客户端,服务器;传输完成,释放连接,效率低
最少需要三次握手,保证稳定连接! A:你瞅啥? B:瞅你咋地? A:干一场
四次挥手 A:我要分手了 B:我知道你要分手了 B:你真的要分手吗? A:我真的要分手了
- UDP:可以想象成发短信,不连接,不稳定;客户端、服务端:没有明确的解现;不管有没有准备好,都可以发给你;DDOS:洪水攻击! 发垃圾包 堵塞线路 (饱和攻击);
6.TCP
6.1模拟聊天 先启动服务端,再启动客户端!!!!
客户端:
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TCPClientDemo01 {
public static void main(String[] args) throws IOException {
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("欢迎学习Java!".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
os.close();
socket.close();
}
}
}
服务器端
- 建立服务的端口 ServerSocket
- 等待的用户的连接 accept
- 接收用户的消息
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServerDemo01 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
serverSocket = new ServerSocket(9999);
socket = serverSocket.accept();
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();
}
}
}
6.2模拟文件上传 客户端
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TCPClientDemo02 {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
OutputStream os = socket.getOutputStream();
FileInputStream stream = new FileInputStream(new File("subei.jpg"));
byte[] buffer = new byte[1024];
int len = 0;
while((len = stream.read(buffer)) != -1){
os.write(buffer,0,len);
}
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2 = 0;
while((len2 = stream.read(buffer2)) != -1){
byteArrayOutputStream.write(buffer2,0,len2);
}
System.out.println(byteArrayOutputStream);
byteArrayOutputStream.close();
inputStream.close();
stream.close();
os.close();
socket.close();
}
}
服务器端
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServerDemo02 {
public static void main(String[] args) throws Exception{
ServerSocket socket = new ServerSocket(9000);
Socket accept = socket.accept();
InputStream is = accept.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("resort.jpg");
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer)) != -1){
fileOutputStream.write(buffer,0,len);
}
OutputStream outputStream = accept.getOutputStream();
outputStream.write("服务器已经收集成功,请断开连接!".getBytes());
outputStream.close();
fileOutputStream.close();
is.close();
accept.close();
socket.close();
}
}
7.初识Tomcat
服务器端:
- 自定义 S
- Tomcat服务器 S :Java后台开发
客户端:
8.UDP
Java使用DatagramSocket代表UDP协议的Socket,DatagramSocket的唯一作用就是接收和发送数据报,Java使用DatagramPacket来代表数据报,DatagramSocket接收和发送的数据都是通过DatagramPacket对象完成的。
1.发信息
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String msg = "你好啊!";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
socket.send(packet);
socket.close();
}
}
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();
}
}
output:
127.0.0.1
你好啊!
2.循环发信息
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class chatSenderDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true){
String data = reader.readLine();
byte[] bytes = data.getBytes();
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress("localhost",9090));
socket.send(packet);
if(data.equals("bye")){
break;
}
}
socket.close();
}
}
input:
nihao
nihao2
bye
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class chatReceiveDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9090);
while(true){
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, 0, data.length);
socket.receive(packet);
byte[] datas = packet.getData();
String receiveData = new String(datas);
System.out.println(receiveData);
if(receiveData.equals("bye")){
break;
}
}
socket.close();
}
}
output:
nihao
nihao2
bye
3.在线聊天 利用多线程
发送端
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TalkSend implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromport;
private String toID;
private int toPort;
public TalkSend(int fromport, String toID, int toPort) throws SocketException {
this.fromport = fromport;
this.toID = toID;
this.toPort = toPort;
DatagramSocket socket = new DatagramSocket(fromport);
reader = new BufferedReader(new InputStreamReader(System.in));
}
@Override
public void run() {
while(true){
String data = null;
try {
data = reader.readLine();
byte[] bytes = data.getBytes();
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress(this.toID,this.toPort));
socket.send(packet);
if(data.equals("bye")){
break;
}
}
catch (Exception e) {
e.printStackTrace();
}
socket.close();
}
}
}
接收端
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable {
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TalkReceive(int port,String msgFrom) throws SocketException {
this.port = port;
this.msgFrom = msgFrom;
DatagramSocket socket = new DatagramSocket(port);
}
@Override
public void run() {
while(true){
try {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, 0, data.length);
socket.receive(packet);
byte[] datas = packet.getData();
String receiveData = new String(datas);
System.out.println(msgFrom + ":" + receiveData);
if(receiveData.equals("bye")){
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
socket.close();
}
}
public class TalkStudent {
public static void main(String[] args) throws Exception {
new Thread(new TalkSend(7777,"127.0.0.1",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
public class TalkTeache {
public static void main(String[] args) throws Exception {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
9.URL
URL又被称为统一资源定位符:定位互联网上的某一个资源 URL的一般语法格式为:(带方括号[]的为可选项)
协议://iP地址:[端口]//路径
如https://www.baidu.com 注意www.baidu.com是通过DNS域名解析iPhone地址得到的 DNS域名解析:www.baidu.com —> xxx.xxx.xxx.xxx
import java.net.MalformedURLException;
import java.net.URL;
public class test01 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/helloworld/index.jsp?username = tutu && password = 123");
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPath());
System.out.println(url.getPort());
System.out.println(url.getFile());
System.out.println(url.getQuery());
}
}
output:
http
localhost
/helloworld/index.jsp
8080
/helloworld/index.jsp?username = tutu && password = 123
username = tutu && password = 123
|