Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. package eu.matim;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.WindowAdapter;
  8. import java.awt.event.WindowEvent;
  9. import java.awt.event.WindowListener;
  10. import java.io.BufferedReader;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.io.PrintWriter;
  14. import java.net.Socket;
  15. import java.net.UnknownHostException;
  16.  
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JPanel;
  21. import javax.swing.JTextField;
  22. import javax.swing.UIManager;
  23. import javax.swing.UIManager.LookAndFeelInfo;
  24.  
  25. @SuppressWarnings("serial")
  26. public class SocketClient extends JFrame implements ActionListener
  27. {
  28.     JLabel text, clicked;
  29.     JButton button;
  30.     JPanel panel;
  31.     JTextField textField;
  32.     Socket socket = null;
  33.     PrintWriter out = null;
  34.     BufferedReader in = null;
  35.    
  36.     private String ip = "85.117.9.11";
  37.     private int port = 4444;
  38.  
  39.     public SocketClient()
  40.     {
  41.         text = new JLabel("Text to send over socket:");
  42.         textField = new JTextField(20);
  43.         button = new JButton("Click Me");
  44.         button.addActionListener(this);
  45.  
  46.         panel = new JPanel();
  47.         panel.setLayout(new BorderLayout());
  48.         panel.setBackground(Color.white);
  49.         getContentPane().add(panel);
  50.         panel.add("North", text);
  51.         panel.add("Center", textField);
  52.         panel.add("South", button);
  53.     }
  54.  
  55.     public void actionPerformed(ActionEvent event)
  56.     {
  57.         Object source = event.getSource();
  58.  
  59.         if(source == button)
  60.         {
  61.             //Send data over socket
  62.             String text = textField.getText();
  63.             out.println(text);
  64.             textField.setText(new String(""));
  65.            
  66.             // Print received data
  67.             try
  68.             {
  69.                 String line = in.readLine();
  70.                
  71.                 if (line.equals("mayConnect"))
  72.                 {
  73.                     System.out.println("Password was ok, we can connect! :)");
  74.                 }
  75.                 else if (line.equals("cantConnect"))
  76.                 {
  77.                     System.out.println("Password wasn't correct :(!");
  78.                 }
  79.             }
  80.             catch (IOException e)
  81.             {
  82.                 System.out.println("Client: " + e);
  83.                 listenSocket();
  84.             }
  85.         }
  86.     }
  87.  
  88.     public void listenSocket()
  89.     {
  90.         //Create socket connection
  91.         try
  92.         {
  93.             socket = new Socket(ip, port);
  94.             out = new PrintWriter(socket.getOutputStream(), true);
  95.             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  96.         }
  97.         catch (UnknownHostException e)
  98.         {
  99.             System.out.println("Client: Unknown host: " + ip);
  100.             System.exit(1);
  101.         }
  102.         catch (IOException e)
  103.         {
  104.             System.out.println("Client: No I/O " + e);
  105.             // Try to reconnect
  106.             listenSocket();
  107.         }
  108.     }
  109.  
  110.    public static void main(String[] args)
  111.    {
  112.         try
  113.         {
  114.             for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
  115.             {
  116.                 if ("Nimbus".equals(info.getName()))
  117.                 {
  118.                     UIManager.setLookAndFeel(info.getClassName());
  119.                     break;
  120.                 }
  121.             }
  122.         }
  123.         catch (Exception e)
  124.         {
  125.             // Shit..
  126.         }
  127.        
  128.        SocketClient frame = new SocketClient();
  129.        frame.setTitle("Matim Client");
  130.        WindowListener l = new WindowAdapter()
  131.        {
  132.            public void windowClosing(WindowEvent e)
  133.            {
  134.                System.exit(0);
  135.            }
  136.         };
  137.  
  138.         frame.addWindowListener(l);
  139.         frame.pack();
  140.         frame.setVisible(true);
  141.         frame.listenSocket();
  142.    }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement