文章目录
- 网络编程
- (一)网络编程入门
- 1.网络编程概述
- 2.网络编程三要素
- 3.IP地址
- 4.InetAddress的使用
- 5.端口
- 6.协议
- (二)UDP通信程序
- 1.UDP通信原理
- 2.UDP发送数据
- 3.UDP接收数据
- 4.案例(聊天室)
- (三)TCP通信程序
- 1.TCP通信原理
- 2.TCP发送数据
- 3.TCP接收数据
- (四)TCP通信练习
- 练习一:
- 练习二:
- 练习三:
- 练习四:
- 练习五:
- 练习六:
- (一)网络编程入门
网络编程
(一)网络编程入门
1.网络编程概述
网络编程
- 在网络通信协议下,实现网路互连的不同计算机上运行的程序间可以进行数据交换
2.网络编程三要素
3.IP地址
常用命令:
- ipconfig:查看本机IP地址
- ping IP地址:检查网路是否连通
特殊IP地址:
- 127.0.0.1:是回送地址,可以代表本机地址,一般用来测试使用
4.InetAddress的使用
代码演示:
代码语言:javascript复制import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressDemo {
public static void main(String[] args) throws UnknownHostException {
// InetAddress address = InetAddress.getByName("PC-202111286656");
InetAddress address = InetAddress.getByName("192.168.1.105");
String name = address.getHostName();
String ip = address.getHostAddress();
System.out.println(name);//PC-202111286656
System.out.println(ip);//192.168.1.105
}
}
5.端口
6.协议
(二)UDP通信程序
1.UDP通信原理
UDP协议是一种不可靠的网路协议,它在通信的两端各建立一个Socket对象,但是这两个Socket只是发送,接收数据的对象 因此对于基于UDP协议的通信双方而言,没有所谓的客户端和服务器的概念
Java提供了DatagramSocket类作为UDP协议的Socket
2.UDP发送数据
代码演示:
代码语言:javascript复制import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPDemo {
public static void main(String[] args) throws IOException {
//DatagramSocket() 构建一个数据报套接字绑定到本地主机的任何可用的端口。
DatagramSocket ds = new DatagramSocket();
byte[] bys = "hello java".getBytes();
DatagramPacket dp = new DatagramPacket(bys,bys.length, InetAddress.getByName("192.168.1.105"),10086);
ds.send(dp);
ds.close();
}
}
3.UDP接收数据
代码演示:
代码语言:javascript复制import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10086);
//创建数据包,用于接收数据
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
//接收数据
ds.receive(dp);
//解析数据
byte[] datas = dp.getData();
int len = dp.getLength();
System.out.println("数据是:" new String(datas, 0, len)); //数据是:hello java
//注意接收数据,先运行接收端,再运行发送端
//关闭接收端
ds.close();
}
}
4.案例(聊天室)
定义发送端:
代码语言:javascript复制import java.io.IOException;
import java.net.*;
import java.util.Scanner;
public class SendDemo {
public static void main(String[] args) throws IOException {
//创建发送端Socket对象
DatagramSocket ds = new DatagramSocket();
Scanner sc = new Scanner(System.in);
System.out.println("请输入传输的数据:");
String ss = "";
while (!ss.equals("886")) {
ss = sc.nextLine();
//创建数据包
byte[] bys = ss.getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.1.105"), 10086);
//发送数据
ds.send(dp);
}
ds.close();
}
}
定义接收端:
代码语言:javascript复制import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class Receive {
public static void main(String[] args) throws IOException {
//创建接收端对象
DatagramSocket ds = new DatagramSocket(10086);
//创建数据包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
while(true){
//接收数据
ds.receive(dp);
//解析数据
byte[] data = dp.getData();
int length = dp.getLength();
System.out.println("数据是:" new String(data,0,length));
}
}
}
(三)TCP通信程序
1.TCP通信原理
2.TCP发送数据
发送数据的步骤
1.创建客户端的Socket对象(Socket)
代码语言:javascript复制Socket(String host,int port)
2.获取输出流,写数据
代码语言:javascript复制OutputStream getOutputStream()
3.释放资源
代码语言:javascript复制void close()
代码演示:
代码语言:javascript复制import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TCP_sendDemo {
public static void main(String[] args) throws IOException {
//Socket(InetAddress address, int port)
// Socket s = new Socket(InetAddress.getByName("192.168.1.105"),10000);
//创建客户端对象 Socket(String host,int port)
Socket s =new Socket("192.168.1.105",8888);
//获取输出流写数据
OutputStream os = s.getOutputStream();
os.write("TCP我来了".getBytes());
//释放资源
s.close();
}
}
3.TCP接收数据
接收数据的步骤
1.创建服务器端的Socket对象(ServerSocket)
代码语言:javascript复制ServerSocket(int port)
2.监听客户端连接,返回一个Socket对象
代码语言:javascript复制Socket accept()
3.获取输入流,读数据,并把数据显示在控制台
代码语言:javascript复制InputStream getInputStream()
4.释放资源
代码语言:javascript复制void close()
代码演示:
代码语言:javascript复制import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCP_acceptDemo {
public static void main(String[] args) throws IOException {
//创建服务端对象
ServerSocket ss = new ServerSocket(8888);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//获取输入流,读数据,并把数据显示在控制台
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
System.out.println("数据是:" new String(bys, 0, len));
//释放资源
ss.close();
}
}
(四)TCP通信练习
练习一:
- 客户端:发送数据,接收服务器反馈
- 服务器:接受数据,给出反馈
代码实现:
客户端:
代码语言:javascript复制import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端对象
Socket s = new Socket(InetAddress.getByName("192.168.1.105"), 8888);
//获取输出流,写数据
OutputStream os = s.getOutputStream();
os.write("服务端我来了".getBytes());
//接收服务端反馈
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String data = new String(bys, 0, len);
System.out.println("服务端:" data);//服务端:数据收到了
//释放资源
s.close();
}
}
服务器:
代码语言:javascript复制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 {
//创建服务端对象
ServerSocket ss = new ServerSocket(8888);
//监听客户端连接
Socket s = ss.accept();
//获取输入流对象,读数据
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String data = new String(bys, 0, len);
System.out.println("数据是:" data);//数据是:服务端我来了
//给客户端反馈
OutputStream os = s.getOutputStream();
os.write("数据收到了".getBytes());
//释放资源
ss.close();
}
}
练习二:
- 客户端:数据来自键盘录入,直到输入的数据是886,发送数据结束
- 服务器:接收到的数据在控制台输出
代码实现:
客户端:
代码语言:javascript复制import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class ClientDemo2 {
public static void main(String[] args) throws IOException {
//创建客户端对象
Socket s = new Socket(InetAddress.getByName("192.168.1.105"), 8888);
//键盘录入
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//封装字符输出流对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
//服务端反馈
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String ss = new String(bys, 0, len);
System.out.println("服务端:" ss);
if (line.equals("886")) {
break;
}
}
//释放资源
s.close();
}
}
服务器:
代码语言:javascript复制import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo2 {
public static void main(String[] args) throws IOException {
//创建服务端对象
ServerSocket ss = new ServerSocket(8888);
//监听客户端连接
Socket s = ss.accept();
//封装字符输入流,读取数据
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println("数据是:" line);
//给客户端反馈
OutputStream os = s.getOutputStream();
os.write("服务端收到了数据".getBytes());
}
//释放资源
ss.close();
}
}
练习三:
- 客户端:数据来自键盘录入,直到输入的数据是886,结束数据发送
- 服务器:接收到的数据写入文本文件
客户端:
代码语言:javascript复制import java.io.*;
import java.net.Socket;
public class ClientDemo3 {
public static void main(String[] args) throws IOException {
//创建客户端对象
Socket s = new Socket("192.168.1.105", 8888);
//封装字符输入流对象,键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//获取输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.equals("886")) {
break;
}
bw.write(line);
bw.newLine();
bw.flush();
//服务端给出反馈
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String ss = new String(bys, 0, len);
System.out.println("服务端:" ss);
}
//释放资源
s.close();
}
}
服务器:
代码语言:javascript复制import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo3 {
public static void main(String[] args) throws IOException {
//创建服务端对象
ServerSocket ss = new ServerSocket(8888);
//监听客户端连接
Socket s = ss.accept();
//封装字符输入流对象,读取数据
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//创建把数据写入文本
BufferedWriter bw = new BufferedWriter(new FileWriter("src\TEMP\TEMP29\sd3.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
//给客户端反馈
OutputStream os = s.getOutputStream();
os.write("数据已写入文本中".getBytes());
}
//释放资源
bw.close();//此处BufferedWriter指向的是文本而不是Socket所以需要进行单独释放
ss.close();
}
}
练习四:
客户端:数据来自文本文件
服务器:接收到的数据写入文本文件
客户端:
代码语言:javascript复制import java.io.*;
import java.net.Socket;
public class ClientDemo4 {
public static void main(String[] args) throws IOException {
//创建客户端对象
Socket s = new Socket("192.168.1.105", 8888);
//封装文本文件的数据
BufferedReader br = new BufferedReader(new FileReader("src\TEMP\TEMP29\sd3.txt"));
//封装输出流写数据
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
//服务端反馈
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String data = new String(bys, 0, len);
System.out.println("服务端:" data);
}
//释放资源
br.close();
s.close();
}
}
服务器:
代码语言:javascript复制import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo4 {
public static void main(String[] args) throws IOException {
//创建服务端对象
ServerSocket ss = new ServerSocket(8888);
//监听客户端连接
Socket s = ss.accept();
//封装字符输入流,读取数据
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//把数据写入文本
BufferedWriter bw = new BufferedWriter(new FileWriter("src\TEMP\TEMP29\sd4.txt"));
String line;
while((line = br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
//给客户端反馈
OutputStream os = s.getOutputStream();
os.write("数据已读取并写入目标文本中".getBytes());
}
//释放资源
bw.close();
ss.close();
}
}
练习五:
- 客户端:数据来自文本文件,接收服务器反馈
- 服务器:接收的数据写入文本文件,给出反馈
- 出现问题:程序一直等待
- 原因:读数据的方法是阻塞式的
- 解决办法:自定义结束标语;使用shutdownOutput()方法 (推荐)
客户端:
代码语言:javascript复制import java.io.*;
import java.net.Socket;
public class ClientDemo4 {
public static void main(String[] args) throws IOException {
//创建客户端对象
Socket s = new Socket("192.168.1.105", 8888);
//封装文本文件的数据
BufferedReader br = new BufferedReader(new FileReader("src\TEMP\TEMP29\sd3.txt"));
//封装输出流写数据
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
//结束标语
s.shutdownOutput();
//接收服务端反馈
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String data = new String(bys, 0, len);
System.out.println("服务端:" data);
//释放资源
br.close();
s.close();
}
}
服务器:
代码语言:javascript复制import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo4 {
public static void main(String[] args) throws IOException {
//创建服务端对象
ServerSocket ss = new ServerSocket(8888);
//监听客户端连接
Socket s = ss.accept();
//封装字符输入流,读取数据
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//把数据写入文本
BufferedWriter bw = new BufferedWriter(new FileWriter("src\TEMP\TEMP29\sd4.txt"));
String line;
while((line = br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
//给客户端反馈
OutputStream os = s.getOutputStream();
os.write("数据已读取并写入目标文本中".getBytes());
//释放资源
bw.close();
ss.close();
}
}
练习六:
- 客户端:数据来自文本文件,接收服务器反馈
- 服务器:接收到的数据写入文本文件,给出反馈,代码用线程进行封装,为每一个客户端开启一个线程
服务器线程类:
代码语言:javascript复制import java.io.*;
import java.net.Socket;
public class ServerThread implements Runnable {
private Socket s;
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("src\TEMP\TEMP29\copyFile.txt"));
//解决名称冲突问题
int count = 0;
File file = new File("src\TEMP\TEMP29\copyFile[" count "].txt");
while(file.exists()){
count ;
file =new File("src\TEMP\TEMP29\copyFile[" 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();
}
}
}
服务器:
代码语言:javascript复制import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo5 {
public static void main(String[] args) throws IOException {
//创建服务端对象
ServerSocket ss = new ServerSocket(8888);
while(true) {
//监听客户端连接
Socket s = ss.accept();
//为每个客户端开启一个线程
new Thread(new ServerThread(s)).start();
}
// ss.close();
}
}