this is my server code
import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.*;
class Server1
{
Socket soc;
ServerSocket sersoc;
InputStream inStream,iStream;
DataInputStream inDataStream,iDataStream;
ObjectInputStream inObjStream;
OutputStream outStream;
DataOutputStream outDataStream;
ObjectOutputStream outObjStream;
int j;
public void runServer()
{
try
{
sersoc=new ServerSocket(2000);
System.out.println("Server Started");
while(true)
{
soc=sersoc.accept();
System.out.println("Waiting to Accept Data");
getFile();
}
}
catch(Exception e)
{
System.out.println("This is error generated by function runServer()");
e.printStackTrace(System.out);
}
//soc.close();
}
public byte[] ConvertFile(
String filelocation) throws Exception
{
try
{
File f = new File(filelocation);
System.out.println("file length"+ f.length());
byte[] b = new byte[(int)(f.length())];
FileInputStream in = new FileInputStream(filelocation);
in.read(b);
return b;
}
catch(Exception e)
{
System.out.println("This is error generated by function ConvertFile()");
e.printStackTrace(System.out);
return null;
}
}
public void getFile()
{
System.out.println("get file called");
try
{
DataOutputStream fout = new DataOutputStream(soc.getOutputStream());
System.out.println(ConvertFile("c:\\test.jar"));
byte[] filebits = ConvertFile("c:\\test.jar");
fout.writeInt(filebits.length);
fout.write(filebits);
fout.flush();
}
catch(Exception e)
{
System.out.println("This is error generated by function getFile()");
e.printStackTrace(System.out);
}
}
public static void main(String[] args)
{
Server1 ms=new Server1();
ms.runServer();
System.out.println("call runserver");
}
}
and this is my client code
import java.net.*;
import java.io.*;
import java.util.*;
class Client1
{
Socket connection;
InputStream inStream;
DataInputStream inDataStream;
ObjectInputStream inObjStream;
OutputStream outStream,oStream;
DataOutputStream outDataStream,oDataStream;
public void ConvertFileBack(byte[] in, String outfile) throws Exception
{
System.out.println("***outfile***"+outfile);
FileOutputStream fos = new FileOutputStream(outfile);
fos.write(in);
fos.close();
}
public void connectServer()
{
try
{
connection = new Socket ( "10.0.0.56",2000 );
DataInputStream dis = new DataInputStream(connection.getInputStream());
byte[] fileenc = new byte[dis.readInt()];
dis.read(fileenc);
ConvertFileBack(fileenc, "C:\\test\\test.jar");
}
catch(ConnectException ce){
System.out.println( "Server is Closed\n" );
System.exit ( 1 );
}
catch ( IOException except) {
System.out.println( "Error connecting to server\n" );
except.printStackTrace ();
System.exit ( 1 );
}
catch ( Exception e) {
System.out.println( "Error " );
System.exit ( 1 );
}
}
public static void main(String[] args)
{
Client1 cs=new Client1();
cs.connectServer();
}
}
plz see if u can find any bug in the above code