Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 51
  1. #21
    kaijeong is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

    i run program again and it works
    but instead of writing down msg in test.txt file it just print out -1
    Last edited by kaijeong; 07-19-2010 at 10:08 PM.

  2. #22
    kaijeong is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

    is there any way we can download file from client to server?
    thks

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Yes, your code shown in post#15 does send the data from the client to the server. See my post #20

  4. #24
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hi kaijeong

    I've written a file transfer framework for a client/server architecture using object serialization and I came across a serious problem. I chose object serialization to simplify the implementation. It also allowed me to communicate additional data to allow users to monitor download/upload progress and control when downloads should cancel. The problem was scalability. It turned out that the deserialization API has a bug that causes the StreamCorruptedException to be thrown. Strangely, I only had the problem when transmitting more than 10MB. Anything less was okay. Very weird. So the fix I came up with, was to reset the connection every 10 MB and continue the downloading/uploading process. ;) I'd just like to thank hardwired again for helping. :)

    Just thought I'd share
    Tim
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #25
    kaijeong is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Yes, your code shown in post#15 does send the data from the client to the server. See my post #20
    thank you for all of ur help
    my program is running, but instead of give real msg it has in test.txt file from client, it gave me -1? did i do something wrong?

    this is what it gave me

    Java Code:
    Started:  ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8080]
    Connection accepted: Socket[addr=/127.0.0.1,port=49206,localport=8080]
    -1
    socket is closing
    Server socket is closing
    so instead of giving "test" (what i have in test.txt file_ it gave me -1? i just not really get it

    im sorry for asking too much, this is really new to me and i m kind of confuse
    thank you again for all of ppl help, i really appreciate that

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    What method returned the -1? What is the reason the method returns a -1?

    Why do you expect the method to return the contents of the text file?
    Have you read the API doc for the methods you are using?

  7. #27
    kaijeong is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

    i think
    Java Code:
     while (line != null) {
                        System.out.println(line);
                        
                    }
    will give the -1

    i expect that server will download file from client (test.txt in this case) and save it to wherever server want and then server can open the file and see what content in that file.

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    while (line != null) {
    System.out.println(line);

    }
    Where is there a method in that code that assigns a value to line?????

    What method returned the -1? What is the reason the method returns a -1?

    Have you read the API doc for the I/O methods you are using?
    Last edited by Norm; 07-20-2010 at 02:13 AM.

  9. #29
    kaijeong is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

    so i spend whole night to look for right doc, i dont know its right or not but i were readig typical uses of io streams methods
    just wondering if that is right doc im reading?

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I don't know what doc you were reading. It does not sound like the right one.
    The one I was suggesting is called the java api doc. If you use google with 'java api doc' you will get this:
    Java Platform SE 6

  11. #31
    kaijeong is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

    i just rewrite my code, would like to know ur opinion,
    thank you

    client
    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class Client {
    
        public static void main(String arg[]) throws IOException {
    
            int length;
    
            InetAddress address = InetAddress.getLocalHost();
            System.out.println("Address = " + address);
            Socket socket = new Socket(address, 8080);
    
            String source = "C:\\Users\\Chien Le\\Desktop\\test.txt";
            FileInputStream in = new FileInputStream(source);
            byte[] buf = in.toString().getBytes();
            OutputStream out = new BufferedOutputStream(socket.getOutputStream());
    
            try {
                while ((length = in.read(buf)) > 0) {
                    out.write(buf, 0, length);
                }
            }
            finally {
                out.flush();
                out.close();
                in.close();
                System.out.println("socket is closing");
                socket.close();
            }
        }
    }
    server
    Java Code:
    import java.net.*;
    import java.io.*;
    import java.lang.*;
    
    
    public class Server {
        public static final int port = 8080;
    
        public static void main(String arg[]) throws IOException {
    
            StringBuffer buf = new StringBuffer();
            ServerSocket p = new ServerSocket(port);
            System.out.println("Started:  " + p);
            byte[] buffer = new byte[1000];
    
            try {
                Socket socket = p.accept();
                System.out.println("Connection: " + socket);
                //copy file  code here
                try {
                    //write  the data
                    DataOutputStream out2 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("C:\\Users\\Chien Le\\Desktop\\text.txt")));
                    InputStream bis = new BufferedInputStream(socket.getInputStream());
                    DataInputStream in = new DataInputStream(new BufferedInputStream(bis));
                    out2.writeBytes(String.valueOf(in.read()));
                    out2.close();
                    //recover data
                    DataInputStream in5 = new DataInputStream(new BufferedInputStream(new FileInputStream("C:\\Users\\Chien Le\\Desktop\\text.txt")));
                    System.out.println(in5.readLine());
                } catch (EOFException e){
                    System.out.println("End of stream encoutered");}
    
                finally {
                    socket.close();
                    System.out.println("socket is closing");
                }
            }
            finally {
                p.close();
                System.out.println("Server socket is closing");
            }
        }
    }
    thank you

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Does it do what you want?

    What is the following statement supposed to do?
    out2.writeBytes(String.valueOf(in.read()));

    What does in.read() return?
    What does valueOf() do to what's returned?

    I rewrote your code to this:
    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class ClientServerProblem3 {
    
      public  final int port = 8080;
    
    
      public static void main(String[] args) {
         new ClientServerProblem3();
      }
    
      public ClientServerProblem3() {
         Thread t1 = new Thread(new Runnable() {
           public void run() {
             new Server();
           }
         });
         t1.start();
    
         Thread t2 = new Thread(new Runnable() {
           public void run() {
             new Client();
           }
         });
         t2.start();
    
      }
    
      //--------------------------------------------------------------------
     class Client {
    
        public Client() {
          try {
            InetAddress address = InetAddress.getByName(null);
            System.out.println("Address = " + address);
            Socket socket = new Socket(address, port);
    
            String source = "CS_test.txt";
            int length;
    
            //InputStream in = new BufferedInputStream(new FileInputStream(source));
            FileInputStream in = new FileInputStream(source);
            byte[] buf = in.toString().getBytes();
            OutputStream out = new BufferedOutputStream(socket.getOutputStream());
    
            try {
                while ((length = in.read(buf)) > 0) {
                    System.out.println("writing len=" + length);
                    out.write(buf, 0, length);
                }
            } catch(Exception x) {
               x.printStackTrace(); 
            }finally {
                out.flush();
                out.close();
                in.close();
                System.out.println("client socket is closing");
                socket.close();
            }
           } catch(Exception x) {
              x.printStackTrace(); 
           }
        }
    }
    
    //@SuppressWarnings({"ALL"})
     class Server {
    
        Server() {
            String line;
            StringBuffer buf = new StringBuffer();
            ServerSocket p = null;
            try {
               p = new ServerSocket(port);
               System.out.println("Server Started:  " + p);
               byte[] buffer = new byte[1000];
    
                Socket socket = p.accept();
                System.out.println("Connection accepted: " + socket);
                //copy file  code here
                try {
                    BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
                    DataInputStream in = new DataInputStream(bis);
                    System.out.println("available=" + in.available());
                    int nbrRd = in.read(buffer);
                    line = String.valueOf(nbrRd);
                    System.out.println("line=" + line);
                    System.out.println("buffer=" + new String(buffer, 0, nbrRd) + "<");
    
                    BufferedReader tin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    
                    while ((line = tin.readLine()) != null) {
                        System.out.println("line=" + line + "<");
                    }
                } catch(Exception x) {
                   x.printStackTrace(); 
    
                }
                finally {
                    socket.close();
                    System.out.println("socket is closing");
                }
            } catch(Exception x) {
               x.printStackTrace(); 
            }
            finally {
    //            p.close();
                System.out.println("Server socket is closing");
            }
        }
     }}
    /*
    Running: "C:\Program Files\Java\j2re1.4.2_08\bin\java.exe" -cp D:\JavaDevelopment;.;acm.jar ClientServerProblem3
    
    Address = localhost/127.0.0.1
    Server Started:  ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8080]
    Connection accepted: Socket[addr=/127.0.0.1,port=4474,localport=8080]
    available=0
    writing len=31
    writing len=25
    line=56
    buffer=This is a test file
    with more than one line
    last line.<
    socket is closing
    Server socket is closing
    client socket is closing
    
    0 error(s)
    
    */
    Last edited by Norm; 07-20-2010 at 04:23 PM.

  13. #33
    kaijeong is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

    out2.writeBytes(String.valueOf(in.read()));

    that will write to file text.txt file whatever data transfer ( i think)

    i still cant get right information from client, still reading doc for right code

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    that will write to file text.txt file whatever data transfer ( i think)
    the i think means you are guessing. Read the API doc for the read() method. What does it return? Don't guess, read the doc!!!

    You ignored what valueOf() does.

  15. #35
    kaijeong is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

    would you mind if i ask what do u have in CS_test.txt?
    thks u

  16. #36
    kaijeong is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

    ty i got it
    i need to read doc again and learn lots
    ty for ur helping

  17. #37
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Exclamation

    Yummy... you getInputStream() twice :D

    BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
    DataInputStream in = new DataInputStream(bis);
    line = String.valueOf(in.read(buffer));
    BufferedReader tin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    Modify you server code more correctly please

  18. #38
    kaijeong is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

    oh yeh, i just looked at it again,
    i need to take this piece of code out
    Java Code:
     BufferedReader tin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    
                    while ((line = tin.readLine()) != null) {
                        System.out.println("line=" + line + "<");
                    }
    thank you again for all ur helps
    i will update code later on if i have changed sth
    thank you

  19. #39
    rav
    rav is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

    Default

    what does CS_test.txt contain

  20. #40
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    See these lines printed out by the program. They show the contents of the file.
    buffer=This is a test file
    with more than one line
    last line.<
    The above was printed by:
    System.out.println("buffer=" + new String(buffer, 0, nbrRd) + "<");

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 04-05-2010, 02:19 AM
  2. Sending Text File --- Server-To-Client
    By nigamsir in forum Networking
    Replies: 1
    Last Post: 03-08-2010, 03:45 PM
  3. Replies: 8
    Last Post: 02-18-2010, 12:54 PM
  4. Replies: 1
    Last Post: 02-10-2009, 09:16 AM
  5. sending jar files from client to server?
    By gobinathm in forum New To Java
    Replies: 2
    Last Post: 11-13-2007, 05:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •