« 上一篇
个人整理非商业用途,欢迎探讨与指正!!
计算机网络:为了实现资源的共享和信息的传递,通过通讯线路将若干台主机连接在一起
常见的计算机网络:
互联网(Internet):点与点相连
万维网(WWW World Wide Web):端与端相连
物连网(IoT Internet of things):物与物相连
网络编程:让计算机与计算机之间搭建连接,进行通讯
该模型定义了网络的七层架构:
物理层 进行数据传输
数据链路层 保证准确性
网络层 分配IP地址
传输层 通过网络协议传输数据(TCP协议,UPD协议)
会话层 保持主机和主机之间的联系
表示层 将数据加密或者解密
应用层 指程序
该模型使用OSI参数模型,分为四层:
网络接口层:物理层 数据链路层
网络层:
传输层:
应用层:会话层 表示层 层
分配给每台计算机的唯一表示
IPV4:4个字节,32位组成,每位0~255的
IPV6:16个字节,128组成,每位0~65535的
A类:大型网络 网络地址.主机地址.主机地址.主机地址
B类:中型网络 网络地址.网络地址.主机地址.主机地址
C类:个人网络 网络地址.网络地址.网络地址.主机地址
本机地址:127.0.0.1 域名:localhost
windows:ipconfig
liunx:ifconfig
Java中获取IP的类
public class Demo01 {
public static void main(String[] args) throws UnknownHostException {
// 获取本机ip
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
System.out.println(localHost.getHostName());
System.out.println(localHost.getHostAddress());
// 获取指定的主机ip信息
localHost = InetAddress.getByName("www.baidu.com");
System.out.println(localHost);
System.out.println(localHost.getHostName());
System.out.println(localHost.getHostAddress());
System.out.println("======================");
// 获取指定ip地址的信息
localHost = InetAddress.getByName("192.168.40.228");
System.out.println(localHost);
System.out.println(localHost.getHostName());
System.out.println(localHost.getHostAddress());
localHost = InetAddress.getByName("10.2.10.52");
System.out.println(localHost);
System.out.println(localHost.getHostName());
System.out.println(localHost.getHostAddress());
}
}
每个设备程序在计算机上的唯一数字标识
端口号范围:0~65535
公认端口号:0~1023
注册端口号:1024~49151
动态端口号:49151~65535
常见的端口号:
mysql:3306
oracle:1521
redis:6379
tomcat:8080
web服务器:80
ftp服务器:21
自定义端口号:1024~65535
TCP:
面向连接,安全可靠,效率低,数据大小无限制
打电话
UDP:
面向无连接,不安全,效率高,数据大小64kb
广播/对讲机
案例一:
// 客户端:只有启动状态的服务器可以接收我们的请求
public class Client {
public static void main(String[] args) throws Exception {
// 创建客户端Socket对象
Socket socket = new Socket("192.168.40.172",6666);
// 创建Socket输出流
OutputStream out = socket.getOutputStream();
// 写内容
out.write("6666你好,我是客户端wxx".getBytes());
System.out.println("我写完了...");
// 关闭资源
out.close();
socket.close();
}
}
// 服务器端:启动状态
// 服务器端 ip:192.168.40.172 端口:6666
public class Server {
public static void main(String[] args) throws Exception {
// 创建服务器端Socket
ServerSocket serverSocket = new ServerSocket(6666);
// 通过serverSocket创建Socket对象
// 接收到的信息,进行处理
Socket accept = serverSocket.accept();
// 获取客户端发送的内容
InputStream in = accept.getInputStream();
byte[] bs = new byte[1024];
int len;
while( (len = in.read(bs)) != -1 ) {
System.out.println(new String(bs,0,len));
}
in.close();
accept.close();
serverSocket.close();
}
}
案例二:使用多线程完成
// 客户端:只有启动状态的服务器可以接收我们的请求
public class Client {
public static void main(String[] args) throws Exception {
// 创建Scanner
Scanner scanner = new Scanner(System.in);
while(true) {
// 创建客户端Socket对象
Socket socket = new Socket("localhost",6666);
// 创建Socket输出流
OutputStream out = socket.getOutputStream();
System.out.println("你想说啥");
String words = scanner.next();
if("886".equals(words) || "拜拜".equals(words)) {
// 关闭资源
out.close();
socket.close();
break;
}
// 写内容
out.write(words.getBytes());
System.out.println("我写完了...");
// 关闭资源
out.close();
socket.close();
}
scanner.close();
}
}
// 服务器端:启动状态
// 服务器端 ip:192.168.40.172 端口:6666
public class Server {
public static void main(String[] args) throws Exception {
// 创建服务器端Socket
ServerSocket serverSocket = new ServerSocket(6666);
System.out.println("服务器已启动,等待您的输入...");
while(true) {
// 通过serverSocket创建Socket对象
// 接收到的信息,进行处理
Socket accept = serverSocket.accept();
// 获取ip信息
String ip = accept.getInetAddress().getHostAddress();
String hostName = accept.getInetAddress().getHostName();
new Thread(new Runnable() {
@Override
public void run() {
try {
InputStream in = accept.getInputStream();
// 转换流
InputStreamReader reader = new InputStreamReader(in);
// 字符缓冲流
BufferedReader br = new BufferedReader(reader);
// 读取内容
String readLine = br.readLine();
System.out.println(ip+":"+hostName+":"+readLine);
br.close();
in.close();
accept.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
}
案例三:文件上传
// 客户端:只有启动状态的服务器可以接收我们的请求
public class Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("192.168.40.172",6666);
// 创建Socket输出流
OutputStream out = socket.getOutputStream();
// 从当前电脑的本地上传一个文件
FileInputStream in = new FileInputStream("file/1.jpg");
// 边读边写
byte[] bs = new byte[1024];
int len;
while( (len = in.read(bs)) != -1) {
// 写向服务器端
out.write(bs,0,len);
}
// 告知服务器,我们写完了
socket.shutdownOutput();
// 等待服务器的返回信息
InputStream input = socket.getInputStream();
InputStreamReader reader = new InputStreamReader(input);
BufferedReader br = new BufferedReader(reader);
String str;
while( (str = br.readLine()) != null) {
System.out.println(str);
}
// 关闭资源
br.close();
reader.close();
input.close();
in.close();
out.close();
socket.close();
}
}
// 服务器端:启动状态
// 服务器端 ip:192.168.40.172 端口:6666
public class Server {
public static void main(String[] args) throws Exception {
// 创建服务器端Socket
ServerSocket serverSocket = new ServerSocket(6666);
System.out.println("服务器已启动,等待您的输入...");
while(true) {
Socket accept = serverSocket.accept();
new UploadThread(accept).start();
}
}
}
class UploadThread extends Thread {
private Socket socket;
public UploadThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
// 读取流
InputStream in = socket.getInputStream();
// 写入到指定的位置
FileOutputStream out = new FileOutputStream("upload/"+UUID.randomUUID().toString()+".jpg");
byte[] bs = new byte[1024];
int len;
while( (len = in.read(bs)) != -1) {
out.write(bs, 0, len);
}
// 向客户端返回信息
OutputStream outputStream = socket.getOutputStream();
outputStream.write("success!您的文件以上传成功".getBytes());
outputStream.close();
out.close();
in.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}