Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eu.matim;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.UIManager;
- import javax.swing.UIManager.LookAndFeelInfo;
- @SuppressWarnings("serial")
- public class SocketClient extends JFrame implements ActionListener
- {
- JLabel text, clicked;
- JButton button;
- JPanel panel;
- JTextField textField;
- Socket socket = null;
- PrintWriter out = null;
- BufferedReader in = null;
- private String ip = "85.117.9.11";
- private int port = 4444;
- public SocketClient()
- {
- text = new JLabel("Text to send over socket:");
- textField = new JTextField(20);
- button = new JButton("Click Me");
- button.addActionListener(this);
- panel = new JPanel();
- panel.setLayout(new BorderLayout());
- panel.setBackground(Color.white);
- getContentPane().add(panel);
- panel.add("North", text);
- panel.add("Center", textField);
- panel.add("South", button);
- }
- public void actionPerformed(ActionEvent event)
- {
- Object source = event.getSource();
- if(source == button)
- {
- //Send data over socket
- String text = textField.getText();
- out.println(text);
- textField.setText(new String(""));
- // Print received data
- try
- {
- String line = in.readLine();
- if (line.equals("mayConnect"))
- {
- System.out.println("Password was ok, we can connect! :)");
- }
- else if (line.equals("cantConnect"))
- {
- System.out.println("Password wasn't correct :(!");
- }
- }
- catch (IOException e)
- {
- System.out.println("Client: " + e);
- listenSocket();
- }
- }
- }
- public void listenSocket()
- {
- //Create socket connection
- try
- {
- socket = new Socket(ip, port);
- out = new PrintWriter(socket.getOutputStream(), true);
- in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- }
- catch (UnknownHostException e)
- {
- System.out.println("Client: Unknown host: " + ip);
- System.exit(1);
- }
- catch (IOException e)
- {
- System.out.println("Client: No I/O " + e);
- // Try to reconnect
- listenSocket();
- }
- }
- public static void main(String[] args)
- {
- try
- {
- for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
- {
- if ("Nimbus".equals(info.getName()))
- {
- UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- }
- catch (Exception e)
- {
- // Shit..
- }
- SocketClient frame = new SocketClient();
- frame.setTitle("Matim Client");
- WindowListener l = new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- };
- frame.addWindowListener(l);
- frame.pack();
- frame.setVisible(true);
- frame.listenSocket();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement