JAva udpプロトコル

7039 ワード

package udp;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ServerUI extends JFrame{
 
 JTextArea textarea = new JTextArea();
 DatagramSocket ds = null;
 
 public ServerUI(){
  super("   ");
  Container con = getContentPane();
  con.setLayout(new BorderLayout());
  textarea.setEditable(false);
  con.add(new JScrollPane(textarea),BorderLayout.CENTER); 
  setSize(400,600);
  setVisible(true);
  
     addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
             ds.close();
             System.exit(0);
            }
        });
 }
 
 private void startServer() {
  // TODO Auto-generated method stub
  
        DatagramPacket dp = null;
        try {
            ds = new DatagramSocket(3000); //              ,   3000
            
            while (true)
            {
                byte[] buf = new byte[1024];
                dp = new DatagramPacket(buf,1024); //       1024    ,            buf 
                ds.receive(dp); //           
                displayClientUser(dp);    //              
                //       
                String message = dp.getAddress().toString().substring(1)+" :"+new String(buf).trim();
                DatagramPacket response = new DatagramPacket(message.getBytes(),message.getBytes().length,dp.getAddress(),dp.getPort());
                ds.send(response);
                
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally{
            ds.close();
        }
 }
 
    private void displayClientUser(DatagramPacket dp) {
  // TODO Auto-generated method stub
     textarea.append(dp.getAddress().toString().substring(1)+"    !
");  }
 public static void main(String[] args) {
        //new ServerUI().startServer();
  new ServerUI().startServer();
    }
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
package udp;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ClientUI extends JFrame {
 
 JTextArea textarea = new JTextArea();
 JTextField textfield = new JTextField(22);
    DatagramSocket ds = null;      
    DatagramPacket dp = null;
 
    public ClientUI(){
     super("   ");
     Container con = getContentPane();
     con.setLayout(new BorderLayout());
     textarea.setEditable(false);
     
     con.add(new JScrollPane(textarea),BorderLayout.CENTER);
     con.add(southPanel(),BorderLayout.SOUTH);
     
     setSize(600,500);
     setVisible(true);
     addWindowListener(new WindowAdapter(){
              public void windowClosing(WindowEvent e){
               ds.close();
               System.exit(0);
              }
     });
    }
    
    private Component southPanel() {
  // TODO Auto-generated method stub
     JPanel panel = new JPanel();
     panel.setLayout(new BorderLayout());
     
     JPanel panel2 = new JPanel();
     panel2.setLayout(new GridLayout(1,2));
     JButton button = new JButton("  ");
     button.addActionListener(new ActionListener(){
   @Override
   public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    sendMessage(ds,dp); //     
   }
      
     });
     panel2.add(textfield);
     panel2.add(button);
     
     panel.add(panel2,BorderLayout.EAST);
  return panel;
 }
 public static void main(String[] args) {
        new ClientUI().startClient();   
    }
 private void startClient() {
  // TODO Auto-generated method stub
        try {
            ds = new DatagramSocket(); //              
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        receiveMessage(ds, dp);  
 }
 private void receiveMessage(DatagramSocket ds, DatagramPacket dp){
  // TODO Auto-generated method stub
    try
    {
     while(true){
        byte[] buf = new byte[1024];
        dp = new DatagramPacket(buf,1024); //       1024    ,            buf 
     ds.receive(dp);
     // TODO Auto-generated catch block
        textarea.append(new String(buf).trim()+"
");      }     }catch(Exception e){             e.printStackTrace();             throw new RuntimeException(e);     }finally{      ds.close();     }
 }
 private void sendMessage(DatagramSocket ds, DatagramPacket dp) {
  // TODO Auto-generated method stub
        String str=textfield.getText();
        if(str!=null && !str.trim().equals("")){
            byte[] sendInfo = str.getBytes();
            try {
             //       10.0.157.126   3000  
    dp = new DatagramPacket(sendInfo,sendInfo.length,
             InetAddress.getByName("10.0.157.126"),3000); 
             ds.send(dp); //     
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } 
        }
        textfield.setText("");
 }
}