//Clientimport java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.border.*;public class Client extends Frame implements ActionListener, WindowListener
{
Socket connection;
//static final String OUTPUTFILENAME = "C:\\receivedData";
final JFileChooser fc = new JFileChooser();  InputStream inStream;
 DataInputStream inDataStream;
 OutputStream outStream;
 DataOutputStream outDataStream;

public static final String DEFAULT_HOST = "DLKD122-PC7";
public static final int DEFAULT_PORT = 8901;
static String host;
static int port;

static String message;

static TextField hostDisplay, portDisplay;
static TextArea logDisplay, msgDisplay;
Panel topPanel;
Panel middlePanel;
Panel buttonPanel;
Button sendButton, sendFileButton,quitButton;   public Client ()
   {     super ( "Client" );
    buildUI ();   }  // end Client
public void connectClient ( )  
{

try 
{
InetAddress here = InetAddress.getLocalHost ();
logDisplay.appendText ( "here =  "+here+"\n" );
host = here.getHostName ();
logDisplay.appendText ( "host =  "+host+"\n" );
}
catch (UnknownHostException e) { ;}

//host = hostDisplay.getText ();
//if ( host.equals ("" ) ) 
host = DEFAULT_HOST;
if ( ! ( portDisplay.getText () ).equals ( "" ) ) 
port = Integer.parseInt ( portDisplay.getText () );
else 
port = DEFAULT_PORT;

try  
{
connection = new Socket ( InetAddress.getByName (host), port );
//connection = new Socket ( InetAddress.getByAddress (host), port );
outStream = connection.getOutputStream ();
outDataStream = new DataOutputStream ( outStream );
inStream = connection.getInputStream ();
inDataStream = new DataInputStream ( inStream );

logDisplay.setText ( "Socket created:  connecting to server\n" );

message = msgDisplay.getText ();
outDataStream.writeUTF ( message );
logDisplay.appendText ( "Message, below, sent to Server\n" );

   try 
   {
msgDisplay.setText ( "" );
msgDisplay.setForeground ( Color.red );
while ( true ) 
{
message = inDataStream.readUTF ();
msgDisplay.appendText ( message );
logDisplay.appendText ( "Message, below, received from server\n" );
     }  // end while
     }  // end try for input
catch ( EOFException except )
{
    connection.close ();
    logDisplay.appendText ( "Connection closed\n" );
    }  // end catch
   catch ( IOException except ) 
   {
System.out.println ( "IO Exception");
except.printStackTrace ();
    }  // end catch

}  // end try

catch ( IOException except)  
{
except.printStackTrace ();
}  // end catch

}  // end connectClient

public static boolean send( String filename ) 
{

    try 
{
InetAddress here = InetAddress.getLocalHost ();
logDisplay.appendText ( "here =  "+here+"\n" );
host = here.getHostName ();
logDisplay.appendText ( "host =  "+host+"\n" );
}
catch (UnknownHostException e) { ;}

//host = hostDisplay.getText ();
//if ( host.equals ("" ) ) 
host = DEFAULT_HOST;
if ( ! ( portDisplay.getText () ).equals ( "" ) ) 
port = Integer.parseInt ( portDisplay.getText () );
else port = DEFAULT_PORT;

    try
    {
        logDisplay.append("Sending data...\n");
        Socket skt = new Socket(host, port);

        //Create a file input stream and a buffered input stream.
        FileInputStream fis = new FileInputStream(filename);
        BufferedInputStream in = new BufferedInputStream(fis);
        BufferedOutputStream out = new BufferedOutputStream( skt.getOutputStream() );
       
 //System.out.println("after new inputstream");
long currTime = System.currentTimeMillis();

        //Read, and write the file to the socket
        int i;
        while ((i = in.read()) != -1) 
        {
            out.write(i);
            //System.out.println(i);
        }

        //out.write(-1); // to signal the end of file??
        //Close the socket and the file
        //System.out.println("about to read");
    
        
        //logDisplay.append(message);
        out.flush();
        out.close();
        in.close();
        //in1.close();
        //in2.close();
        skt.close();
//logDisplay.append(""+(System.currentTimeMillis()-currTime));
        return true;
    }
    catch( Exception e )
    {

        
        logDisplay.append("Error! It didn't work! " + e + "\n");

        return false;
    }
}

public static void main ( String [ ] args ) 
{

Client client = new Client ();
client.addWindowListener ( client );

}  // end main public void buildUI ()  
{

try 
{
InetAddress here = InetAddress.getLocalHost ();
host = here.getHostName ();
}
catch (UnknownHostException e) 
{ ;}

hostDisplay = new TextField ( host, 30 );
portDisplay = new TextField ( Integer.toString ( DEFAULT_PORT ), 4 );
topPanel = new Panel ();
topPanel.setLayout ( new GridLayout ( 2, 1 ) );
topPanel.add ( hostDisplay );
topPanel.add ( portDisplay );

logDisplay = new TextArea ( 40, 10 );
msgDisplay = new TextArea ( 40, 10 );
msgDisplay.setText ( "Default Message." );
middlePanel = new Panel ();
middlePanel.setLayout ( new GridLayout ( 2, 1 ) );
middlePanel.add ( logDisplay );
middlePanel.add ( msgDisplay );

sendButton = new Button ( "Send Message" );
sendFileButton = new Button ( "Send File" );
quitButton = new Button ( "Quit" );
sendButton.addActionListener ( this );
sendFileButton.addActionListener ( this );
quitButton.addActionListener ( this );
buttonPanel = new Panel ( );
buttonPanel.add ( sendFileButton );
buttonPanel.add ( sendButton );
buttonPanel.add ( quitButton );

add ( "North", topPanel );
add ( "Center", middlePanel );
add ( "South", buttonPanel );

resize ( 400, 450 );
show ();

}  // end buildUI public void actionPerformed ( ActionEvent e ) 
{
Object s = e.getSource();

// *** process Button actions

if ( s instanceof Button )  
{
    if ( s == sendButton )  
    {
        connectClient ();
    }  // end sendButton

if ( s == sendFileButton )  
    {
        int returnVal = fc.showOpenDialog(Client.this);                logDisplay.append("Opening - ");                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();                    logDisplay.append(file.getName() + "\n");                    //Create a client, give it the chosen filename, and send the data across
                    
                    if (send(file.getPath().toString()))
                        logDisplay.append("Success." + "\n");
                    else
                        logDisplay.append("Failure." + "\n");
                }     }  // end sendButton
    if ( s == quitButton )  
    {
        this.hide ();
        this.dispose ();
        System.exit ( 0 );
    }  // end quitButton

}  // end process Button actions   }  // end actionPerformed
//****  WindowListener methods

public void windowActivated ( WindowEvent e )  
{
}

public void windowDeactivated ( WindowEvent e )  
{
}

public void windowOpened ( WindowEvent e ) 
{
}

public void windowClosed ( WindowEvent e )  
{
}

public void windowClosing ( WindowEvent e )  
{
hide ();
dispose ();
System.exit(0);
}

public void windowIconified ( WindowEvent e )  
{
}

public void windowDeiconified ( WindowEvent e )  
{
}
}  // end Client