一、基础
二、IP
获取IP地址:InetAddress类
package com.bravo.demo.network;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInetAddress {
public static void main(String[] args) {
try {
InetAddress address1 = InetAddress.getByName("127.0.0.1");
InetAddress address2 = InetAddress.getByName("localhost");
InetAddress address3 = InetAddress.getLocalHost();
System.out.println("address1:"+address1);
System.out.println("address2:"+address2);
System.out.println("address3:"+address3);
InetAddress address4 = InetAddress.getByName("www.baidu.com");
System.out.println("address4:"+address4);
System.out.println("---------------------------");
System.out.println(address4.getAddress());
System.out.println(address4.getCanonicalHostName());
System.out.println(address4.getHostAddress());
System.out.println(address4.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
控制台输出结果:
address1:/127.0.0.1
address2:localhost/127.0.0.1
address3:LAPTOP-NIVLBH1R/192.168.0.7
address4:www.baidu.com/110.242.68.3
---------------------------
[B@7530d0a
110.242.68.3
110.242.68.3
www.baidu.com
三、端口
端口是指计算机上一个程序的进程:
package com.bravo.demo.network;
import java.net.InetSocketAddress;
public class TestInetSocketAddress {
public static void main(String[] args) {
InetSocketAddress address1 = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress address2 = new InetSocketAddress("localhost", 8080);
System.out.println("address1:"+address1);
System.out.println("address2:"+address2);
System.out.println(address2.getHostName());
System.out.println(address2.getHostString());
System.out.println(address2.getPort());
}
}
控制台输出结果:
address1:/127.0.0.1:8080
address2:localhost/127.0.0.1:8080
localhost
localhost
8080
四、通信协议
TCP/IP协议是网络相关各类协议簇的总称,包括TCP,UDP,IP,FTP,HTTP,ICMP,SMTP等
TCP:用户传输协议
UDP:用户数据报协议
IP:网络互联协议
区别点 | TCP | UDP |
---|
是否连接 | 面向连接 | 无连接 | 是否可靠 | 可靠传输,使用流量控制和拥塞控制 | 不可靠传输,不使用流量控制和拥塞控制 | 连接对象个数 | 只能是一对一通信 | 支持一对一,一对多,多对一和多对多交互通信 | 传输方式 | 面向字节流 | 面向报文 | 首部开销 | 首部最小20字节,最大60字节 | 首部开销小,仅8字节 | 适用场景 | 适用于要求可靠传输的应用,例如文件传输 | 适用于实时应用(IP电话、视频会议、直播等) |
TCP可理解为打电话(需要双方连接),UDP可理解为发短信(只需要一方发送,对方是否接受成功不知道)
五、TCP实现会话
客户端:1. 连接服务端Socket;2. 发送消息
package com.bravo.demo.network;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo1 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port=9999;
socket = new Socket(serverIP,port);
os = socket.getOutputStream();
os.write("Hello,world!".getBytes());
} catch (Exception 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();
}
}
}
}
}
服务端:1. 建立服务端口ServerSocket;2. 等待客户端连接accept;3. 接受客户端消息
package com.bravo.demo.network;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo1 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
serverSocket = new ServerSocket(9999);
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());
} 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();
}
}
}
}
}
先启动服务端,再启动客户端,服务端就会收到一条“Hello,world!”的消息
六、TCP实现文件传输
package com.bravo.demo.network;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo2 {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
OutputStream os = socket.getOutputStream();
FileInputStream fis=new FileInputStream(new File("1.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
socket.shutdownOutput();
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[2014];
int len2;
while((len2=is.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
fis.close();
os.close();
socket.close();
}
}
package com.bravo.demo.network;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo2 {
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.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
OutputStream os = socket.getOutputStream();
os.write("服务端接受完毕了,客户端可以断开连接了".getBytes());
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
先启动服务端,再启动客户端,客户端会输出“服务端接受完毕了,客户端可以断开连接了”,项目根目录出现一张名为receive.jpg的图片
七、UDP发送消息
-
发送端: package com.bravo.demo.network;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* UDP发送端
*/
public class UdpClientDemo1 {
public static void main(String[] args) throws Exception{
//1.建立一个Socket
DatagramSocket socket = new DatagramSocket();
//2.建立包
String msg="你好!";
InetAddress localhost = InetAddress.getByName("localhost");
int port=9090;
//DatagramPacket构造参数分别代表发送的数据、数据起始、终止、接受IP、接受端口
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
//3.发送包
socket.send(packet);
//4.关闭资源
socket.close();
}
}
-
接收端: package com.bravo.demo.network;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpServerDemo1 {
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(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
UDP不需要在发送端和接收端直接建立连接
八、UDP循环发送消息
-
发送端 package com.bravo.demo.network;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpSendDemo1 {
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, InetAddress.getByName("127.0.0.1"), 6666);
socket.send(packet);
if("bye".equals(data)){
break;
}
}
socket.close();
}
}
-
接收端 package com.bravo.demo.network;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceiveDemo1 {
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.trim())){
break;
}
}
socket.close();
}
}
九、UDP多线程相互发送消息
-
发送端线程 package com.bravo.demo.network;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
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) {
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, InetAddress.getByName(this.toIP), this.toPort);
socket.send(packet);
if("bye".equals(data)){
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
socket.close();
}
}
-
接收端线程 package com.bravo.demo.network;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TalkReceive(int port,String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(port);
} catch (SocketException 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.trim())){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
-
开启两个线程 package com.bravo.demo.network;
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
package com.bravo.demo.network;
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
十、URL
URL(统一资源定位符):定位互联网上的某一个资源
URL常见格式:协议名://IP地址:端口号/项目名/资源
DNS域名解析:将域名解析为IP地址
package com.bravo.demo.network;
import java.net.MalformedURLException;
import java.net.URL;
public class URLDemo1 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=admin&password=111111");
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());
}
}
输出结果:
http
localhost
8080
/helloworld/index.jsp
/helloworld/index.jsp?username=admin&password=111111
username=admin&password=111111
package com.bravo.demo.network;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class URLDemo2 {
public static void main(String[] args) throws Exception {
URL url = new URL("http://music.163.com/song/media/outer/url?id=853708.mp3");
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
FileOutputStream fos =new FileOutputStream("download.mp3");
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,buffer.length);
}
System.out.println("下载资源成功!");
}
}
|