import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.*;

public class ClientServerChat {
        
    public static final int PORT = 8000;

    public static void passMessage(DataOutputStream toSend,
                                   JTextField message, JTextArea myChatWindow)
    {
        try
        {
            String myLine  = message.getText() + '\n';
            toSend.writeChars(myLine);
            toSend.flush();
            message.setText("");
            myChatWindow.append("I say: " + myLine);
        }       
        catch (IOException ex) {
            System.err.println(ex.toString() + '\n');
        }
         
    }
        
    public static void setupFrame(String title, JTextField message,
                                  JTextArea chatwindow)
    {
        JPanel p = new JPanel();
        p.setLayout(new BorderLayout());
        p.add(new Label("Enter message: "), BorderLayout.WEST);
        p.add(message, BorderLayout.CENTER);
        JFrame frame = new JFrame(title);
        frame.getContentPane().add(p, BorderLayout.NORTH);
        frame.getContentPane().add(new JScrollPane(chatwindow), BorderLayout.CENTER);
                
        frame.setSize(500, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
        
    public static void keepReadingMessages(DataInputStream from, 
                                           JTextArea chatwindow)
    {
        while (true)
        {
            String message = readLine(from);
            chatwindow.append("You say: " + message);
        }
    }
        
    public static String readLine(DataInputStream is)
    {
        String s = "";
        char c='0';
        try 
        {
            while ((int)c != 10)
            {
                c = is.readChar();
                s += c;
            }
        }
        catch (IOException ex)
        {
                        
        }
        return s;
    }
        
    public static void printMyIPAddress( ) throws IOException
            
    {
        InetAddress inetAddress = InetAddress.getLocalHost( );
        System.out.println("client's host name is " +
                           inetAddress.getHostName());
        System.out.println("client's host name is " +
                           inetAddress.getHostAddress());
                    
    }
    public static void main(String[] args) throws IOException
    {
        printMyIPAddress();
    }
         
}
