Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.text.DefaultCaret;
- public class ServerGui extends JFrame{
- //Initialize components
- JPanel mainPanel = null;
- JTextArea chtArea = null;
- JScrollPane chtScroll = null;
- DefaultCaret chtScrCaret = null;
- JPanel subPanel = null;
- JTextField txtMessage = null;
- JButton btnSnd = null;
- JButton btnCls = null;
- Server s = null;
- public ServerGui() {
- super("ServerChat");
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true);
- //Create components and set properties
- mainPanel = (JPanel)getContentPane();
- mainPanel.setLayout(new BorderLayout());
- chtArea = new JTextArea();
- chtScroll = new JScrollPane(chtArea);
- chtScrCaret = (DefaultCaret)chtArea.getCaret();
- chtScrCaret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
- subPanel = new JPanel();
- subPanel.setLayout(new FlowLayout());
- txtMessage = new JTextField("");
- txtMessage.setPreferredSize(new Dimension(300,20));
- btnSnd = new JButton("Send");
- btnCls = new JButton("Close");
- btnSnd.addActionListener(new SendActionListener());
- btnCls.addActionListener(new CloseActionListener());
- btnSnd.setEnabled(false); // default false
- subPanel.add(txtMessage);
- subPanel.add(btnSnd);
- subPanel.add(btnCls);
- mainPanel.add(chtScroll, BorderLayout.CENTER);
- mainPanel.add(subPanel, BorderLayout.SOUTH);
- setSize(480,400);
- setResizable(false);
- //Creates the server
- s = new Server();
- //Keep reading the socket
- while(true) {
- chtArea.append("\nClient: "+s.readSocket()+"\n");
- }
- }
- // Buttons ActionListeners class
- public class SendActionListener implements ActionListener{
- //For button send
- @Override
- public void actionPerformed(ActionEvent e) {
- s.writeSocket(txtMessage.getText());
- chtArea.append("\nYou: "+txtMessage.getText()+"\n");
- txtMessage.setText("");
- }
- }
- public class CloseActionListener implements ActionListener{
- //For button close
- @Override
- public void actionPerformed(ActionEvent e) {
- System.exit(DISPOSE_ON_CLOSE);
- s.closeConnection();
- }
- }
- // Server class
- public class Server {
- // Initialize sockets and socket streams
- ServerSocket ss = null;
- Socket soc = null;
- DataInputStream sdis = null;
- DataOutputStream sdos = null;
- int port = 3333;
- boolean notConnected;
- //Create connection when Server is created
- public Server() {
- createConnection();
- }
- public void createConnection() {
- notConnected = true; //default false
- while(notConnected) {
- //Create server connection
- try {
- chtArea.append("Creating server... \n");
- ss = new ServerSocket(port);
- soc = ss.accept();
- Thread.sleep(2000);
- notConnected = false;
- chtArea.setText("");
- chtArea.append("Server Established! \n");
- btnSnd.setEnabled(true);
- } catch (Exception e) {
- //When failed disable send button.
- chtArea.append("Error on Connection. \n");
- btnSnd.setEnabled(false);
- try {
- chtArea.append("Trying to reconnect... \n");
- Thread.sleep(2000);
- } catch (InterruptedException e1) {
- chtArea.append("Error reconnecting. \n");
- e1.printStackTrace();
- }
- }
- }
- //Creates the socket streams
- try {
- sdis = new DataInputStream(soc.getInputStream());
- sdos = new DataOutputStream(soc.getOutputStream());
- } catch (Exception e) {
- chtArea.append("Error on creating streams. \n");
- }
- }
- //for reading data on socket streams
- public String readSocket() {
- try {
- return sdis.readUTF();
- } catch (Exception e) {
- // try to reconnect when read fails
- s = new Server();
- if(notConnected) {
- return "Error on read or connection timeout \n";
- }else {
- return "Reconnection success";
- }
- }
- }
- //for writing data on socket streams
- public String writeSocket(String ms) {
- try {
- sdos.writeUTF(ms);
- return ms;
- } catch (Exception e) {
- return "Error on write or connection timeout\n";
- }
- }
- //for closing socket connections
- public void closeConnection(){
- try {
- ss.close();
- soc.close();
- sdis.close();
- sdos.close();
- } catch (Exception e) {
- chtArea.append("Error closing connection. \n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment