网络编程
网络编程
概述
地球村
计算机网络
网络编程的目的:交流信息,数据交换
需要:
- 准确定位 网络上的主机 ip+端口
- 如何传输数据
网络通信的要素
实现网络的通信:
-
通信双方地址
- ip
- 端口号
-
规则:网络通信的协议
ip地址
ip地址:InetAddress
-
唯一定位一台网络上计算机
-
127.0.0.1:本机localhost
-
ip地址的分类
-
IPV4/IPV6
- ipv4 127.0.0.1,4个字节组成,2~255,42亿;30亿在北美,亚洲4亿,2011年用尽
- ipv6 16个字节,128位,8个无符号整数~
-
公网(互联网),私网(局域网)
-
ABCD类地址
-
192.168.xx.xx,专门给组织内部使用的
-
-
-
域名:记忆IP问题
- IP:
public static void main(String[] args) throws UnknownHostException {
InetAddress address1 = InetAddress.getByName("www.baidu.com");
InetAddress address2 = InetAddress.getByName("127.0.0.1");
System.out.println(address1);
System.out.println(address2);
}
端口
端口表示计算机的一个程序的进程
-
不同的进程有不同的端口号,用来区分软件
-
被规定0-65535
-
TCP,UDP:65535*2 tcp: udp: 单个协议下,端口号不能冲突,tcp,udp是不同的协议
-
端口分类:
-
公有端口0-1023
- HTTP:80
- HTTPS:443
- FTP:21
- Telnet:23
-
程序注册端口:2014-49151,分配用户或者程序
- Tomcat:8080
- MySQL:3306
- Oracle:1521
-
动态,私有:49152-65535
-
netstat -ano
netstat -ano|findstr "5900"#查看指定的端口
tasklist|findstr "8696" #查看指定端口的进程
InetSocketAddress类
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1",8080);
通信协议
协议:约定
网络通信协议:速率,传输码率,代码结构,传输控制
分层:
TCP/IP协议簇
重要:
- TCP:用户传输协议
- UDP:用户数据报协议
- IP:网络互联协议
TCP
- 连接,稳定
- 三次握手,四次挥手
- 客户端,服务端
- 传输完成,释放连接,效率低
UDP
- 不连接,不稳定
- 客户端,服务端:没有明确的界限
- 不管有没有准备好,都可以发给你
- DDOS:洪水攻击(饱和攻击)
TCP
服务器
- 建立服务的端口ServerSocket
- 监听端口 ,等待用户的连接 accept
- 接收用户的消息
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
//管道流
ByteArrayOutputStream 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());
baos.close();
is.close();
socket.close();
serverSocket.close();
}
客服端
- 连接服务器Socket
- 发送消息
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("nihao ".getBytes());
os.close();
socket.close();
}
TCP文件上传
服务端
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("receive.iml"));
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
客户端
public class TcpClient {
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();
//读取文件
FileInputStream fis = new FileInputStream(new File("JS.iml"));
byte[] buffer = new byte[1024];
int len;
while((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
fis.close();
os.close();
socket.close();
}
}
Tomcat
服务端:
- 自定义S
- Tomcat服务器S
客户端:
- 自定义C
- 浏览器B
UDP消息发送
Datagram Packet
Datagram Socket
服务端
public class UDPServer {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
客户端
public class UDPClient {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String msg = "你好服务器";
InetAddress localhost = InetAddress.getByName("127.0.0.1");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
socket.send(packet);
socket.close();
}
}
UDP聊天实现
public class Chat2 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while(true) {
byte[] contains = new byte[1024];
DatagramPacket packet = new DatagramPacket(contains, 0, contains.length, new InetSocketAddress("localhost", 8888));
socket.receive(packet);
byte[] data = packet.getData();
String datas = new String(data, 0, data.length);
System.out.println(datas);
if (datas.equals("bye")) {
break;
}
}
socket.close();
}
}
public class Chat1 {
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(data.equals("bye")){
break;
}
}
socket.close();
}
}
UDP多线程在线咨询
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TalkReceive implements Runnable {
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TalkReceive(int port, String msgFrom) throws Exception {
this.port = port;
socket = new DatagramSocket(port);
this.msgFrom = msgFrom;
}
@Override
public void run() {
while (true) {
byte[] contains = new byte[1024];
DatagramPacket packet = new DatagramPacket(contains, 0, contains.length, new InetSocketAddress("localhost", 8888));
try {
socket.receive(packet);
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = packet.getData();
String datas = new String(data, 0, data.length);
System.out.println(msgFrom+":"+datas);
if (datas.equals("bye")) {
break;
}
}
socket.close();
}
}
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 Exception {
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 = null;
try {
data = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toPort));
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
if(data.equals("bye")){
break;
}
}
socket.close();
}
}
public class TalkStudent {
public static void main(String[] args) throws Exception {
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
public class TalkTeacher {
public static void main(String[] args) throws Exception {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
URL下载网络资源
统一资源定位符:定位资源上的,定位互联网上的某一个资源
DNS域名解析
协议://ip地址:端口/项目名
public static void main(String[] args) throws Exception {
URL url = new URL("https://cdn.bootcdn.net/ajax/libs/blueimp-md5/2.18.0/js/md5.js");
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println(url.getQuery());
}
下载URL
public static void main(String[] args) throws Exception {
URL url = new URL("https://cdn.bootcdn.net/ajax/libs/blueimp-md5/2.18.0/js/md5.js");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("md5.js");
byte[] bytes = new byte[1024];
int len;
while ((len=inputStream.read(bytes))!=-1) {
fos.write(bytes,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}