Facebook
From Mature Lion, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 232
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.InetAddress;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. import javax.swing.JTextArea;
  9.  
  10. /*
  11.  * To change this license header, choose License Headers in Project Properties.
  12.  * To change this template file, choose Tools | Templates
  13.  * and open the template in the editor.
  14.  */
  15.  
  16. /**
  17.  *
  18.  * @author Kamil
  19.  */
  20. public class MyServerForm extends javax.swing.JFrame {
  21.  
  22.  
  23. static Socket s;
  24. static ServerSocket ss;
  25. static InputStreamReader isr;
  26. static BufferedReader br;
  27. static String message;
  28.    
  29.     public MyServerForm() {
  30.         initComponents();
  31.     }
  32.  
  33.  
  34.     @SuppressWarnings("unchecked")
  35.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  36.     private void initComponents() {
  37.  
  38.         jScrollPane1 = new javax.swing.JScrollPane();
  39.         jTextArea1 = new javax.swing.JTextArea();
  40.         jButton1 = new javax.swing.JButton();
  41.         jTextField1 = new javax.swing.JTextField();
  42.  
  43.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  44.  
  45.         jTextArea1.setColumns(20);
  46.         jTextArea1.setRows(5);
  47.         jScrollPane1.setViewportView(jTextArea1);
  48.  
  49.         jButton1.setText("jButton1");
  50.         jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
  51.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  52.                 jButton1MouseClicked(evt);
  53.             }
  54.         });
  55.  
  56.         jTextField1.setText("jTextField1");
  57.  
  58.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  59.         getContentPane().setLayout(layout);
  60.         layout.setHorizontalGroup(
  61.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  62.             .addGroup(layout.createSequentialGroup()
  63.                 .addGap(134, 134, 134)
  64.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  65.                     .addGroup(layout.createSequentialGroup()
  66.                         .addComponent(jButton1)
  67.                         .addGap(45, 45, 45)
  68.                         .addComponent(jTextField1))
  69.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 409, javax.swing.GroupLayout.PREFERRED_SIZE))
  70.                 .addContainerGap(167, Short.MAX_VALUE))
  71.         );
  72.         layout.setVerticalGroup(
  73.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  74.             .addGroup(layout.createSequentialGroup()
  75.                 .addGap(120, 120, 120)
  76.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
  77.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  78.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  79.                     .addComponent(jButton1)
  80.                     .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  81.                 .addContainerGap(96, Short.MAX_VALUE))
  82.         );
  83.  
  84.         pack();
  85.     }// </editor-fold>                        
  86.  
  87.     private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
  88.         try
  89. {
  90.         Socket s=new Socket("192.168.0.31",7000);
  91.         PrintWriter pw = new PrintWriter(s.getOutputStream());
  92.         pw.write(jTextField1.getText());
  93.         pw.flush();
  94.         pw.close();
  95.         s.close();
  96. }catch(IOException e)
  97. {
  98.         e.printStackTrace();
  99. }
  100.     }                                    
  101.  
  102.     public void interpreter(String tekst)
  103.     {
  104.        
  105.     }
  106.    
  107.     public static void main(String args[]) {
  108.         /* Set the Nimbus look and feel */
  109.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  110.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  111.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  112.          */
  113.         try {
  114.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  115.                 if ("Nimbus".equals(info.getName())) {
  116.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  117.                     break;
  118.                 }
  119.             }
  120.         } catch (ClassNotFoundException ex) {
  121.             java.util.logging.Logger.getLogger(MyServerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  122.         } catch (InstantiationException ex) {
  123.             java.util.logging.Logger.getLogger(MyServerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  124.         } catch (IllegalAccessException ex) {
  125.             java.util.logging.Logger.getLogger(MyServerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  126.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  127.             java.util.logging.Logger.getLogger(MyServerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  128.         }
  129.         //</editor-fold>
  130.  
  131.         /* Create and display the form */
  132.         java.awt.EventQueue.invokeLater(new Runnable() {
  133.             public void run() {
  134.                 new MyServerForm().setVisible(true);
  135.             }
  136.         });
  137.        
  138.         try{
  139.                 ss = new ServerSocket(6000);
  140.                 while(true)
  141.                 {
  142.                 s = ss.accept();
  143.                 isr = new InputStreamReader(s.getInputStream());
  144.                 br = new BufferedReader(isr);
  145.                 message = br.readLine();
  146.                
  147.                 System.out.println(message);
  148.                
  149.                
  150.                
  151.                 if(jTextArea1.getText().toString().equals(""))
  152.                 {
  153.                     jTextArea1.setText(message);
  154.                    
  155.                    
  156.                 }
  157.                  else   {
  158.                     jTextArea1.setText(jTextArea1.getText()+ "n"+message);
  159.                 }
  160.                    
  161.                 }
  162.                
  163.                
  164.                 }catch(IOException e)
  165.                 {
  166.                         e.printStackTrace();
  167.                 }
  168.         }
  169.        
  170.    
  171.  
  172.     // Variables declaration - do not modify                    
  173.     private javax.swing.JButton jButton1;
  174.     private javax.swing.JScrollPane jScrollPane1;
  175.     private static javax.swing.JTextArea jTextArea1;
  176.     private javax.swing.JTextField jTextField1;
  177.     // End of variables declaration                  
  178. }
  179.