hi every body, i was working on my Client-server file hosting system and at this level my program can upload file from client to server but it is also expected to download the files previously uploaded. Now i can browse the filenames from the server with the following code,

try {
Socket Dclient = new Socket("localhost", 7777);
System.out.println("Connection is created");
in = new ObjectInputStream(Dclient.getInputStream());
String st [] = new String[80];
while( (st.length)!=-1)
{
fileName=in.readUTF();
String data[] = {fileName};
//System.out.printl(fileName);
jList1=new JList(data);
}
in.close();
Dclient.close();
}catch (Exception e) {
    e.printstacktrace();
        }

here i have two problems the first is even though all files were sent from the server only one is displayed on the Jlist(Interface problem), and secondly how can i download the content of the file name really need help.
if u need more explanation u can ask me
waiting for reply

Recommended Answers

All 10 Replies

hi every body, i was working on my Client-server file hosting system and at this level my program can upload file from client to server but it is also expected to download the files previously uploaded. Now i can browse the filenames from the server with the following code,
Client

try {
Socket Dclient = new Socket("localhost", 7777);
System.out.println("Connection is created");
in = new ObjectInputStream(Dclient.getInputStream());
String st [] = new String[80];
while( (st.length)!=-1)
{
fileName=in.readUTF();
String data[] = {fileName};
//System.out.printl(fileName);
jList1=new JList(data);
}
in.close();
Dclient.close();
}catch (Exception e) {
    e.printstacktrace();
        }

here i have two problems the first is even though all files were sent from the server only one is displayed on the Jlist(Interface problem), and secondly how can i download the content of the file name really need help.
if u need more explanation u can ask me
waiting for reply

I have now seen that the problem is no from the Jlist but its from the While loop i used to accept the file names from the server, as i have seen from the output the loop accepts allfile names as long as the server sends and when the serves finish sendind the client throws exception instead of leaving out of the loop. But i still can't figure out how to correct it can some body help me out please.......!!
here is the code for server side sendin fileName:
Server

File directory = new File("D:/Recieved/");
File filename[] = directory.listFiles();
out=new ObjectOutputStream(Dsocket.getOutputStream());
for (int i = 0; i < filename.length; i++) {
st=filename[i].toString();
out.writeUTF(st);
out.flush();
}
System.out.println("Client Connected!\nFileNames sent for the client");
out.close();

you done only reading file name...

you should read file content..like below..

BufferedOutputStream output = new
                BufferedOutputStream(new FileOutputStream(file));

than..
convert file content into bytearray(filedata)

output.write([B]filedata[/B], 0, [B]filedata[/B].length);

because socket send data as stream not string...


first you should look any sample program to send data over socket by googling it...

I have done the file download part, but my problem is when browsing the file names on a JList. As i told u before the file names can be displayed within the loops with
System.out.println(fileName);
but to display them in Jlist i need leave out of the While loop, even though my code ain't. So if u get what i mean the problem is not with downloading the file rather browsing the files on Jlist if u can please see it that way.
By the way 10Q for ur reply

can you please send your server code ..and what exception you got..

its better to get grip of it...

Server:

public void readFileName2() {
            try {
                System.out.println("Waitin Connection . . . . ");
                Dserver = new ServerSocket(7777,100);
                Dsocket = Dserver.accept();                
                File directory = new File("D:/Recieved/");
                 File filename[] = directory.listFiles();
                 out=new ObjectOutputStream(Dsocket.getOutputStream());
                 for (int i = 0; i < filename.length; i++) {
                     st=filename[i].toString();
                     out.writeUTF(st);
                     out.flush();
      }
System.out.println("Client Connected!\nFileNames sent for the client");
                out.close();
                Dsocket.close();
                Dserver.close();
            } catch (Exception ex) {
              System.out.println("can not wait" );
        }
    }

Client:

public void Display2(){
                 try {
            Dclient = new Socket("localhost", 7777);
            System.out.println("Connection is created with " + Dclient.getInetAddress());
            in = new ObjectInputStream(Dclient.getInputStream());
            String st [] = new String[20];
            while( (st.length)!=-1)
            {
            fileName=in.readUTF();
            list.add(fileName);
            //System.out.println(fileName);
            }
            jlist1.setListData(list);
            System.out.println("Done");
            in.close();
            Dclient.close();
        } catch (Exception e) {
            System.out.println("NO files from the server");
        }
    }

10Q!

you please send exception you got when running the program...

you never used while there but you used for loop there....

you try with filname.length-1 instead of filname.length..

please post your exception it may save my time because i am busy...

Ok! here is the thing, my program on the server side is supposed to read file names from the directory and send this file names to the client upon request and this works fine!
At the client side it is supposed to accept this file names as long as available and display them on JList. Here the file names are accepted succesfully but when it finishes accepting file name it throws exception

No files on a serve

instead of outputing Done , closing stream(in) & socket(DClient)
see the client side the server side work perfect

10Q again

when it finishes accepting file name it throws exception

No files on a serve

Please provide exact error message from exception, not custom message provided by you. Use printStackTrace() method

java.io.EOFException
One of the error points to line
fileName=in.readUTF();

Here is the solution to my project i finally got it
Client:

public void Display2(){
     try {
        Dclient = new Socket("192.168.9.68", 7777);
        System.out.println("Request sent for browsing file");
        in = new ObjectInputStream(Dclient.getInputStream());
        String st [] = new String[20];
        while(fileName.equals("\0")==false)
        {
        fileName=in.readUTF();
        if(fileName.equals("\0")==false)
        list.add(fileName);
        }
        MyList.setListData(list);
        System.out.println("Files On a server Opened");
        in.close();
        Dclient.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Server:

public void readFileName2() {
    try {
        System.out.println("\nWaiting For File Browse Request..... ");
        Dserver = new ServerSocket(7777,100);
        Dsocket = Dserver.accept();
        System.out.println("File Browse Request Accepted");
        File directory = new File("D:/Recieved/");
         //File filename[] = directory.listFiles();
        String filename[]=directory.list();
         out=new ObjectOutputStream(Dsocket.getOutputStream());
         for (int i = 0; i < filename.length; i++) {
             st=filename[i].toString();
             out.writeUTF(st);
             out.flush();
         }
        out.writeUTF("\0");
        System.out.println("Files Uploaded sent for the client");
        out.close();
        Dsocket.close();
        Dserver.close();
    } catch (Exception ex) {
        System.out.println("can not wait" );
    }
    }

10Q musthafa for all your cooperation

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.