服务端
import javax.naming.ldap.SortKey; import java.io.IOException; import java.net.DatagramSocket; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.Scanner; public class Server_Chat { private ServerSocket serverSocket = null; public Server_Chat(int port) throws IOException { serverSocket = new ServerSocket(port); } public void start() throws IOException, ClassNotFoundException { Socket socket = connection(); User user = getUserMsg(socket); sendUserToClient(socket,user); printUser(user); while (true) { Scanner scanner = new Scanner(System.in); receiveMsg(socket); sendMsg(socket, scanner.next()); } } public void sendUserToClient(Socket socket,User user) throws IOException { ChatUtils.sendUserToClient(socket,user); } public void printUser(User user) { System.out.println(user + " 上线了"); } public User getUserMsg(Socket socket) { String ip = socket.getInetAddress().getHostAddress(); int port = socket.getPort(); return new User(ip,port); } public void receiveMsg(Socket socket) throws IOException { User userMsg = getUserMsg(socket); String msg = ChatUtils.receiveMsg(socket); System.out.println(userMsg +" : "+ msg); } public void sendMsg(Socket socket,String msg) throws IOException { ChatUtils.sendMsg(socket, msg); } public void sendChatType(Socket socket,ChatType chatType) throws IOException { ChatUtils.sendChatType(socket,chatType); } public ChatType receiveChatType(Socket socket) throws IOException, ClassNotFoundException { return ChatUtils.receiveChatType(socket); } public Socket connection(){ try { Socket accept = serverSocket.accept(); return accept; } catch (IOException e) { e.printStackTrace(); } return null; } }
客户端
import java.io.IOException; import java.net.Socket; import java.util.Scanner; public class Client_Chat { private String ip; private int port; public Client_Chat() throws IOException { } public void start() throws Exception { Socket socket = connection("127.0.0.1",6666); User user = receiveUserToClient(socket); printUser(user); while (true) { Scanner scanner = new Scanner(System.in); sendMsg(socket, scanner.next()); System.out.println(user +" : "+receiveMsg(socket)); } } public User receiveUserToClient(Socket socket) throws Exception { User user = ChatUtils.receiveUserToClient(socket); return user; } public void printUser(User user) { System.out.println(user + "上线了"); } public Socket connection(String ip,int port) throws IOException { return new Socket(ip,port); } public void sendMsg(Socket socket,String msg) throws IOException { ChatUtils.sendMsg(socket, msg); } public String receiveMsg(Socket socket) throws IOException { return ChatUtils.receiveMsg(socket); } public void sendChatType(Socket socket,ChatType chatType) throws IOException { ChatUtils.sendChatType(socket,chatType); } public ChatType receiveChatType(Socket socket) throws IOException, ClassNotFoundException { return ChatUtils.receiveChatType(socket); } }
信息收发工具类
import java.io.*; import java.net.Socket; import java.nio.charset.StandardCharsets; public class ChatUtils { public static void sendMsg(Socket socket,String msg) throws IOException { OutputStream outputStream = socket.getOutputStream(); byte[] bytes = msg.getBytes(); outputStream.write(msg.getBytes(StandardCharsets.UTF_8)); } public static String receiveMsg(Socket socket) throws IOException { InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; int read = inputStream.read(bytes); return new String(bytes,0,read); } public static void sendUserToClient(Socket socket,User user) throws IOException { ObjectOutputStream oop = new ObjectOutputStream(socket.getOutputStream()); oop.writeObject(user); } public static User receiveUserToClient(Socket socket) throws Exception { ObjectInputStream oip = new ObjectInputStream(socket.getInputStream()); return (User) oip.readObject(); } public static void sendChatType(Socket socket,ChatType chatType) throws IOException { ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(chatType); } public static ChatType receiveChatType(Socket socket) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); return (ChatType)ois.readObject(); } }
效果