计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路和通信设备连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。(来源百度百科)
无线电台…
传播交流信息
数据交换
通信
1.如何准确的定位一台主机 192.168.x.x : 端口,定位到这个计算机的某个资源
2. 找到了主机如何传输数据呢?
package com.IP;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
/**
* @Description
* 测试IP
* @Author zhaopeng
* @Date 2023/10/12 10:49
*/
public class TestInetAddress {
public static void main(String[] args) {
try {
// 查询本机和百度的地址
InetAddress baiduName = InetAddress.getByName("www.baidu.com");
InetAddress localhost1 = InetAddress.getByName("localhost");
InetAddress localHost = InetAddress.getLocalHost();
System.out.println("baiduName-"+baiduName);
System.out.println("localhost1-"+localhost1);
System.out.println("localHost-"+localHost);
// 常用的方法
System.out.println("baiduName.getAddress()--" + Arrays.toString(baiduName.getAddress()));;
System.out.println("baiduName.getCanonicalHostName()--"+baiduName.getCanonicalHostName()); // 规范的名字
System.out.println("baiduName.getHostAddress()--"+baiduName.getHostAddress()); // 获取主机名地址(ip)
System.out.println("baiduName.getHostName() -- "+baiduName.getHostName());// 获取域名,或者自己电脑的名字
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
netstat -ano #查看所有的端口
netstat -ano|findstr "5900" # 查看指定的端口
tasklist|findstr "8696" 查看指定的端口的进程
package com.IP;
import java.net.InetSocketAddress;
/**
* @Description
* @Author zhaopeng
* @Date 2023/10/12 11:25
*/
public class TestInetSocketAddress {
public static void main(String[] args) {
InetSocketAddress address = new InetSocketAddress("www.baidu.com", 80);
InetSocketAddress localhost = new InetSocketAddress("localhost", 8080);
System.out.println(address);
System.out.println(localhost);
System.out.println(address.getHostName()); // 获取地址
System.out.println(address.getAddress());
System.out.println(address.getPort());// 获取端口
}
}
package com.IP.Demo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Description
* 服务器
* @Author zhaopeng
* @Date 2023/10/12 13:38
*/
public class TcpServer {
public static void main(String[] args) {
ServerSocket serverSocket =null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
// 1.创建端口号
serverSocket = new ServerSocket(9999);
while (true){
// 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 {
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 com.IP.Demo;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @Description 客户端
* @Author zhaopeng
* @Date 2023/10/12 13:38
*/
public class TcpClient {
public static void main(String[] args) {
OutputStream os = null;
Socket socket = null;
try {
// 1.需要知道服务器的地址,端口号
InetAddress serverIp = InetAddress.getByName("localhost");
int port = 9999;
// 2.创建一个socket连接
socket = new Socket(serverIp,port);
// 3. 发送消息 IO流
os = socket.getOutputStream();
os.write("你好,我是客户端,我要写东西".getBytes());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException 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 com.IP.FileDemo;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @Description
* 文件上传
* @Author zhaopeng
* @Date 2023/10/12 14:33
*/
public class FileClient {
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 is = new FileInputStream(new File("2.jpg"));
// 4. 写出文件
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
os.write(buffer,0,len);
}
// 通知服务器已经结束
socket.shutdownOutput();
// 确定服务器接收完毕,再断开
InputStream is2 = socket.getInputStream();
// 来个管道,把is2接收到的东西转化一下
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2 = is2.read(buffer2)) != -1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
// 关闭资源
baos.close();
is2.close();
is.close();
os.close();
socket.close();
}
}
package com.IP.FileDemo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Description
* @Author zhaopeng
* @Date 2023/10/12 14:37
*/
public class FileServer {
public static void main(String[] args) throws Exception{
// 1.创建serversocket服务端口
ServerSocket serverSocket = new ServerSocket(9000);
// 2. 监听客户端的连接,等待连接
Socket accept = serverSocket.accept(); // 阻塞式监听,会一直等待
// 3. 获取输入流
InputStream is = accept.getInputStream();
// 4.文件输出
// 需要一个管道来接受
FileOutputStream fos = new FileOutputStream(new File("testtest.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
// 传输完毕开始向客户端发送消息
OutputStream outputStream = accept.getOutputStream();
outputStream.write("已经接受完毕,情断开".getBytes());
// 5.关闭资源
outputStream.close();
fos.close();
is.close();
accept.close();
serverSocket.close();
}
}
package com.IP.UDPDemo;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* @Description
* @Author zhaopeng
* @Date 2023/10/12 15:33
*/
//不需要连接服务器
public class UDPClient {
public static void main(String[] args) throws Exception{
// 1. 建立一个Socket
DatagramSocket socket = new DatagramSocket();
// 2. 建立一个包
String msg = "这里是UDP协议通讯";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9000;
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
// 3. 发送包
socket.send(packet);
// 4.关闭流
socket.close();
}
}
package com.IP.UDPDemo;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/**
* @Description
* @Author zhaopeng
* @Date 2023/10/12 15:33
*/
public class UDPServer {
public static void main(String[] args) throws Exception {
// 开放端口
DatagramSocket socket = new DatagramSocket(9000);
// 开始接受数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet); // 阻塞式接受
System.out.println(packet.getAddress().getHostAddress()); // 获取ip
System.out.println(new String(packet.getData(),0,packet.getLength()));
// 关闭连接
socket.close();
}
}
· 发送方:
package com.IP.UDPDemo.chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
/**
* @Description
* @Author zhaopeng
* @Date 2023/10/12 15:54
*/
public class UdpSender {
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[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
// 发送
socket.send(packet);
if ("bye".equals(data)){
break;
}
}
socket.close();
}
}
package com.IP.UDPDemo.chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.Arrays;
/**
* @Description
* @Author zhaopeng
* @Date 2023/10/12 15:53
*/
public class UdpReceive {
public static void main(String[] args) throws Exception{
DatagramSocket socket = new DatagramSocket(6666);
while (true){
// 准备接受包裹
byte[] buffer= new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet); // 阻塞式接受
// 断开连接
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(receiveData);
if ("bye".equals(receiveData)){
break;
}
}
socket.close();
}
}
TalkSender实现了Runnable接口
package com.IP.UDPDemo.chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
/**
* @Description
* @Author zhaopeng
* @Date 2023/10/12 16:24
*/
public class TalkSender implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSender(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(toIP, toPort));
// 发送
socket.send(packet);
if ("bye".equals(data)){
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
socket.close();
}
}
package com.IP.UDPDemo.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @Description
* @Author zhaopeng
* @Date 2023/10/12 16:28
*/
public class TalkReceive implements Runnable{
private String msgFrom;
private int fromPort;
DatagramSocket socket = null;
public TalkReceive(int fromPort,String msgFrom) {
this.fromPort = fromPort;
this.msgFrom =msgFrom;
try {
socket = new DatagramSocket(this.fromPort);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
// 准备接受包裹
byte[] buffer= new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet); // 阻塞式接受
// 断开连接
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(msgFrom + ":"+receiveData);
if ("bye".equals(receiveData)){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
package com.IP.UDPDemo.chat;
/**
* @Description
* @Author zhaopeng
* @Date 2023/10/12 16:35
*/
public class Student {
public static void main(String[] args) {
new Thread(new TalkSender(7778,"localhost",9998)).start();
new Thread(new TalkReceive(6666,"老师")).start();
}
}
package com.IP.UDPDemo.chat;
/**
* @Description
* @Author zhaopeng
* @Date 2023/10/12 16:37
*/
public class Teacher {
public static void main(String[] args) {
new Thread(new TalkSender(4444,"localhost",6666)).start();
new Thread(new TalkReceive(9998,"学生")).start();
}
}
URL:统一资源定位符,定位互联网上的某一个资源
DNS:域名解析 www.baidu.com x.xx.x.x 把域名解析为ip地址
1. 协议://ip地址:端口/项目名/资源
package com.IP.URL;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @Description
* @Author zhaopeng
* @Date 2023/10/12 17:30
*/
public class URLTest {
public static void main(String[] args) throws IOException {
URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=dapeng&password=123");
System.out.println("协议:" + url.getProtocol());
System.out.println("主机IP:" + url.getHost());
System.out.println("端口:" + url.getPort());
System.out.println("文件路径:" + url.getPath());
System.out.println("全路径:" + url.getFile());
System.out.println("参数:" + url.getQuery());
System.out.println("主机IP+端口:" + url.getAuthority());
}
}
package com.IP.URL;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* @Description
* @Author zhaopeng
* @Date 2023/10/12 17:40
*/
public class DownLoadFile {
public static void main(String[] args) throws Exception {
URL url = null;
// 1. 下载地址
url = new URL("https://m701.music.126.net/20231012181404/f7d562aebfe48ec3e14d1582c678bf9d/jdyyaac/060c/0e5c/5259/1f601e5eb2966119ebfde37f17c0bb41.m4a");
// 2. 开启连接,HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 3. 获取文件
InputStream is = connection.getInputStream();
// 4.下载文件
FileOutputStream fos = new FileOutputStream("f.m4a");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer);
}
fos.close();
is.close();
connection.disconnect();
}
}