java socket聊天室
2021/10/2 14:12:02
本文主要是介绍java socket聊天室,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
服务端
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(); } }
效果
这篇关于java socket聊天室的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南