在吗?你是GG还是MM?
网络模型一般是指:
我们应该或多或少都有见过IP地址的格式 xxx.xxx.xxx.xxx大致应该是类似这样的,但是计算机不是只能识别二进制的数据,但是很显然,我们的IP地址确实不是二进制的,这是什么原因呢?
我们先随便拿一个IP地址举个例子看看
IP:192.168.1.100
换算:11000000 10101000 00000001 01100100
但是如果我们日后需要用到这个IP地址的时候, 记忆起来就比较麻烦 ,所以,为了方便表示IP地址,我们就 把IP地址的每一个字节上的数据换算成十进制 ,然后用 ' . ' 分开来表示:"点分十进制"
A类:第一号段为网络号段+后三段的主机号段,一个网络号:256 256 256 = 16777216
B类:前二号段为网络号段+后二段的主机号段,一个网络号:256*256 = 65536
C类:前三号段为网络号段+后一段的主机号段,一个网络号:256
1.0.0.1---127.255.255.254 (1)10.X.X.X是私有地址(私有地址就是在互联网上不使用,而被用在局域网络中的地址) (2)127.X.X.X是保留地址,用做循环测试用的
128.0.0.1---191.255.255.254 172.16.0.0---172.31.255.255是私有地址 169.254.X.X是保留地址
192.0.0.1---223.255.255.254 192.168.X.X是私有地址
224.0.0.1---239.255.255.254
240.0.0.1---247.255.255.254
ipconfig 查看本机ip地址
ping 后面跟ip地址, 测试本机与指定的ip地址间的通信是否有问题
127.0.0.1 回环地址(表示本机)//也就是说,ping本机的IP地址相当于ping 127.0.0.1
x.x.x.255 广播地址
x.x.x.0 网络地址
//根据主机名或者IP地址的字符串表示得到IP地址对象 public static InetAddress getByName(String host):
import java.net.InetAddress; import java.net.UnknownHostException; public class InetAddressDemo { public static void main(String[] args) throws UnknownHostException { InetAddress address = InetAddress.getByName("192.168.24.1"); //获取两个东西:主机名,IP地址 String name = address.getHostName(); String ip = address.getHostAddress(); System.out.println(name + "---" + ip); } } //运行结果 LAPTOP-5T03DV1G---192.168.24.1
TCP:传输控制协议,传输效率低,可靠性强,用于传输可靠性要求高,数据量大的数据
UDP:用户数据报协议,与TCP特性恰恰相反,用于传输可靠性要求不高,数据量小的数据,如QQ微信聊天数据就是通过这种方式传输的
import java.io.IOException; import java.net.*; /* UDP协议发送数据: * A:创建发送端Socket对象 * B:创建数据,并把数据打包 * C:调用Socket对象的发送方法发送数据包 * D:释放资源 */ public class SendDemo { public static void main(String[] args) throws IOException { //创建socket对象 DatagramSocket ds = new DatagramSocket(); //创建数据,并把数据打包 //DatagramPacket(byte[] buf, int length, InetAddress address, int port) byte[] bys = "Hello,BWH!".getBytes();//把字符串转换成字符数组 int length = bys.length; InetAddress address = InetAddress.getByName("192.168.24.1"); int port = 10086; //自拟 DatagramPacket dp = new DatagramPacket(bys, length, address, port); //调用Socket对象的方法发送数据包 //public void send(DatagramPacket p) ds.send(dp); //释放资源 ds.close(); //底层依赖IO流,所以要释放资源 } }
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; /* * UDP协议接收数据: * A:创建接收端Socket对象 * B:创建一个数据包(接收容器) * C:调用Socket对象的接收方法接收数据 * D:解析数据包,并显示在控制台 * E:释放资源 */ public class ReceiveDemo { public static void main(String[] args) throws IOException { // 创建接收端Socket对象 // DatagramSocket(int port) DatagramSocket ds = new DatagramSocket(10086); // 创建一个数据包(接收容器) // DatagramPacket(byte[] buf, int length) byte[] bys = new byte[1024]; int length = bys.length; DatagramPacket dp = new DatagramPacket(bys, length); // 调用Socket对象的接收方法接收数据 // public void receive(DatagramPacket p) ds.receive(dp); // 解析数据包,并显示在控制台 // 获取对方的ip // public InetAddress getAddress() InetAddress address = dp.getAddress(); String ip = address.getHostAddress(); // public byte[] getData():获取数据缓冲区 // public int getLength():获取数据的实际长度 byte[] bys2 = dp.getData(); int len = dp.getLength(); String s = new String(bys2, 0, len); System.out.println(ip + ": " + s); // 释放资源 ds.close(); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class SendDemo { public static void main(String[] args) throws IOException { //创建发送端的Socket对象 DatagramSocket ds = new DatagramSocket(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; while ((line = br.readLine()) != null) { if ("886".equals(line)) { break; } //创建数据并打包 byte[] bys = line.getBytes(); DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.24.1"), 10086); //发送数据 ds.send(dp); } //释放资源 ds.close(); } }
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class ReceiveDemo { public static void main(String[] args) throws IOException { //创建接受端的Socket对象 DatagramSocket ds = new DatagramSocket(10086); while (true) { //创建一个包裹 byte[] bys = new byte[1024]; DatagramPacket dp = new DatagramPacket(bys, bys.length); //接收数据 ds.receive(dp); //解析数据 String ip = dp.getAddress().getHostAddress(); String s = new String(dp.getData(), 0, dp.getLength()); System.out.println(ip + ": " + s); } // 释放资源,但是接收端是服务器应该一直开启 //ds.close(); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class SendThread implements Runnable { private DatagramSocket ds; public SendThread(DatagramSocket ds) { this.ds = ds; } @Override public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; while ((line = br.readLine()) != null) { if ("886".equals(line)) { break; } // 创建数据并打包 byte[] bys = line.getBytes(); DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.24.1"), 10086); // 发送数据 ds.send(dp); } // 释放资源 ds.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class ReceiveThread implements Runnable { private DatagramSocket ds; public ReceiveThread(DatagramSocket ds) { this.ds = ds; } @Override public void run() { try { while (true) { //创建一个包裹 byte[] bys = new byte[1024]; DatagramPacket dp = new DatagramPacket(bys, bys.length); //接收数据 ds.receive(dp); //解析数据 String ip = dp.getAddress().getHostAddress(); String s = new String(dp.getData(), 0, dp.getLength()); System.out.println("from " + ip + " data is : " + s); } } catch (IOException e) { e.printStackTrace(); } } }
import java.net.DatagramSocket; import java.net.SocketException; public class ChatRoom { public static void main(String[] args) throws SocketException { DatagramSocket dsSend = new DatagramSocket(); DatagramSocket dsReceive = new DatagramSocket(10086); SendThread st = new SendThread(dsSend); ReceiveThread rt = new ReceiveThread(dsReceive); Thread t1 = new Thread(st); Thread t2 = new Thread(rt); t1.start(); t2.start(); } }
package cn.bwh_06_TCP2; import java.io.*; import java.net.Socket; public class Clietn { public static void main(String[] args) throws IOException { Socket s = new Socket("192.168.24.1", 22222); //键盘录入对象 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //把通道内的流包装一下 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { if ("886".equals(line)) { break; } bw.write(line); bw.newLine(); bw.flush(); } s.close(); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(22222); Socket s = ss.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } s.close(); } }
//封装通道内的数据 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); //封装文本文件 BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
//封装文本文件 BufferedReader br = new BufferedReader(new FileReader("Demo.java")); //封装通道内的数据 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); }
如果内容中有什么不足,或者错误的地方,欢迎大家给我留言提出意见, 蟹蟹大家 !^_^
如果能帮到你的话,那就来关注我吧!(系列文章均会在公众号第一时间更新)
一个坚持推送原创Java技术的公众号:理想二旬不止