20 Ekim 2012 Cumartesi

Client - Server



Server.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package talk;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

/**
 *
 * @author Onur
 */
public class Server extends JFrame{

    private JTextField enterField;
    private JTextArea displayArea;
    private ObjectOutputStream output;
    private ObjectInputStream input;
    private ServerSocket server;
    private Socket connection;
    private int counter;


    public Server()
    {
        super("Server");
        enterField = new JTextField();
        enterField.setEditable(true);
        enterField.addActionListener(

                new ActionListener() {

            public void actionPerformed(ActionEvent event) {
              sendData(event.getActionCommand());
              enterField.setText("");
            }
        }
                );

                add(enterField, BorderLayout.NORTH);

                displayArea = new JTextArea();
                add(new JScrollPane(displayArea), BorderLayout.CENTER);

                setSize(300,150);
                setVisible(true);  
    }

    public void runServer()
    {
        try {
            server = new ServerSocket(12345, 100);

            while(true)
            {
                try {
                    waitForConnection();
                    getStreams();
                    processConnection();
                }
                catch (EOFException eofException)
                {
                    displayMessage("\n Server terminated message");
                }
                finally
                {
                    closeConnection();
                    ++counter;
                }
            }
        }
        catch (IOException ioException)
        {
            ioException.printStackTrace();
        }
    }

    private void waitForConnection() throws IOException
    {

        displayMessage("Waitinf for connection");
        connection = server.accept();
        displayMessage("Connection " + counter + " received from " + connection.getInetAddress().getHostName());
    }

    private void getStreams() throws IOException
    {
        output = new ObjectOutputStream(connection.getOutputStream());
        output.flush();

        input = new ObjectInputStream(connection.getInputStream());

        displayMessage(" got i/o streams");
    }

    private void processConnection() throws IOException
    {
        String message = "Connection successful";
        sendData(message);

        setTextFieldEditable(true);

        do
        {
            try {
                 message = (String) input.readObject();
                displayMessage("\n" + message);
            }
            catch (ClassNotFoundException classNotFoundException)
            {
                displayMessage("Unknow object type received");
            }
        }while(!message.equals("Client >>> Terminate"));
    }

    private void closeConnection()
    {
        displayMessage("Terminatind connection");
        setTextFieldEditable(true);

        try {
            output.close();
            input.close();
            connection.close();

        } catch (IOException ioException)
        {
            ioException.printStackTrace();
        }
    }

    private void displayMessage(final String messageToDisplay)
    {
        SwingUtilities.invokeLater(
             
                new Runnable() {

            public void run() {
              displayArea.append(messageToDisplay);
            }
        }
                );
    }

    private void sendData(String message)
    {
        try {
           output.writeObject("Server >>> " + message);
           output.flush();
           displayMessage("\n Server >>> " + message);

        } catch (IOException ioException)
        {
            displayArea.append("\n error waiting object");
        }
    }

    private void setTextFieldEditable(final boolean editable) {

         SwingUtilities.invokeLater(

                new Runnable() {

            public void run() {
              enterField.setEnabled(editable);
            }
        }
                );
    }

    public static void main(String args[])
    {
        Server application = new Server();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.runServer();
    }

}

Client.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package talk;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
/**
 *
 * @author Onur
 */
public class Client extends JFrame {

    private JTextField enterField;
    private JTextArea displayArea;
    private ObjectOutputStream output;
    private ObjectInputStream input;
    private String message = "";
    private String chatServer;
    private Socket client;
   
    public Client(String host)
    {
        super("Client");

        chatServer = host;

        enterField = new JTextField();
        enterField.setEditable(true);
        enterField.addActionListener(

                new ActionListener() {

            public void actionPerformed(ActionEvent event) {
              sendData(event.getActionCommand());
              enterField.setText("");
            }
        }
                );

                add(enterField, BorderLayout.NORTH);

                displayArea = new JTextArea();
                add(new JScrollPane(displayArea), BorderLayout.CENTER);

                setSize(300,150);
                setVisible(true);
    }

    public void runClient()
    {
  
           try
           {
                 connectToServer();
                    getStreams();
                    processConnection();
                }
                catch (EOFException eofException)
                {
                    displayMessage("\n Client terminated message");
                }
                catch (IOException ioException)
                 {
                   ioException.printStackTrace();
                  }
                finally
                {
                    closeConnection();                  
                }
                 
    }

    private void getStreams() throws IOException
    {
        output = new ObjectOutputStream(client.getOutputStream());
        output.flush();

        input = new ObjectInputStream(client.getInputStream());

        displayMessage(" got i/o streams");
    }

    private void processConnection() throws IOException
    {
       
        setTextFieldEditable(true);

        do
        {
            try {
                 message = (String) input.readObject();
                displayMessage("\n" + message);
            }
            catch (ClassNotFoundException classNotFoundException)
            {
                displayMessage("Unknow object type received");
            }
        }while(!message.equals("Server >>> Terminate"));
    }

    private void closeConnection()
    {
        displayMessage("Closing connection");
        setTextFieldEditable(true);

        try {
            output.close();
            input.close();
            client.close();

        } catch (IOException ioException)
        {
            ioException.printStackTrace();
        }
    }

    private void displayMessage(final String messageToDisplay)
    {
        SwingUtilities.invokeLater(

                new Runnable() {

            public void run() {
              displayArea.append(messageToDisplay);
            }
        }
                );
    }

    private void sendData(String message)
    {
        try {
           output.writeObject("Client >>> " + message);
           output.flush();
           displayMessage("\n Client >>> " + message);

        } catch (IOException ioException)
        {
            displayArea.append("\n error waiting object");
        }
    }

    private void setTextFieldEditable(final boolean editable) {

         SwingUtilities.invokeLater(

                new Runnable() {

            public void run() {
              enterField.setEnabled(editable);
            }
        }
                );
    }

    private void connectToServer() throws IOException
    {
         displayMessage("Attempting connection");
         client = new Socket(InetAddress.getByName(chatServer),12345);
         displayMessage("Connected to " + client.getInetAddress().getHostName());
    }

    public static void main(String args[])
    {
        Client application;

        if(args.length == 0)
        {
            application = new Client("127.0.0.1");
        }
        else
        {
            application = new Client(args[0]);
        }

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.runClient();
    }

}

Hiç yorum yok:

Yorum Gönder