1.网络编程入门
1.1.网络编程概述
1.2网络编程三要素
1.3 IP
IP地址常用命令:
ipconfig 查看本机IP地址
ping IP地址 检查网络是否连通
特殊IP地址:127.0.0.1:是回送地址,可以表示本机地址,一般用来测试使用
1.4 InetAddress
// InetAddress inetAddress = InetAddress.getByName("DESKTOP-2PVNKVI"); InetAddress inetAddress = InetAddress.getByName("192.168.0.111"); String hostAddress = inetAddress.getHostAddress(); String hostName = inetAddress.getHostName(); System.out.println(hostAddress+":"+hostName);
1.5端口
1.6协议
2.UDP通信程序
2.1 UDP通信原理
2.2 UDP发送数据
public static void main(String[] args) throws IOException { // 1.创建发送端Socket对象DatagramSocket DatagramSocket datagramSocket = new DatagramSocket(); // 2.创建数据,并打包数据 // DatagramPacket(byte[] buf, int length, InetAddress address, int port) : // 构造一个数据包,发送长度为 length的数据包到指定主机上的指定端口号。 byte[] bytes = "hello,UDP,I`M COMMING!!!".getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.0.111"),8088); // 3.发送数据 datagramSocket.send(datagramPacket); // 4.关闭发送端 datagramSocket.close(); }
2.3UDP接收数据
public static void main(String[] args) throws IOException { // 1.创建接收端的Socket对象,DataGramSocket // DatagramSocket(int port) 构造数据报套接字并将其绑定到本地主机上的指定端口。 DatagramSocket datagramSocket = new DatagramSocket(8088); // 2.创建一个数据包用来接收数据 // DatagramPacket(byte[] buf, int length) 构造一个 DatagramPacket用于接收长度为 length数据 byte[] bytes = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length); // 3.接收数据 datagramSocket.receive(datagramPacket); // 4.解析数据,加工数据,使数据为实际数据长度 byte[] data = datagramPacket.getData(); String s = new String(data, 0, datgramPacket.getLength); // 5.展示数据 System.out.println("接收到数据:"+s); // 6.关闭接收端 datagramSocket.close(); }
2.4UDP练习
public class PracticeSend { public static void main(String[] args) throws IOException { BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line=bufferedreader.readLine())!=null){ // 输入数据是886,终止发送 if (line.equals("886")){ break; } send(line); } } private static void send(String s) throws IOException { DatagramSocket datagramSocket = new DatagramSocket(); byte[] bytes = s.getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.0.111"),8088); datagramSocket.send(datagramPacket); datagramSocket.close(); } }
public class PracticeRecive { public static void main(String[] args) throws IOException { DatagramSocket datagramSocket = new DatagramSocket(8088); while (true) {//因为不知道发送端什么时候停止发送,所以一直接收 recive(datagramSocket); } } private static void recive(DatagramSocket datagramSocket) throws IOException { byte[] bytes = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length); datagramSocket.receive(datagramPacket); byte[] data = datagramPacket.getData(); System.out.println("接收到数据:" + new String(data, 0, datagramPacket.getLength())); } }
3.TCP通信
3.1TCP通信原理
3.2TCP发送数据
public static void main(String[] args) throws IOException { // 创建客户端Socket对象(Socket) // Socket(InetAddress address, int port) 创建流套接字并将其连接到指定IP地址的指定端口号。 // Socket socket = new Socket(InetAddress.getByName("192.168.0.111"),8099); // Socket(String host, int port) 创建流套接字并将其连接到指定主机上的指定端口号。 Socket st = new Socket("192.168.0.111", 8099); // 2.获取输出流写数据 OutputStream outputStream = st.getOutputStream(); outputStream.write("^_^,你好TCP!!!".getBytes()); // 释放资源 st.close(); }
3.3TCP接收数据
//1.创建服务器端Socket对象(ServerSocket) //ServerSocket(int port) 创建绑定到指定端口的服务器套接字。 ServerSocket serverSocket = new ServerSocket(8099); // 2.监听客户连接并返回一个Socket对象:Socket accept() 侦听要连接到此套接字并接受它。 Socket socket = serverSocket.accept(); // 3.获取输入流并读取数据进行展示 InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; int length=inputStream.read(bytes); String s = new String(bytes, 0, length); System.out.println("数据是:"+s); // 4.释放资源 inputStream.close(); serverSocket.close(); }
3.4TCP案例
字符流发送接收数据(注意:当发送视频音频图片时等不可使用字符流)
public class Client2 { public static void main(String[] args) throws IOException { Socket socket = new Socket("192.168.0.111", 8099); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String line; while ((line=bufferedReader.readLine())!=null){ if ("886".equals(line)){ break; } bufferedWriter.write(line); bufferedWriter.flush(); bufferedWriter.newLine(); } socket.close(); } }
public class Server2 { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8099); Socket socket = serverSocket.accept(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; while ((line=bufferedReader.readLine())!=null){ System.out.println(line); } serverSocket.close(); } }
复制文件并给出反馈
public class Client2 { public static void main(String[] args) throws IOException { Socket socket = new Socket("192.168.0.111", 8099); BufferedReader bufferedReader = new BufferedReader(new FileReader("file\UDPsend.java")); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String line; while ((line = bufferedReader.readLine()) != null) { bufferedWriter.write(line); bufferedWriter.flush(); bufferedWriter.newLine(); } // 自定义结束标制(存在问题:被复制文件中自带与结束标准一样的字符时就会有问题) // bufferedWriter.write("886"); // bufferedWriter.newLine(); // bufferedWriter.flush(); // Socket类自带结束发送的方法: socket.shutdownOutput(); // 接收服务器反馈 BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String s = reader.readLine(); System.out.println("接收到服务器反馈:" + s); reader.close(); bufferedReader.close(); socket.close(); } }
public class Server2 { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8099); Socket socket = serverSocket.accept(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("file\UDPsend2.java")); String line; while ((line = bufferedReader.readLine()) != null) { // if ("886".equals(line)){ // break; // } bufferedWriter.write(line); bufferedWriter.newLine(); bufferedWriter.flush(); } // 给出反馈 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); writer.write("文件已经接收完毕!!!"); writer.newLine(); writer.flush(); writer.close(); bufferedWriter.close(); serverSocket.close(); } }
多线程实现文件上传
public class Server3 { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8099); while (true){ // 监听客户端连接,返回一个对应的Socket对象 Socket socket = serverSocket.accept(); ReciveRunable runable = new ReciveRunable(socket); // 为每一个客户端开启一个线程 Thread thread = new Thread(runable); thread.start(); } } }
public class ReciveRunable implements Runnable{ private Socket socket; public ReciveRunable(Socket socket) { this.socket=socket; } @Override public void run() { try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); int count=0; File file = new File("file\copy[" + count + "].txt"); while (file.exists()){ count++; file = new File("file\copy[" + count + "].txt"); } BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file)); String line; while ((line=bufferedReader.readLine())!=null){ bufferedWriter.write(line); bufferedWriter.newLine(); bufferedWriter.flush(); } } catch (IOException e) { e.printStackTrace(); } } }
public class Client3 { public static void main(String[] args) throws IOException { Socket socket = new Socket("192.168.0.111", 8099); BufferedReader bufferedReader = new BufferedReader(new FileReader("file\UDPsend.java")); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String line; while ((line = bufferedReader.readLine()) != null) { bufferedWriter.write(line); bufferedWriter.flush(); bufferedWriter.newLine(); } socket.shutdownOutput(); bufferedReader.close(); socket.close(); } }