计算机网络
计算机网络是指将[地理位置]不同的具有独立功能的多台计算机及其外部设备,通过[通信线路]和通信设备连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统
网络通信的要素
IP和端口号
网络通信协议
IP地址:inetAddress
//测试IP
public class TestInetAddress {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress3);
//查询网站Ip地址
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
表示计算机上的一个程序的进程
不同的进程有不同的端口号
同一个协议下,端口号不能冲突
端口分类
共有端口 0-1023
HTTP:80
HTTPS:443
程序注册端口 1024-49151
Tomcat:8080
MySQL:3309
Oracle:1521
动态 ,私有:49152-65535
public class TestInetSockAddress {
public static void main(String[] args) {
InetSocketAddress inetSocketAddress1 = new InetSocketAddress("127.0.0.1", 8080);
System.out.println(inetSocketAddress1);
InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhoet", 8080);
System.out.println(inetSocketAddress2);
}
}
TCP/IP协议簇:实际上是一组协议
TUP :用户传输协议
UDP:用户数据报协议
出名的协议
TCP
IP:网络互连协议
//客户端
public class TestTcpClient {
public static void main(String[] args) throws Exception{
//要知道服务器的地址
InetAddress serverIp = InetAddress.getByName("127.0.0.1");
//端口号
int port=9999;
//创建一个socket连接
Socket socket = new Socket(serverIp,port);
//发送消息 IO流
OutputStream outputStream = socket.getOutputStream();
outputStream.write("林夕的花海".getBytes());
outputStream.close();
}
}
===============================================
//服务端/客户端
public class TestTcpServer {
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(9999);
//等待客户端连接过来
Socket socket = serverSocket.accept();
//读取客户端的消息
InputStream inputStream = socket.getInputStream();
//管道流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf=new byte[1024];
int len;
while((len=inputStream.read(buf))!=-1){
baos.write(buf,0,len);
}
System.out.println(baos.toString());
inputStream.close();
}
}
//服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TestTcpServer1 {
public static void main(String[] args) throws Exception{
//创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//监听客户端的连接
Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
//获取输入流
InputStream is = socket.getInputStream();
//文件输出
//管道输出流
FileOutputStream fos = new FileOutputStream(new File("receive.png"));
byte[] buf = new byte[1024];
int len;
while((len=is.read(buf))!=-1){
fos.write(buf,0,len);
}
//通知客户端我接收完毕了
OutputStream os = socket.getOutputStream();
os.write("我接收完毕了".getBytes());
fos.close();
socket.close();
serverSocket.close();
}
}
==========================================================
//客户端
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TestTpcClient1 {
public static void main(String[] args) throws Exception {
//创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//创建一个输出流
OutputStream os = socket.getOutputStream();
//文件流
FileInputStream fis = new FileInputStream(new File("" +
"C:\\Users\\31016\\Pictures\\Screenshots\\屏幕截图 2023-06-10 194502.png"));
//读取文件
byte[] buf = new byte[1024];
int len;
while((len=fis .read(buf))!=-1){
os.write(buf,0,len);
}
//通知服务器,我已经结束
socket.shutdownOutput();//已将传输完了
//确认服务器接收完毕,才能断开连接
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buf2 = new byte[1024];
int len2;
while((len2=inputStream.read(buf2))!=-1){
byteArrayOutputStream.write(buf2,0,len2);
}
System.out.println(byteArrayOutputStream.toString());
fis.close();
socket.close();
os.close();
}
}
UDP实现发送消息
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//服务端
public class TestUdpServer {
public static void main(String[] args) throws Exception{
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据包
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
socket.receive(packet);//阻塞接受
System.out.println(new String(packet.getData(),0,packet.getLength()));
//关闭连接
socket.close();
}
}
==================================
//客户端
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//不需要连接服务器
public class TestUdpClient {
public static void main(String[] args) throws Exception{
//建立一个Socket
DatagramSocket socket = new DatagramSocket();
//建立一个包
String msg="林夕的花海";
InetAddress inetAddress = InetAddress.getByName("localhost");
int port=9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.getBytes().length, inetAddress, port);
//发送一个包
socket.send(packet);
//关闭包
socket.close();
}
}
两个人都可以是接收方与发送方
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//接收端
public class TestUdpReceive {
public static void main(String[] args) throws Exception{
DatagramSocket socket = new DatagramSocket(6666);
while(true){
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(receiveData.toString());
if (receiveData.equals("bye")){
break;
}
}
socket.close();
}
}
=======================================
//发送端
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TestUdpSender {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
//准备数据,控制台读取 Syste.in
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 (data.equals("bye")){
break;
}
}
socket.close();
}
}
public class TalkSender implements Runnable{
DatagramSocket socket =null;
BufferedReader reader ;
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 (SocketException 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();
}
}
-----------------------------------------------------------------
public class TalkReceive implements Runnable{
DatagramSocket socket ;
private int port;
private String msgForm;
public TalkReceive(int port,String msgForm) {
this.port = port;
this.msgForm=msgForm;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
throw new RuntimeException(e);
}
}
@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);
System.out.println(msgForm+":"+receiveData);
if (receiveData.equals("bye")){
break;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
socket.close();
}
}
---------------------------------------------
public class TalkStudent {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSender(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
--------------------------------------------
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSender(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"老师")).start();
}
}