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

    Default Sending file from client to server

    hi everybody,
    i am new to java so i would like to ask for help from ppl have more exp than me
    im trying to send file from client to server and vice versus, currently im sending test.txt file from client to server but it not work, can anyone help me pls?

    this is my code

    client code
    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class Client {
    
        public static void main(String arg[]) throws IOException {
    
            InetAddress address = InetAddress.getByName(null);
            System.out.println("Address = " + address);
            Socket socket = new Socket(address, 8080);
    
            String source = "C:\\test.txt";
            byte[] buf = new byte[1000];
            int length;
    
            InputStream in = new BufferedInputStream(new FileInputStream(source));
            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();
            }
        }
    }
    and server code

    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 {
    
            String line;
            StringBuffer buf = new StringBuffer();
            ServerSocket p = new ServerSocket(port);
            System.out.println("Started:  " + p);
            byte[] buffer = new byte[10];
    
            try {
                Socket socket = p.accept();
                System.out.println("Connection accepted: " + socket);
                //copy file  code here
                try {
                    BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
                    line = String.valueOf(in.read());
                    while(true)//copy the file
                    {
                        buf.append(line + "\n");
                    }
                }
                finally {
                    socket.close();
                    System.out.println("socket is closing");
                }
            }
            finally {
                p.close();
                System.out.println("Server socket is closing");
            }
        }
    }
    it not print out what is on test.txt file for server side and i dont know where i got wrong
    thank you in advance

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    where do you fill this client array?

    byte[] buf = new byte[1000];

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

    Default

    i thought that is the rate of transfer data through server? doesnt it right?
    thanks

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Quote Originally Posted by kaijeong View Post
    i thought that is the rate of transfer data through server? doesnt it right?
    thanks
    No :)

    the array must be filled with bytes ;)
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

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

    Default

    so can i change to this?

    final static int BUFFER_SIZE = 1000;
    byte[] buf = new byte [BUFFER_SIZE];

    and then

    DatagramPacket dp = new DatagramPacket (buf, buf.length);


    thnks
    Last edited by kaijeong; 07-19-2010 at 07:55 PM.

  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Simply :)

    FileInputStream in=new FileInputStream(source));
    byte [] buf=in.toString().getBytes();

    for more information I recommend reading this String (Java 2 Platform SE 5.0))
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

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

    Default

    thank you very much for your help, after changing , i run the program but it still not print out test.txt file on the server side. is that because the program not read into
    Java Code:
     BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
                    line = String.valueOf(in.read());
    on server side?

    i thought that it supports to print out

    test ( this is what i wrote in test.txt file)

    on server side but after connection accepted, it just stands there and does nothing.
    thks

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

    Default

    Quote Originally Posted by kaijeong View Post
    thank you very much for your help, after changing , i run the program but it still not print out test.txt file on the server side. is that because the program not read into
    Java Code:
     BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
                    line = String.valueOf(in.read());
    on server side?
    thks
    I think you have to read each stream line on your server side :confused:

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

    Default

    There are working code samples on this forum that you should look at besides reading the API doc and Tutorial. Do a search for Socket and getInputStream for examples.

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

    Default

    thank you for helping
    i have question in the same topic, " is there any way that we can download test.txt file from client ?"

    thanks

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

    Lightbulb

    As a rule, to read Input Streams one should use while loop :confused:


    Java Code:
    BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    
    
    String line;
    while((line=in.readLine())!=null)
    {
    
    System.out.println(line);
    }
    I think it is useful

  12. #12
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by gafa View Post
    As a rule, to read Input Streams one should use while loop :confused:


    Java Code:
    BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    
    
    String line;
    while((line=in.readLine())!=null)
    {
    
    System.out.println(line);
    }
    I think it is useful
    @kaijeong

    As you may notice, gafa is proposing to modify you server code in this way :)

    BufferedReader is a good way to read text files because it represents readLine() method to read text files line by line... Much useful
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

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

    Default

    thk you for helping,
    i tried rafa method to read line in text file but it not print out the result in the console.
    this is what i get for server

    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=52763,localport=8080]
    (support to have msg of test.txt file here, but it not show)
    socket is closing
    Server socket is closing

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

    Default

    Show us the client and server modified code

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

    Default

    client code
    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class Client {
    
        public static void main(String arg[]) throws IOException {
    
            InetAddress address = InetAddress.getByName(null);
            System.out.println("Address = " + address);
            Socket socket = new Socket(address, 8080);
    
            String source = "C:\\test.txt";
            //byte[] buf = new byte[1000];
            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) {
                    out.write(buf, 0, length);
                }
            }
            finally {
                out.flush();
                out.close();
                in.close();
                System.out.println("socket is closing");
                socket.close();
            }
        }
    }
    server code

    Java Code:
    import java.net.*;
    import java.io.*;
    import java.lang.*;
    
    @SuppressWarnings({"ALL"})
    public class Server {
        public static final int port = 8080;
    
        public static void main(String arg[]) throws IOException {
    
            String line;
            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 accepted: " + socket);
                //copy file  code here
                try {
                    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()));
                    
                    while ((line = tin.readLine()) != null) {
                        System.out.println(line);
                    }
                }
                finally {
                    socket.close();
                    System.out.println("socket is closing");
                }
            }
            finally {
                p.close();
                System.out.println("Server socket is closing");
            }
        }
    }
    Last edited by kaijeong; 07-19-2010 at 09:32 PM.

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

    Default

    YOU need to do some debugging. You are making too many assumptions about what is happening. For example:
    Java Code:
      line = String.valueOf(in.read());
    Instead of assuming that the byte read was OK, read the byte into a variable and display it and then display what is in the line variable.

    Also show if any bytes are available by calling the available() method.

    And what about exceptions? You should catch them and show an error message.

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

    Lightbulb

    I think you should modify your client in this way

    Java Code:
    InetAddress address = InetAddress.getLocalHost( )
    because you test it on the same machine I guess :confused:

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

    Default

    Quote Originally Posted by gafa View Post
    As a rule, to read Input Streams one should use while loop :confused:


    Java Code:
    BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    
    
    String line;
    while((line=in.readLine())!=null)
    {
    
    System.out.println(line);
    }
    I think it is useful
    i use this method but it give me error at line=in.readLine())!=null

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

    Default

    getLocalHost( ) ? I cannot see it in your client code :(

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

    Default

    I just compiled and executed the above program from post#15. The bytes sent by the client are read by server OK. They are in the array: buffer.
    in.read(buffer)
    You need to read the API doc to see what the read() method does.

Page 1 of 3 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
  •