Page 3 of 3 FirstFirst 123
Results 41 to 51 of 51
  1. #41
    rav
    rav is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

  2. #42
    rav
    rav is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

    Default

    hiya, just a quick question, if to send a file to another ip add, do we change the InetAddress address = InetAddress.getByName(null) to InetAddress address = InetAddress.getAddress(null);

    Thanks

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

    Default

    i think you should change that one to
    Java Code:
                            InetAddress address = InetAddress.getLocalHost();

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

    Default

    i have a question about the same code.

    i update my code and try to send any random file path in computer. if the file i want to send is small enough, client will have it all, but if the the file is too small, then client just receive part of file i am sending.
    can you give my any suggestion? thank you

    this is my code

    server
    Java Code:
    
    import java.io.IOException;
    import java.net.*;
    import java.io.*;
    import java.lang.*;
    
    public class MftpServer {
        public static final int port = 8080;
    
        public static void main(String arg[]) throws IOException {
                    String line;
                    ServerSocket p;
                    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);
                            int nbrRd = in.read(buffer);
                            line = String.valueOf(nbrRd);
                            System.out.println("Data from client: \n" + "> " + new String(buffer, 0, nbrRd) + "\n <");
                        } 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");
                    }
                }
            });
        }
    and this is client code

    Java Code:
    
    import java.io.IOException;
    
    import java.net.*;
    import java.io.*;
    import java.util.Scanner;
    
    public class MftpClient {
    
        public static void main(String arg[]) throws IOException {
                      try {
                            InetAddress address = InetAddress.getLocalHost();
                            System.out.println("Address = " + address);
                            Socket socket = new Socket(address, 8080);
    
                            String path;
                            int length;
    
                            Scanner input = new Scanner(System.in);
                            System.out.println("Please enter file path you want to send: ");
                            path = input.nextLine();
                            String source = path;
    
                            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);
                                }
                            } 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();
                    }
                 }
            });
        }
    }
    thank you again

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

    Default

    this is the original file:

    Java Code:
    import java.net.*;
    import java.io.*;
    import java.util.Scanner;
    
    public class Client {
    
        public static void main(String arg[]) throws IOException {
            try {
                InetAddress address = InetAddress.getByName(null);
                System.out.println("Address = " + address);
                Socket socket = new Socket(address, 8080);
    
                String path;
                int length;
    
                Scanner input = new Scanner(System.in);
                System.out.println("Please enter file path you want to send: ");
                path = input.nextLine();
                String source = path;
                
                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);
                    }
                } 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();
            }
        }
    }
    and this is the result after i ran my program

    Java Code:
    "C:\Program Files\Java\jdk1.6.0_20\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:52283,suspend=y,server=n -Dfile.encoding=windows-1252 -classpath "C:\Program Files\Java\jdk1.6.0_20\jre\lib\alt-rt.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\json.jar;C:\Users\Chien Le\Research\2010-05 MMPong\out\production\MMPong;C:\Users\Chien Le\Research\2010-05 MMPong\out\production\jstyx-0.2.0;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\mina-0.8.0.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\log4j-1.2.8.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\dom4j-1.6.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\jaxen-1.1-beta-5.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\JSAP-2.0b.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\slf4j-log4j12-1.0-beta9.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\TableLayout.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\quartz-1.5.1.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\commons-logging-1.0.4.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 9.0.2\lib\idea_rt.jar" edu.wustl.cse.mobilab.mftp.MftpServer
    Connected to the target VM, address: '127.0.0.1:52283', transport: 'socket'
    Server Started:  ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8080]
    Connection accepted: Socket[addr=/172.18.3.8,port=52287,localport=8080]
    Data from client: 
    > import java.net.*;
    import java.io.*;
    import java.util.Scanner;
    
    public class Client {
    
        public static void main(String arg[]) throws IOException {
            try {
                InetAddress address = InetAddress.getByName(null);
                System.out.println("Address = " + address);
                Socket socket = new Socket(address, 8080);
    
                String path;
                int length;
    
                Scanner input = new Scanner(System.in);
                System.out.println("Please enter file path you want to send: ");
                path = input.nextLine();
                String source = path;
                
                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);
                    }
                } catch (Exception x) {
                    x.printStack
     <
    Socket is closing
    Server socket is closing
    so it stopped at "x.printStack" and it closed socket, i dont know how to fix this problem.
    i think this is caused because of the limitation of transfer data between server and client. so i think i could cut down file into different packets and send it from server to client, but i dont know where i need to start.
    thank you

  6. #46
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    How many bytes were read by the server?
    How many bytes were written by the client?

    You only do one read in the Server? Why not loop to read all of the bytes sent?

    Have you tried sending a shorter file. Say one less than 1000 bytes?

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

    Default

    i tried sending shorter file (which <= 1000 bytes) and it worked prefect.
    but if it is bigger than 1000 bytes, then server only display 1000 bytes data in the file and then it will close the socket, the rest data after 1000 bytes will not display on server side.

  8. #48
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    Do you think the following has anything to do with your problem?

    byte[] buffer = new byte[1000];

    You only do one read in the Server? Why not loop to read all of the bytes sent?

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

    Default

    if i change value of 1000 to bigger value, it will receive more data in file
    what if the file is too large cause us cant write value into new byte?

  10. #50
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    Why not use a loop to read the bytes sent little by little?

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

Page 3 of 3 FirstFirst 123

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
  •