在网络通信协议下,不同计算机上运行的程序,可以进行数据传输。
IP地址
端口
协议
IP:全称“互联网协议地址”,也称IP地址。是分配给上网设备的数字标签。常见的IP分类为ipv4和ipv6.
由于互联网的蓬勃发展,IP地址的需求量愈来愈大,而IPv4的模式下IP的总结是有限的。
采用128位地址长度,分成8组。
InetAddress:此类表示Interme协议(IP)地址。
cmd+ipconfig+回车:查询主机IP。
public class InetadressTest{
public static void main (String[] args) throws UnknowHostException{
InetAddress address = InetAddress,getByName("LAPTOP-RPLJ5AUN");
String hostName = address.getHostName();
System.out.println("主机名为:"+hostName);
String ip = address.getHostAddress();
System.out.println("IP为:"+ip);
}
}
运行结果:
主机名为:LAPTOP-RPLJ5AUN
IP为192.168.0.100
端口:应用程序在设备中唯一的标识。
端口号:用两个字节表示的整数,取值范围是0-65535.
其中0-1023之间的端口号用于一些知名的网络服务或者应用。
我们自己使用1024以上的端口号就可以了。
注意:一个端口号只能被一个应用程序使用。
协议:计算机网络中,连接和通信的规则被称为网络通信协议。
网络编程:可以让两台计算机进行数据交互。
网络编程三要素:
IP:设备在网络中唯一的标识。
端口号:应用程序在设备中唯一的标识。
协议:数据在传输的过程中遵守的规则。
UDP发送数据:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* Created by Ajiac on 2020/3/26 16:14
*/
public class Client {
public static void main(String[] args) throws IOException {
//1.创建发送端DatagramSocket对象
DatagramSocket ds = new DatagramSocket();
//2.打包数据
//结构:DatagramPacket (byte[] buf, int length, InetAddress address, int port)
String s = "啥也不是";
byte[] bytes = s.getBytes();
InetAddress address =InetAddress.getByName("127.0.0.1");
int port = 10000;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
//3.调用DatagramSocket对象的方法发送数据
ds.send(dp);
//4.释放资源
ds.close();
}
}
UDP接收数据
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* Created by Ajiac on 2020/3/26 16:30
*/
public class Server {
public static void main(String[] args) throws IOException {
//1.创建接收端DatagramSocket对象------表示接收端从10000端口接收数据
DatagramSocket ds= new DatagramSocket(10000);
//2.创建一个新箱子,接收数据
//结构:DatagramPacket (byte[] buf, int length)
byte[] bytes = new byte[1024];
DatagramPacket dp= new DatagramPacket(bytes,bytes.length);
//3.调用DatagramSocket方法接收数据,将数据放入箱子
ds.receive(dp);
//4.从新的箱子里获取数据
byte[] data = dp.getData();
int length = dp.getLength();
System.out.println(new String(data,0,length));
//5.释放资源
ds.close();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
/**
* Created by Ajiac on 2020/3/26 17:23
*/
public class Client1 {
public static void main(String[] args) throws IOException {
Scanner sn = new Scanner(System.in);
DatagramSocket ds = new DatagramSocket();
while (true){
String s = sn.nextLine();
if ("886".equals(s)){
break;
}
byte[] bytes = s.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 10000;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
ds.send(dp);
}
ds.close();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* Created by Ajiac on 2020/3/26 18:28
*/
public class Server1 {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10000);
while (true){
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
ds.receive(dp);
byte[] data = dp.getData();
int length = dp.getLength();
System.out.println(new String(data,0,length));
}
}
}
运行结果:
在Client1的控制台中键盘输入:
Server1的结果为:
单播发送端:
①创建发送端的Socket对象(DatagramSocket)
②创建数据,并把数据打包(DatagramPacket)
③调用DatagramSocket对象的方法发送数据
【在单播中,这是发给指定ip的电脑,但在组播中,这是发给组播地址。】
【在单播中,这是发给指定ip的电脑,但在广播中,这是发给广播地址。】
④释放资源
单播接收端:
①创建接收端的Socket对象(DatagramSocket)
DatagramSocket ds = new DatagramSocket(10086);
【组播的接收端为 new MulticasSocket(10086);】
②创建一个箱子,用于接收数据(DatagramPacket)
DatagramPacket dp = new DatagramPacket(new byte[1024],1024);
【在②和③之间:把当前电脑添加在组当中】
③调用DatagramSocket对象的方法接收数据,并将数据放入箱子中
ds.receive(dp);
④解析数据包,并把数据在控制台中显示
byte[] data = dp.getData();
System.out.println(new String(data,0,dp.getLength()));
⑤释放资源
UDP通信组播代码实现
组播地址:224.0.0.0~239.255.255.255
其中224.0.0.0~224.0.0.255为预留的组播地址。
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* Created by Ajiac on 2020/3/26 23:07
*/
public class Client2 {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket();
String s = "hello 组播 你好呀";
byte[] bytes = s.getBytes();
InetAddress address = InetAddress.getByName("224.0.1.0");
int port = 10000;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
ds.send(dp);
ds.close();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
* Created by Ajiac on 2020/3/26 23:39
*/
public class Server2 {
public static void main(String[] args) throws IOException {
MulticastSocket ms = new MulticastSocket(10000);
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
//把当前计算机绑定一个组播地址,表示添加在这一组播中
ms.joinGroup(InetAddress.getByName("224.0.1.0"));
ms.receive(dp);
byte[] data = dp.getData();
int length = dp.getLength();
System.out.println(new String(data,0,length));
ms.close();
}
}
运行结果:
UDP通信广播代码实现
广播地址:255.255.255.255
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* Created by Ajiac on 2020/3/27 14:52
*/
public class Client3 {
public static void main(String[] args) throws IOException{
DatagramSocket ds = new DatagramSocket();
String s = "hello 广播 你好呀";
byte[] bytes = s.getBytes();
InetAddress address = InetAddress.getByName("255.255.255.255");
int port = 10000;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
ds.send(dp);
ds.close();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* Created by Ajiac on 2020/3/27 14:57
*/
public class Server3 {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10000);
DatagramPacket dp = new DatagramPacket(new byte[1024],1024);
ds.receive(dp);
byte[] data = dp.getData();
int length = dp.getLength();
System.out.println(new String(data,0,length));
ds.close();
}
}
运行结果:
①创建客户端的Socket对象(Socket)与指定服务端连接
Socket(String host,int port)
②获取输出流,写数据
OutputStream getOutputStream()
③释放资源
void close()
①创建服务器端的Socket对象(ServerSocket)
②监听客户端连接,返回一个Socket对象
Socket accept()
③获取输入流,读数据,并把数据显示在控制台
inputStream getinputStream()
④释放资源
void close()
代码实现:
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
/**
* Created by Ajiac on 2020/3/27 21:49
*/
public class Client4 {
public static void main(String[] args) throws IOException {
//1.创建一个Socket对象
Socket socket = new Socket("127.0.0.1",10001);
//2.获取一个IO流开始写数据
OutputStream os = socket.getOutputStream();
os.write("hello".getBytes());
//3.释放资源
os.close();
socket.close();
}
}
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by Ajiac on 2020/3/27 21:53
*/
public class Server4 {
public static void main(String[] args) throws IOException {
//1.创建Socket对象
ServerSocket ss = new ServerSocket(10001);
//2.等待客户端连接
System.out.println("连接前");
Socket accept = ss.accept();
System.out.println("连接后");
//3.获取输入流对象
InputStream is = accept.getInputStream();
int b;
while ((b = is.read())!=-1){
System.out.print((char)b);
}
//4.释放资源
is.close();
ss.close();
}
}
运行结果:
TCP通信原理:
代码实现:
import java.io.*;
import java.net.Socket;
/**
* Created by Ajiac on 2020/3/27 23:52
*/
public class Client5 {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1",10002);
OutputStream os = socket.getOutputStream();
os.write("hello".getBytes());
//os.close(); 如果在这里关流,会导致整个Socket都无法使用
socket.shutdownOutput();//仅仅关闭输出流,并写一个结束标记,对socket没有任何影响
// InputStream is = socket.getInputStream();
// int b;
// while ((b = is.read())!=-1){
// System.out.println((char)b);
// }
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = br.readLine())!=null){
System.out.println(line);
}
br.close();
os.close();
socket.close();
}
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by Ajiac on 2020/3/27 23:55
*/
public class Server5 {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10002);
Socket accept = ss.accept();
InputStream is = accept.getInputStream();
int b;
while ((b = is.read())!=-1){
System.out.print((char)b);
}
// OutputStream os = accept.getOutputStream();
// os.write("你谁啊?".getBytes());
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
bw.write("你谁啊?");
bw.newLine();
bw.flush();
bw.close();
is.close();
accept.close();
ss.close();
}
}
运行结果:
TCP通信原理:
先准备好文件
代码实现:
import java.io.*;
import java.net.Socket;
/**
* Created by Ajiac on 2020/3/28 12:28
*/
public class Client6 {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1",10005);
//是本地的流,用来读取本地的
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\idea_me\\ClientDir\\1.jpg"));
//写到服务器--网络中的流
OutputStream os = socket.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
int b;
while ((b = bis.read())!= -1){
bos.write(b);//通过网络写到服务器中
}
//给服务器一个结束标志,告诉服务器文件已经传输完毕
socket.shutdownOutput();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = br.readLine())!=null){
System.out.println(line);
}
bis.close();
socket.close();
}
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by Ajiac on 2020/3/28 12:47
*/
public class Server6 {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10005);
Socket accept = ss.accept();
//网络中的流,从客户端读取数据的
BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
//本地的IO流,把数据写到本地中,实现永久化存储
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\idea_me\\ServerDir\\copy.jpg"));
int b;
while ((b = bis.read())!= -1){
bos.write(b);
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
bw.write("上传成功");
bw.newLine();
bw.flush();
bos.close();
accept.close();
ss.close();
}
}
运行结果:
这时我们来查看ServerDir文件夹,
会发现里面多了个copy.jpg
点开会发现,
1.jpg与copy.jpg是相同的两个文件
读取本地的流时,要输入全路径。