关于TCP以及UDP的相关知识,在此就不做相关总结,如有需要可以参考https://blog.csdn.net/li_ning_/article/details/52117463
一、使用TCP实现一个简易登录功能 1.创建一个实体类 User.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.tcp.multithreadingTcp;import java.io.Serializable;public class User implements Serializable { private String username; private String password; public String getUsername () { return username; } public void setUsername (String username) { this .username = username; } public String getPassword () { return password; } public void setPassword (String password) { this .password = password; } @Override public String toString () { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}' ; } }
2.服务器端代码 LoginServer.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.tcp.multithreadingTcp;import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class LoginServer { public static void main (String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8888 ); int i = 0 ; while (true ) { Socket socket = serverSocket.accept(); new Thread(new LoginThread(socket)).start(); System.out.println("你是访问该服务器的第" + ++i + "个用户,你的ip地址为" + socket.getInetAddress().getHostAddress()); socket.close(); } } }
3.登录线程代码 LoginThread.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package com.tcp.multithreadingTcp;import java.io.*;import java.net.Socket;public class LoginThread implements Runnable { private Socket socket; LoginThread(Socket socket) { this .socket = socket; } @Override public void run () { try { ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream()); User user = (User) objectInputStream.readObject(); System.out.println(user.toString()); DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()); if ("zhangsan" .equals(user.getUsername()) && "1234" .equals(user.getPassword())) { dataOutputStream.writeUTF("登录成功" ); } else { dataOutputStream.writeUTF("登录失败" ); } dataOutputStream.close(); objectInputStream.close(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } }
4.客户端代码 LoginClient.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.tcp.multithreadingTcp;import java.io.*;import java.net.InetAddress;import java.net.Socket;import java.util.Scanner;public class LoginClient { public static void main (String[] args) throws IOException { Socket socket = new Socket(InetAddress.getLocalHost(), 8888 ); Scanner sc = new Scanner(System.in); System.out.print("请输入用户名:" ); String username = sc.nextLine(); System.out.print("请输入密码:" ); String password = sc.nextLine(); User user = new User(); user.setUsername(username); user.setPassword(password); OutputStream outputStream = socket.getOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(user); InputStream inputStream = socket.getInputStream(); DataInputStream dataInputStream = new DataInputStream(inputStream); String res = dataInputStream.readUTF(); System.out.println(res); dataInputStream.close(); inputStream.close(); objectOutputStream.close(); outputStream.close(); socket.close(); } }
运行结果:(客户端)
1 2 3 请输入用户名:zhangsan 请输入密码:1234 登录成功
运行结果:(服务端)
1 2 User{username='zhangsan', password='1234'} 你是访问该服务器的第1个用户,你的ip地址为xxx.xxx.xxx.xxx
二、使用UDP实现一个简易聊天室功能
UDP相较于TCP实现简单一些,所以代码量也稍微的少一些
1.服务端代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 package com.udp.moreThanOneUdp;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.util.Scanner;public class AskServer { public static void main (String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(8888 ); Scanner sc = new Scanner(System.in); while (true ) { byte [] buf = new byte [1024 ]; DatagramPacket datagramPacket1 = new DatagramPacket(buf, buf.length); socket.receive(datagramPacket1); System.out.println("对方说:" + new String(datagramPacket1.getData(), 0 , datagramPacket1.getLength())); System.out.print("请您输入:" ); String info = sc.nextLine(); InetAddress inetAddress = datagramPacket1.getAddress(); int port = datagramPacket1.getPort(); DatagramPacket datagramPacket2 = new DatagramPacket(info.getBytes(), info.getBytes().length, inetAddress, port); socket.send(datagramPacket2); if (info.equals("bye" )) { break ; } } System.out.println("聊天结束" ); socket.close(); } }
2.客户端代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 package com.udp.moreThanOneUdp;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.util.Scanner;public class AskClient { public static void main (String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(8889 ); Scanner sc = new Scanner(System.in); while (true ) { System.out.print("请您输入:" ); String info = sc.nextLine(); InetAddress inetAddress = InetAddress.getLocalHost(); DatagramPacket datagramPacket1 = new DatagramPacket(info.getBytes(), info.getBytes().length, inetAddress, 8888 ); socket.send(datagramPacket1); if (info.equals("bye" )) { break ; } byte [] buf = new byte [1000 ]; DatagramPacket datagramPacket2 = new DatagramPacket(buf, buf.length); socket.receive(datagramPacket2); System.out.println("对方说:" + new String(buf, 0 , datagramPacket2.getLength())); } System.out.println("聊天结束" ); socket.close(); } }
运行结果:(客户端)
1 2 3 4 5 6 7 8 请您输入:你好呀 对方说:你是谁 请您输入:我是张三,你呢 对方说:我是李四 请您输入:好的,我要忙了 对方说:bye 请您输入:bye 聊天结束
运行结果:(服务端)
1 2 3 4 5 6 7 对方说:你好呀 请您输入:你是谁 对方说:我是张三,你呢 请您输入:我是李四 对方说:好的,我要忙了 请您输入:bye 聊天结束
三、使用TCP实现文件上传以及文件下载功能
文件上传的本质:文件从客户端到服务器端的复制
文件下载的本质:文件从服务器端到客户端的复制
文件的上传和下载都是两次文件复制
1.文件上传功能服务端代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 package com.tcp.uploadTcp;import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class UploadServer { public static void main (String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8888 ); Socket socket = serverSocket.accept(); BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream()); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File("/test1.txt" ))); int n; n = bufferedInputStream.read(); while (n != -1 ){ bufferedOutputStream.write(n); System.out.println(1 ); n = bufferedInputStream.read(); } bufferedInputStream.close(); bufferedOutputStream.close(); socket.close(); serverSocket.close(); } }
2.文件上传功能客户端代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.tcp.uploadTcp;import java.io.*;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.UnknownHostException;public class UploadClient { public static void main (String[] args) throws IOException { Socket socket = new Socket(InetAddress.getLocalHost(),8888 ); BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(new File("/test.txt" ))); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream()); bufferedInputStream.close(); bufferedOutputStream.close(); socket.close(); } }
1.文件下载功能服务端代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 package com.tcp.downloadTcp;import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class DownloadServer { public static void main (String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8888 ); Socket socket = serverSocket.accept(); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File("/test.txt" ))); BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream()); int n; n = bufferedInputStream.read(); while (n != -1 ){ bufferedOutputStream.write(n); n = bufferedInputStream.read(); } bufferedInputStream.close(); bufferedOutputStream.close(); socket.close(); serverSocket.close(); } }
2.文件下载功能客户端代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.tcp.downloadTcp;import java.io.*;import java.net.InetAddress;import java.net.Socket;public class DownloadClient { public static void main (String[] args) throws IOException { Socket socket = new Socket(InetAddress.getLocalHost(),8888 ); BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream()); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File("/test1.txt" ))); bufferedInputStream.close(); bufferedOutputStream.close(); socket.close(); } }
其实上传功能和下载功能的逻辑是相同的,只不过一个是文件c => s,一个是文件s => c
参考视频:https://www.bilibili.com/video/av31123719/