教育路上
摘要:java在線聊天室服務端ServerSocket。以下是我們為大家整理的,相信大家閱讀完后肯定有了自己的選擇吧。
2021-06-29 11:20網絡推薦
package chartall; import java.io.*; import java.net.*; import java.util.HashMap; import java.util.Set; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class server extends JFrame { private JButton serverstart; private JPanel up; private JPanel down; private JLabel serverstartlable; private JTextField port; private JButton serversubmit=new JButton("確定啟動"); private ServerSocket serversocket; private HashMapuserlist=new HashMap(); private Socket client; public server() { gui();//圖形界面 listion();//按鈕監聽 waitsocket();//等待客戶端連接 flashchart();//刷新聊天內容 } void gui(){ //窗體 JFrame f=new JFrame("服務端"); f.setSize(400,200); f.setLocation(300,100); up=new JPanel() ; down=new JPanel() ; serverstartlable=new JLabel("未啟動服務器") ; up.add(serverstartlable); serverstart=new JButton("啟動服務器") ; down.add(serverstart); f.setLayout(new BorderLayout()); f.add(up,BorderLayout.NORTH); f.add(down,BorderLayout.SOUTH); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } void listion() { //服務器端口 serverstart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(server.this, true ); dialog.setTitle("服務器連接端口"); dialog.setSize(300, 200); dialog.setLocation(50, 50); dialog.setLayout(new FlowLayout()); JLabel portlable=new JLabel("端口號"); port=new JTextField(10); dialog.add(portlable); dialog.add(port); dialog.add(serversubmit); dialog.setVisible(true); } }); //服務器端口確定 serversubmit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String a="服務器已啟動,端口:"+port.getText(); serverstartlable.setText(a); int ports=Integer.parseInt(port.getText()); try { serversocket=new ServerSocket(ports); }catch(Exception e1) { return; } } }); } void flashchart(){ new Thread() { public void run() { while(true) { System.out.println(client); if(client!=null) { String str; try { BufferedReader reader = new BufferedReader(new InputStreamReader(client .getInputStream())); str=reader.readLine(); }catch(Exception e1) { return; } String[] arr=str.split("@"); String usrename = arr[0]; String message = arr[1]; String status = arr[2]; //zhangshan@@reg //zhangshan@聊天內容@all //zhangshan@聊天@wang if(status.equals("reg")) { Setkeyset=userlist.keySet(); int a=0; for(String user:keyset) { if(user.equals(usrename)) { a=1; break; } } if(a==0) { userlist.put(usrename, client); } String userflash=null; for(String user:keyset) { userflash=userflash+"@"+user; } userflash=userflash+"@userlist"; for(String user:keyset) { try { PrintWriter writer = new PrintWriter(userlist.get(user).getOutputStream()); writer.println(userflash); writer.flush(); }catch(Exception e1) { return; } } }else if(status.equals("所有人")) { Setkeyset=userlist.keySet(); for(String user:keyset) { try { String allchart=usrename+"@對所有人說:"+message+"@群發聊天"; PrintWriter writer = new PrintWriter(userlist.get(user).getOutputStream()); writer.println(allchart); writer.flush(); }catch(Exception e1) { return; } } }else{ Setkeyset=userlist.keySet(); for(String user:keyset) { try { if(status.equals(user)) { String allchart=usrename+"@對"+status+"說:@"+message; PrintWriter writer = new PrintWriter(userlist.get(user).getOutputStream()); writer.println(allchart); writer.flush(); } }catch(Exception e1) { return; } } } } } } }.start(); } void waitsocket(){ new Thread() { public void run() { while(true) { System.out.println(serversocket); if(serversocket!=null) { try { client= serversocket.accept(); }catch(Exception e1) { return; } } } } }.start(); } public static void main(String[] args) { new server(); } }
訪客的評論 2024/09/19 04:20
文中描述的是準確的嗎,如何報名!