IP
ip地址:InetAddress(java类)
- 唯一定位一台网络上计算机
- 127.0.0.1:本机localhost
- IP地址的分类
- ipv4、ipv6
- IPV4 127.0.0.1,4个字节组成。0~255,42亿;30亿在北美。4亿在亚洲。2011年用尽
- IPV6 128位,8个无符号整数
- 公网(互联网)-私网(局域网)
- ABCD类地址
- 192.168.xx.xx,专门给组织内部使用
端口
端口表示计算机上的一个进程;
通信协议
网络通信协议:速率,传输码率,代码结构,传输控制……
TCP/IP协议簇:实际上是一组协议
重要:
出名的歇息:
TCP于UDP对比
TCP:打电话
- 连接,稳定
- 三次握手,四次挥手
- 三次挥手:A:你愁啥!B:瞅你咋滴 A:打一架
- 四次挥手:A:分开吧 B:等我收拾收拾东西 B:我收拾完了,走了 A:好 拜拜
- 客户端、服务端
- 传输完成,释放连接,效率低
UDP:发短信
- 不连接,不稳定
- 客户端服务端:没有明确的界限
- 不管有没有准备好,都可以发给你
TCP
客户端
连接服务器通过socket
发送消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.bestrookie.lesson02; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket;
public class TcpClientDemo { public static void main(String[] args) throws IOException { InetAddress serverIp = InetAddress.getByName("127.0.0.1"); int port = 9999; Socket socket = new Socket(serverIp,port); OutputStream os = socket.getOutputStream(); os.write("这就是网络".getBytes()); os.close(); socket.close();
} }
|
服务器
简历服务的端口 serverSocket
等待连接 accept
接受用的消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.bestrookie.lesson02; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket;
public class TcpServerDemo { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(9999); Socket socket; InputStream is; ByteArrayOutputStream baos; while (true){ socket = serverSocket.accept(); 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()); } } }
|
UDP
发短信:不用连接,需要知道对方的地址
发送消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.bestrookie.lesson03; import java.io.IOException; import java.net.*; import java.nio.charset.StandardCharsets;
public class UdpClientDemo { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(); String msg = "udp test"; InetAddress localhost = InetAddress.getByName("localhost"); int port = 9090; DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(), 0, msg.length(),localhost,port); socket.send(datagramPacket); socket.close(); } }
|
接受消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.bestrookie.lesson03; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket;
public class UdpServerDemo { public static void main(String[] args) throws IOException { 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.getSocketAddress()); System.out.println(new String(packet.getData(),0,packet.getLength())); } }
|
循环发送消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.bestrookie.chat;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.*;
public class UdpSenderDemo { public static void main(String[] args) throws IOException { 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(); } }
|
循环接受消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.bestrookie.chat; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket;
public class UdpReceiveDemo { public static void main(String[] args) throws IOException { 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,packet.getLength()); System.out.println(receiveData); if ("bye".equals(receiveData)){ break; }
} socket.close(); } }
|
在线聊天小例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package com.bestrookie.chat;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketException;
public class TalkSend implements Runnable{ DatagramSocket socket = null; BufferedReader reader = null; private int fromPort; private String toIp; private int toPort;
public TalkSend(int fromPort, String toIp, int toPort) throws SocketException { this.fromPort = fromPort; this.toIp = toIp; this.toPort = toPort; socket = new DatagramSocket(fromPort); reader = new BufferedReader(new InputStreamReader(System.in)); }
@Override public void run() { while (true){ String data; try { 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 ("bye".equals(data)){ break; } } catch (IOException e) { e.printStackTrace(); } } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.bestrookie.chat;
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException;
public class TalkReceive implements Runnable{ DatagramSocket socket ; private int port; private String msg;
public TalkReceive(int port,String msg) throws SocketException { this.port = port; this.msg = msg; socket = new DatagramSocket(port); }
@Override public void run() { while (true){ byte[] container = new byte[1024]; DatagramPacket packet = new DatagramPacket(container,0,container.length ); try { socket.receive(packet); } catch (IOException e) { e.printStackTrace(); } byte[] data = packet.getData(); String receiveData = new String(data,0,packet.getLength()); System.out.println(msg + receiveData); if ("bye".equals(receiveData)){ break; }
} socket.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.bestrookie.chat;
import java.net.SocketException;
public class TalkStudent { public static void main(String[] args) throws SocketException { new Thread(new TalkSend(7777,"localhost",9999)).start(); new Thread(new TalkReceive(8888,"学生")).start(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.bestrookie.chat;
import java.net.SocketException;
public class TalkTeacher { public static void main(String[] args) throws SocketException { new Thread(new TalkSend(5555,"localhost",8888)).start(); new Thread(new TalkReceive(9999,"学生")).start(); } }
|
Url
从下载地址,解析下载文件小demo,类似于爬虫
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.bestrookie.lesson04;
import javax.net.ssl.HttpsURLConnection; import java.io.*; import java.net.URL;
public class UrlDemo { public static void main(String[] args) throws IOException { URL url = new URL("https://www.baidu.com"); HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); File file; InputStream inputStream = urlConnection.getInputStream(); FileOutputStream fos = new FileOutputStream("文件名称"); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1){ fos.write(buffer,0,len); } fos.close(); inputStream.close(); urlConnection.disconnect(); } }
|