计算机网络

image.png
- javaweb 网页编程 B/S
- TCP/IP C/S
网络通信的要素
双方地址
1.ip
2.端口号
image.png
- ping +网址,可以得到地址
网络编程中的要素
1.ip号和端口号
2.网络通信协议 udp,tcp
IP
- ip地址InetAddress
- ip地址分类
1.ipv4/ipv6 (4个字节,0-255) - ipv4 四个字节
ip6 128位 8个无符号整数
2.公网-私网ABCD类,折半
192.168.xx.xx专门给组织内部使用
测试ip
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestIP {
public static void main(String[] args) {
try {
//查询本机
InetAddress[] inetAddress1 = InetAddress.getAllByName("127.0.0.1");
System.out.println(inetAddress1);
//locohost或者getLocalHost都可以
//查询网站ip
InetAddress[] inetAddress2 = InetAddress.getAllByName("www.baidu.com");
System.out.println(inetAddress2);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
}
端口Port
- 端口表示计算机上的一个程序进程
- 规定:0-65535
- TCP,UDP:65535*2(单个协议下不能被冲突)
- 端口分类:
1.公有端口0-1023 - HTTP:80
- HTTPS:443
- FTP:21
-Telent:23
2.程序注册端口:1024-49151 分配用户或者程序 - Tomcat 8080
- MySQL:3306
- Oracle:1521
3.动态私有:49152~65534 - netstat -ano #查看所有端口
- netstat -ano|findstr "5900"查看指定端口
- tasklist|findstr"8696" 查找指定端口进程
- Ctrl+shift+Esc
通信协议
网络通信协议
- 速率
- 传输码率
- 代码结构
- 传输控制
TCP/IP协议
- 重要:
1.TCP:用户传输协议
2.UDP用户数据报协议
-出名的协议
1.TCP
2.IP网络互联协议
TCP
- 连接稳定
- 三次握手,四次分手
- 客户端服务端
UDP
- 不连接不稳定
- 客户端服务端没有明确界限
- 不管有没有准备好,都可以发给你
- DDOS:洪水攻击
聊天
ackage Tcp;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
//客户端
public class Client {
public static void main(String[] args) {
Socket socket=null;
OutputStream os=null;
//1.要知道服务器地址
try {
InetAddress severIP=InetAddress.getByName("127.0.0.1");
int port = 9999;
//2.创建一个socket连接
socket = new Socket(severIP,port);
//3.发送消息 IO流
os=socket.getOutputStream();
os.write("shy".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package Tcp;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class Sever {
public static void main(String[] args) {
ServerSocket serverSocket=null;
Socket socket=null;
InputStream is=null;
ByteArrayOutputStream baos=null;
//要有一个地址
try {
serverSocket = new ServerSocket(9999);
//2.等待客户端连过来
socket = serverSocket.accept();
//3.读取客户端信息
is = socket.getInputStream();
// byte[] buffer=new byte[1024];
// int len;
// while((len=is.read(buffer))!=-1){
// String msg=new String(buffer,0,len);
// System.out.println(msg);
//管道流
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 {
if (baos!=null)
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
if (is!=null)
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
if (socket!=null)
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
if (serverSocket!=null)
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
上传文件
package Tcp;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client2 {
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);
}
//通知服务器,我已经结束了
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);
}
System.out.println(baos.toString());
//关闭资源
fis.close();
os.close();
socket.close();
}
}
package Tcp;
import com.sun.source.tree.NewArrayTree;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Sever2 {
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.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//通知客户端我接受完毕了
OutputStream os = socket.getOutputStream();
os.write("我接收完毕了".getBytes());
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
Tomcat
- 同一协议端口不能暂停
UDP
- 不需要链接服务器
public class Client {
public static void main(String[] args) throws Exception{
//建立socket
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();
}
}
package UDP;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Sever {
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();
}
}
URL
- 统计资源定位符
协议//ip地址//端口//项目名//资源
image.png

