Java基础-网络编程
概念
三要素:ip地址、端口、协议
ip地址
InetAddress:表示Internet协议(IP)地址
static InetAddress getByName(String host)
确定主机名称的IP地址。主机名称可以是机器名称,也可以是IP地址String getHostName()
获取此IP地址的主机名String getHostAddress()
返回文本显示中的IP地址字符串
package network.demo1;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test1 {
public static void main(String[] args) throws UnknownHostException {
InetAddress address = InetAddress.getByName("LAPTOP");
String name = address.getHostName();
String ip = address.getHostAddress();
System.out.println(address);
System.out.println(name + ' ' + ip);
}
}
/**
LAPTOP/192.168.93.1
LAPTOP 192.168.93.1
**/
端口
端口:设备上应用程序的唯一标识
端口号:用两个字节表示的整数,它的取值范围是065535。其中,01023之间的端口号用于一些知名的网络服务和应用,普通的应用程序需要使用1024以上的端口号。如果端口号被另外一个服务或应用所占用,会导致当前程序启动失败
协议
计算机网络中,连接和通信的规则被称为网络通信协议。如TCP/UDP
UDP
UDP协议是一种不可靠的网络协议,它在通信的两端各建立一个Socket对象,但是这两个Socket只是发送,接收数据的对象因此对于基于UDP协议的通信双方而言,没有所谓的客户端和服务器的概念
Java提供了DatagramSocket类作为基于UDP协议的Socket
Client
package network.demo2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Client {
public static void main(String[] args) throws IOException {
//连接信息
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 6666;
//创建socket对象
DatagramSocket socket = new DatagramSocket();
byte[] bys = "Hello world".getBytes();
//数据包对象
DatagramPacket dp = new DatagramPacket(bys, bys.length, address, port);
//发送数据包
socket.send(dp);
socket.close();
}
}
Server
package network.demo2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Server {
public static void main(String[] args) throws IOException {
//绑定socket
DatagramSocket socket = new DatagramSocket(6666);
byte[] bys = new byte[1024];
//接收数据包
DatagramPacket dp = new DatagramPacket(bys, bys.length);
//接收数据
socket.receive(dp);
//解析数据 放入缓存区
byte[] datas= dp.getData();
int len = dp.getLength();
String dataStr = new String(datas, 0, len);
System.out.println(dataStr);
//关闭socket
socket.close();
}
}
TCP
TCP通信协议是一种可靠的网络协议,它在通信的两端各建立一个Socket对象,从而在通信的两端形成网络虚拟链路,一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路进行通信。
Java对基于TCP协议的的网络提供了良好的封装,使用Socket对象来代表两端的通信端口,并通过Socket产生IO流来进行网络通信。
Java为客户端提供了Socket类,为服务器端提供了ServerSocket类。
Client
package network.demo4;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 6666;
//绑定socket
Socket socket = new Socket(ip, port);
//获取流 写数据
OutputStream os = socket.getOutputStream();
os.write("Hello world".getBytes());
socket.close();
}
}
Server
package network.demo4;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
//绑定socket
ServerSocket serverSocket = new ServerSocket(6666);
//监听socket
Socket socket = serverSocket.accept();
//获取输入流
InputStream is = socket.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);;
String data = new String(bys, 0, len);
System.out.println(data);
socket.close();
serverSocket.close();
}
}
Pracite1
客户端:发送数据,接收服务器反馈
服务器:接收数据,给出反馈
Client
package network.demo5;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 6666);
OutputStream os = socket.getOutputStream();
os.write("hello".getBytes());
InputStream is = socket.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String data =new String(bys, 0, len);
System.out.println(data);
is.close();
os.close();
socket.close();
}
}
Server
package network.demo5;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(6666);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String data = new String(bys, 0, len);
System.out.println(data);
OutputStream os = socket.getOutputStream();
os.write(("大写:" + data.toUpperCase()).getBytes());
}
}
Pracite2
客户端:数据来自于键盘录入,直到输入的数据是quit,发送数据结束服务器:接收到的数据在控制台输出
Clinet
package network.demo6;
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 6666);
//键盘输入 输入流
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//封装输出流对象 字节输出流->字符输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
while ((line= br.readLine()) != null){
if ("quit".equals(line)){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
socket.close();
}
}
Server
package network.demo6;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(6666);
Socket socket = serverSocket.accept();
/* InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);*/
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line=br.readLine()) != null){
System.out.println(line);
}
//释放资源
serverSocket.close();
}
}
Pracite3
客户端:数据来自于键盘录入,直到输入的数据是quit,发送数据结束服务器:接收到的数据写入文本文件
Client
package network.demo7;
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 6666);
//键盘输入 输入流
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//封装输出流对象 字节输出流->字符输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
while ((line= br.readLine()) != null){
if ("quit".equals(line)){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
socket.close();
}
}
Server
package network.demo7;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(6666);
Socket socket = serverSocket.accept();
//接收数据
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//把数据写入文件
BufferedWriter bw = new BufferedWriter(new FileWriter("tcp.txt"));
String line;
while ((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
serverSocket.close();
}
}
Pracite4
客户端:数据来自于文本文件
服务器:接收到的数据写入文本文件
Client
package network.demo8;
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 6666);
//键盘输入 输入流
BufferedReader br = new BufferedReader(new FileReader("client.java"));
//封装输出流对象 字节输出流->字符输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
while ((line= br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
socket.close();
}
}
Server
package network.demo8;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(6666);
Socket socket = serverSocket.accept();
//接收数据
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//把数据写入文件
BufferedWriter bw = new BufferedWriter(new FileWriter("server.java"));
String line;
while ((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
serverSocket.close();
}
}
Pratice5
客户端:数据来自于文本文件,接收服务器反馈
服务器:接收到的数据写入文本文件,给出反馈,代码用线程进行封装,为每一个客户端开启一个线程
Client
package network.demo10;
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 6666);
//键盘输入 输入流
BufferedReader br = new BufferedReader(new FileReader("client.java"));
//封装输出流对象 字节输出流->字符输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
while ((line= br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//设置结束接收信号
socket.shutdownOutput();
BufferedReader brStr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(brStr.readLine());
socket.close();
}
}
Server
package network.demo10;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(6666);
while (true){
Socket socket = serverSocket.accept();
new Thread(new ServerThread(socket)).start();
}
}
}
ServerThread
package network.demo10;
import java.io.*;
import java.net.Socket;
public class ServerThread implements Runnable {
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int nums = 0;
File file = new File("Server[" + nums + "].java");
while (file.exists()){
nums++;
file = new File("Server[" + nums + "].java");
}
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
String line;
while ((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
BufferedWriter bwStr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bwStr.write("文件上传成功");
bwStr.newLine();
bwStr.flush();
}catch (IOException e){
e.printStackTrace();
}
}
}