Java, UDP chat room function

Not much to say, first look at the implementation effect ———————————>

Functions :(all functions are sent to the specified IP address)

I. Interface construction

Public void init () {enclosing setSize (500600); int windowWidth = this.getWidth(); Int windowHeight = this.getheight (); / / get high window Toolkit kit = Toolkit. GetDefaultToolkit (); Dimension screenSize = kit.getScreenSize(); Int screenWidth = screensize.width; Int screenHeight = screenSize. Height; SetLocation (screenWidth/2-windowWidth/2, screenHeight/2-windowHeight/2); // Center the window to display new Receive().start(); // Random port number try {socket = new DatagramSocket(); bw = new BufferedWriter(new FileWriter("config.txt",true)); } catch (Exception e) {e.printStackTrace(); } this.setVisible(true); Public void centerPanel(){Panel Center = new Panel(); ViewText = new TextArea(); SendText = new TextArea(5,1); center.setLayout(new BorderLayout()); / / set to border layout manager center. The add (viewText, BorderLayout. Center); center.add(sendText,BorderLayout.SOUTH); viewText.setEditable(false); // Set uneditable viewtext.setbackground (color.white); // Set the background color viewtext.setfont (new Font(" XXX ",Font.PLAIN,15)); Sendtext.setfont (new Font(" XXX ",Font.PLAIN,15)); / / set to send this area font. Add (center, BorderLayout. Center); } public void southPanel(){ Panel south = new Panel(); // create a TextField to store the IP address tf = new TextField(20); // Set the default IP tf.settext (" xxx.XXX.xxx.xxx "); Send = new Button("send"); send = new Button("send"); // create record Button lg = new Button("record"); Clear = new Button("clear"); // Create shock Button shock = new Button("shock"); south.add(tf); south.add(send); south.add(lg); south.add(clear); south.add(shock); this.add(south,BorderLayout.SOUTH); }Copy the code

Second, send information

private void send(byte[] arr,String ip) throws Exception{ DatagramPacket packet = new DatagramPacket(arr,arr.length, InetAddress.getByName(ip),9999); socket.send(packet); } private void send() throws Exception{ String message = sendText.getText(); String IP = tf.gettext (); // Obtain the IP address IP = ip.trim().length() == 0? "255.255.255.255" : IP; send(message.getBytes(),ip); String time = getCurrentTime(); STR = time + ip.equals("255.255.255.255")? "All ": IP) +" say: \r\n" + message + "\r\n"; viewText.append(str); // Display message bw.write(STR); Sendtext.settext (""); // Write the information to the database. // Change the sending area to empty}Copy the code

3. Receiving messages

private class Receive extends Thread{ public void run(){ try { DatagramSocket socket = new DatagramSocket(9999); DatagramPacket packet = new DatagramPacket(new byte[8192],8192); while (true){ socket.receive(packet); Byte [] arr = packet.getData(); Int len = packet.getLength(); If (arr[0] == -1 && len == 1){// If (arr[0] == -1 && len == 1){// Shock (); // Call the vibrate action continue; } String message = new String(arr,0,len); String time = getCurrentTime(); String ip= packet.getAddress().getHostAddress(); Str1 = time + "" + IP +" said to me: \ r \ n "+ message +" \ r \ n "; viewText.append(str1); bw.write(str1); } } catch (Exception e) { e.printStackTrace(); }}}Copy the code

Four, chat records

private void logFile() throws Exception{ bw.flush(); FileInputStream fis = new FileInputStream("config.txt"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Create buffer in memory int len; byte[] arr = new byte[8192]; while((len=fis.read(arr))! =-1){ baos.write(arr,0,len); } String str = baos.toString(); // Convert the contents of memory to a string viewtext.settext (STR); }Copy the code

Five, clear screen

clear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { viewText.setText(""); }});Copy the code

Six, send “shake”

private void shock() { int x = this.getLocation().x; int y = this.getLocation().y; try { for (int i = 0; i < 10; i++) { this.setLocation(x+20,y+20); Thread.sleep(20); this.setLocation(x+20,y-20); Thread.sleep(20); this.setLocation(x-20,y+20); Thread.sleep(20); this.setLocation(x-20,y-20); Thread.sleep(20); this.setLocation(x,y); } } catch (InterruptedException e) { e.printStackTrace(); }}Copy the code

7. Shortcut keys

Sendtext.addkeylistener (new KeyAdapter() {@override public void keyReleased(KeyEvent e) { == keyevent.vk_enter){//Enter key quick send message try {send(); } catch (Exception e1) { e1.printStackTrace(); }}}});Copy the code

Article inside only part of the function code, to view all the code, please stamp here to download! Download all the code

Download.csdn.net/download/hw…