I made some changes in it, here is code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.*;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.Date;
public class MultiThreadChatClient13 extends JFrame implements Runnable,ActionListener {
private static JButton openB,cancelB,sendB;
private static JLabel fileL,stL,bsL,etL,stlbl,bslbl,etlbl,selectL,sendingL;
private static JFileChooser dialog;
private static Socket clientSocket = null;
private DataInputStream in = null;
private DataOutputStream out = null;
private FileInputStream fis = null;
private
String host="192.168.1.102";
public static void main(String[] args)throws IOException {
// The default port.
int portNumber = 2222;
// The default host.
String host = "192.168.1.102";
if (args.length < 2)
{
System.out
.println("Usage:
java MultiThreadChatClient <host> <portNumber>\n"
+ "Now using host=" + host + ", portNumber=" + portNumber);
}
else
{
host = args[0];
portNumber = Integer.valueOf(args[1]).intValue();
}
/*
* Open a socket on a given host and port. Open input and output streams.
*/
// if (clientSocket != null && out != null && in != null)
// {
try {
clientSocket = new Socket(host, portNumber);
new Thread(new MultiThreadChatClient13()).start();
}
catch (Exception e)
{
System.err.println("Exception123: " + e);
}
// }
/* else
{
System.out.println("In Else part...");
}*/
}
public void run()
{
try
{
setSize(400,330);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
selectL=new JLabel("Select File:");
selectL.setBounds(10,10,100,30);
openB=new JButton("Open");
openB.setToolTipText("Open File");
openB.setBounds(120,10,80,30);
fileL=new JLabel("No File Selected");
fileL.setBounds(20,60,350,30);
sendB=new JButton("Send");
sendB.setToolTipText("Send File");
sendB.setBounds(30,100,80,30);
sendB.setEnabled(false);
cancelB=new JButton("Cancel");
cancelB.setToolTipText("Cancel File Selection");
cancelB.setBounds(120,100,80,30);
stlbl=new JLabel("0");
stlbl.setBounds(190,140,200,30);
bslbl=new JLabel("0Bytes");
bslbl.setBounds(190,180,200,30);
etlbl=new JLabel("0");
etlbl.setBounds(190,220,200,30);
stL=new JLabel("Initiation Time: ");
stL.setBounds(30,140,150,30);
bsL=new JLabel("No of Bytes Send: ");
bsL.setBounds(30,180,150,30);
etL=new JLabel("Complition Time: ");
etL.setBounds(30,220,150,30);
sendingL=new JLabel(" ");
sendingL.setBounds(30,260,150,30);
openB.addActionListener(this);
cancelB.addActionListener(this);
sendB.addActionListener(this);
add(selectL);add(openB);
add(fileL);
add(sendB);add(cancelB);
add(stL);add(stlbl);
add(bsL);add(bslbl);
add(etL);add(etlbl);add(sendingL);
show();
}
catch(Exception e)
{
System.out.println("Exception here....");
}
// sendFileToServer(out);
}
public void actionPerformed(ActionEvent ae)
{
try
{
if(ae.getSource()==openB)
{
dialog=new JFileChooser();
dialog.showOpenDialog(this);
fileL.setText(""+dialog.getSelectedFile());
sendB.setEnabled(true);
}
if(ae.getSource()==cancelB)
{
try
{ // clientSocket.close();
}catch(Exception e){System.out.println(e);}
fileL.setText("No File Selected");
stlbl.setText("");
etlbl.setText("");
bslbl.setText("");
sendingL.setText("");
sendB.setEnabled(false);
}
if(ae.getSource()==sendB)
{
try
{
try
{
in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
out = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host " + host);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to the host "
+ host);
}
sendFileToServer(out);
}
catch(Exception e)
{
System.out.println("Exception in send button code");
}
}
}
catch(Exception e)
{
System.out.println("Exception in action performed.");
}
}
public void sendFileToServer(DataOutputStream out)
{
try
{
stlbl.setText(" "+new Date());
int totsize=0;
int len=0;
String fname=dialog.getSelectedFile().getName();
System.out.println("client : file name: " +fname);
// String suffix=fname.substring(fname.lastIndexOf('/')+1,fname.length()).trim();
out.writeUTF(fname);
out.flush();
int fileLong = new Integer((int)dialog.getSelectedFile().length());
System.out.println("Size:"+fileLong);
byte[] buf=new byte[12837696];
System.out.println("Size1:"+fileLong);
out.writeUTF("get");
out.writeInt(fileLong);
out.flush();
System.out.println("File name send");
fis=new FileInputStream(dialog.getSelectedFile());
System.out.println("File Open");
while((len=fis.read(buf))!=-1)
{
totsize+=len;
//System.out.println(buf);
bslbl.setText(""+totsize);
out.write(buf,0,len);
}
etlbl.setText(""+new Date());
fis.close();
out.close();
in.close();
clientSocket.close();
DecimalFormat df= new DecimalFormat("#0.00 KB");
bslbl.setText(" "+df.format(totsize/1024d));
}
catch (IOException e)
{
System.out.println("Exception in sendFile Server");
e.printStackTrace();
}
}
}// Java Document
But now it shows the exception like:
Exception123: java.net.ConnectException: Connection timed out: connect
the client is at my end and server code is on another end.what is real issue regarding this?