计算机网络
网络编程
IP地址
端口
协议
为了网编我们对IP地址的获取和操作,Java提供了一个雷InetAddress供我们使用
InetAddress:此类表示Internet协议(IP)地址
方法名 | 说明 |
---|---|
static InetAddress getByName(String host) | 确定主机名称的IP地址。主机名称可以是机器名称,也可以是IP地址 |
String getHostName() | 获取此IP地址的主机名 |
String getHostAddress() | 返回文本显示中的IP地址字符串 |
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressDemo {
public static void main(String[] args) throws UnknownHostException {
InetAddress address = InetAddress.getByName("DESKTOP-K84PCFO");
String hostName = address.getHostName();
String hostAddress = address.getHostAddress();
System.out.println(hostName);
System.out.println(hostAddress);
System.out.println(address);
}
}
协议:计算机网络中,连接和通信的规则被称为网络通信协议
UDP协议
TCP协议
传输控制协议(Transmission Control Protocol)
TCP协议是面向连接的通信协议,即传输数据之前,在发送端和接收端建立逻辑连接,然后再传输数据,它提供了两台计算机之间可靠无差错的数据传输。在TCP连接中必须要明确客户端与服务器端,由客户端向服务器发出连接请求,每次连接 的创建都需要经过“三次握手”
三次握手:TCP协议中,在发送数据的准备阶段,客户端与服务器之间的三次交互,以保证连接的可靠
完成三次握手,连接连接建立后,客户端和服务器就可以开始进行数据传输了。由于这种面向连接的特性,TCP协议可以保证传输数据的安全,所以应用十分广泛。例如上传文件、下载文件、浏览网页等
UDP协议是一种不可靠的网络协议,它在通信的两端各建立一个Socket对象,但是这两个Socket只是发送,接收数据的对象
因此对于基于UDP协议的通信双方而言,没有所谓的客户端和服务器的概念
Java提供了DatagramSocket类作为基于UDP协议的Socket
发送数据的步骤
import java.io.IOException;
import java.net.*;
public class SendDemo {
public static void main(String[] args) throws IOException {
//创建发送端的Socket对象(DatagramSocket)
//DatagramSocket() 构造数据报套接字并将其绑定到本地主机上的任何可用端口
DatagramSocket ds = new DatagramSocket();
//创建数据,并把数据打包
byte[] bys = "hello,udp,我来了".getBytes();
int length = bys.length;
InetAddress byName = InetAddress.getByName("192.168.1.66");
int port = 10086;
DatagramPacket dp = new DatagramPacket(bys,length,byName,port);
//调用DatagramSocket对象的方法发送数据
ds.send(dp);
//关闭发送端
ds.close();
}
}
接收数据的步骤
import java.io.IOException;
import java.net.*;
public class SendDemo {
public static void main(String[] args) throws IOException {
//创建发送端的Socket对象(DatagramSocket)
//DatagramSocket() 构造数据报套接字并将其绑定到本地主机上的任何可用端口
DatagramSocket ds = new DatagramSocket();
//创建数据,并把数据打包
byte[] bys = "hello,udp,我来了".getBytes();
int length = bys.length;
InetAddress byName = InetAddress.getByName("127.0.0.1");
int port = 10086;
DatagramPacket dp = new DatagramPacket(bys,length,byName,port);
//调用DatagramSocket对象的方法发送数据
ds.send(dp);
//关闭发送端
ds.close();
}
}
按照下面的要求实现程序
发送端:SendDemo.java
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 {
DatagramSocket ds = new DatagramSocket();
//自己封装一个键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine())!=null){
//输入的是886,结束
if ("886".equals(line)){
break;
}
//创建数据,并打包
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("127.0.0.1"), 10086);
ds.send(dp);
}
ds.close();
}
}
接收端:ReceiveDemo.java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//接收端
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10086);
while (true){
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
ds.receive(dp);
System.out.println("数据是:"+ new String(dp.getData(),0,dp.getLength()));
}
}
}
TCP通信协议是一种可靠地网络协议,它在通信的两端各建立一个Socket对象,从而在通信的两端形成网络虚拟链路,一点建立了虚拟的网络链路,两端的程序就可以通过虚拟链路进行通信
Java对于基于TCP协议的网络提供了良好的封装,使用Socket对象来代表两端的通信端口,并通过Socket产生IO流来进行网络通信
Java为客户端提供了Socket类,为服务器端提供了ServerSocket类
发送数据的步骤
客户端
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
//发送数据
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建Socket对象
// Socket s = new Socket(InetAddress.getByName("127.0.0.1"),10086);
Socket s = new Socket("127.0.0.1",10086);
//获取输出流,写数据
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());
//释放资源
s.close();
}
}
接受数据的步骤
服务端
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器端的Socket对象(ServerSocket)
ServerSocket ss = new ServerSocket(10086);
Socket s = ss.accept();
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = 0;
while ((len=is.read(bys))!=-1){
System.out.println("数据是:"+ new String(bys,0,len));
}
//释放资源
s.close();
ss.close();
}
}
客户端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket)
Socket s = new Socket("127.0.0.1", 10001);
//获取输出流写数据
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());
//接收服务器反馈
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
// while ((len = is.read(bys)) != -1) {
System.out.println("客户端:" + new String(bys, 0, len));
// }
//释放资源
s.close();
}
}
服务端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器端的Socket对象(ServerSocket)
ServerSocket ss = new ServerSocket(10001);
//监听客户端连接,返回一个Socket对象
Socket s = ss.accept();
//获取输入流,读数据,并把数据显示在控制台
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
// while ((len = is.read(bys)) != -1) {
System.out.println("服务器:" + new String(bys, 0, len));
// }
//给出反馈
OutputStream os = s.getOutputStream();
os.write("数据已经收到".getBytes());
//释放资源
ss.close();
}
}
客户端
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket)
Socket s = new Socket("127.0.0.1", 10001);
//数据来自于键盘录入,知道是886,发送数据结束
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//封装输出流对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line = br.readLine())!=null){
if ("886".equals(line)){
break;
}
//获取输出流对象
// OutputStream os = s.getOutputStream();
// os.write(line.getBytes());
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
s.close();
}
}
服务器
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器端的Socket对象(ServerSocket)
ServerSocket ss = new ServerSocket(10001);
//监听客户端连接,返回一个Socket对象
Socket s = ss.accept();
//获取输入流,读数据,并把数据显示在控制台
// InputStream is = s.getInputStream();
// InputStreamReader isr = new InputStreamReader(is);
// BufferedReader bw = new BufferedReader(isr);
BufferedReader bw = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
while ((line = bw.readLine()) != null) {
System.out.println("服务器:" + line);
}
//释放资源
ss.close();
}
}
客户端
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket)
Socket s = new Socket("127.0.0.1", 10001);
//数据来自于键盘录入,知道是886,发送数据结束
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//封装输出流对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line = br.readLine())!=null){
if ("886".equals(line)){
break;
}
//获取输出流对象
// OutputStream os = s.getOutputStream();
// os.write(line.getBytes());
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
s.close();
}
}
服务器
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器端的Socket对象(ServerSocket)
ServerSocket ss = new ServerSocket(10001);
//监听客户端连接,返回一个Socket对象
Socket s = ss.accept();
//获取输入流,读数据,并把数据显示在控制台
// InputStream is = s.getInputStream();
// InputStreamReader isr = new InputStreamReader(is);
// BufferedReader bw = new BufferedReader(isr);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw = new BufferedWriter(new FileWriter("tcp.txt"));
String line=null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
ss.close();
bw.close();
}
}
客户端
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket)
Socket s = new Socket("127.0.0.1", 10001);
//数据来自于文件
BufferedReader br = new BufferedReader(new FileReader("fw.txt"));
//封装输出流对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line = br.readLine())!=null){
//获取输出流对象
// OutputStream os = s.getOutputStream();
// os.write(line.getBytes());
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
s.close();
br.close();
}
}
服务器
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器端的Socket对象(ServerSocket)
ServerSocket ss = new ServerSocket(10001);
//监听客户端连接,返回一个Socket对象
Socket s = ss.accept();
//获取输入流,读数据,并把数据显示在控制台
// InputStream is = s.getInputStream();
// InputStreamReader isr = new InputStreamReader(is);
// BufferedReader bw = new BufferedReader(isr);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw = new BufferedWriter(new FileWriter("tcp.txt"));
String line=null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
ss.close();
bw.close();
}
}
客户端
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket)
Socket s = new Socket("127.0.0.1", 10001);
//数据来自于文件
BufferedReader br = new BufferedReader(new FileReader("fw.txt"));
//封装输出流对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line = br.readLine()) != null) {
//获取输出流对象
// OutputStream os = s.getOutputStream();
// os.write(line.getBytes());
bw.write(line);
bw.newLine();
bw.flush();
}
//自定义结束标记
// bw.write("886");
// bw.newLine();
// bw.flush();
//结束标记
s.shutdownOutput();
//接收反馈
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String data = brClient.readLine();//等待读取数据
System.out.println("服务器反馈" + data);
//释放资源
s.close();
br.close();
brClient.close();
}
}
服务器
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器端的Socket对象(ServerSocket)
ServerSocket ss = new ServerSocket(10001);
//监听客户端连接,返回一个Socket对象
Socket s = ss.accept();
//获取输入流,读数据,并把数据显示在控制台
// InputStream is = s.getInputStream();
// InputStreamReader isr = new InputStreamReader(is);
// BufferedReader bw = new BufferedReader(isr);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw = new BufferedWriter(new FileWriter("tcp.txt"));
String line=null;
while ((line = br.readLine()) != null) {//等待读取数据
// if ("886".equals(line)){
// break;
// }
bw.write(line);
bw.newLine();
bw.flush();
}
BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上传成功");
bwServer.newLine();
bwServer.flush();
//释放资源
ss.close();
bw.close();
bwServer.close();
}
}
客户端
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket)
Socket s = new Socket("127.0.0.1", 10001);
//数据来自于文件
BufferedReader br = new BufferedReader(new FileReader("fw.txt"));
//封装输出流对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line = br.readLine()) != null) {
//获取输出流对象
// OutputStream os = s.getOutputStream();
// os.write(line.getBytes());
bw.write(line);
bw.newLine();
bw.flush();
}
//自定义结束标记
// bw.write("886");
// bw.newLine();
// bw.flush();
//结束标记
s.shutdownOutput();
//接收反馈
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String data = brClient.readLine();//等待读取数据
System.out.println("服务器反馈" + data);
//释放资源
s.close();
br.close();
brClient.close();
}
}
服务器
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器端的Socket对象(ServerSocket)
ServerSocket ss = new ServerSocket(10001);
while (true){
//监听客户端连接,返回一个Socket对象
Socket s = ss.accept();
//为每一个客户端开启一个线程
new Thread(new ServerThread(s)).start();
}
}
}
class ServerThread implements Runnable{
private Socket s;
public ServerThread(){}
public ServerThread(Socket s) {
this.s = s;
}
@Override
public void run() {
//接收数据写到文本文件
try {
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
// BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.txt"));
//解决名称冲突问题
int count = 0;
File file = new File("Copy[" + count + "].txt");
if (file.exists()){
count++;
file = new File("Copy[" + count + "].txt");
}
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
String line;
while ((line = br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
//给出反馈
BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上传成功");
bwServer.newLine();
bwServer.flush();
//释放资源
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}