file uploading & downloading between server-client

30,498

I looked at the code and at first glance there seemed nothing wrong with it. So, I copied it as it was, changed the file used in the server code to a file that existed on my system and ran the code. It worked just fine, so at least I can assure you that the code is correct and it does what it's supposed to do. I ran my code on an Ubuntu machine, but I'm not sure how that can affect the outcome.

Just a few pointers that my help: 1) Are you running the TCPServer file and then the TCPClient? (Dumb, I know, but you never know) 2) Is there a process that might be using port 3248? 3) Does the process running the files have permission to read/write the paths specified? 4) Does the file specified in TCPServer actually exist? 5) Did you try running the class files outside of the IDE - either way it's good to learn how to be independent of the IDE.

Hope this was helpful and good luck with your assignment.

Share:
30,498
el nino
Author by

el nino

Updated on August 17, 2020

Comments

  • el nino
    el nino over 3 years

    I have an assignment to create a client-server file transfer application. That could be a simple example. I tried the examples given in similar questions in SOF, but they were unable to transfer the file.

    I'm trying to communicate the client and the server via sockets. If there could be someone who could help me, I'll be glad.

    The client will upload the file to the server.Also the client could download the file from server.That's how I would create the application.

    Here is the client side code:

    package wdc;
    
    import java.io.*;
    import java.io.ByteArrayOutputStream;
    import java.net.*;
    
    class TCPClient {
    
        public static void main(String args[]) {
            byte[] aByte = new byte[1];
            int bytesRead;
    
            Socket clientSocket = null;
            InputStream is = null;
    
            try {
                clientSocket = new Socket("127.0.0.1", 3248);
                is = clientSocket.getInputStream();
            } catch (IOException ex) {
                // Do exception handling
            }
    
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
            if (is != null) {
    
                FileOutputStream fos = null;
                BufferedOutputStream bos = null;
                try {
                    fos = new FileOutputStream("C:\\testout.pdf");
                    bos = new BufferedOutputStream(fos);
                    bytesRead = is.read(aByte, 0, aByte.length);
    
                    do {
                            baos.write(aByte);
                            bytesRead = is.read(aByte);
                    } while (bytesRead != -1);
    
                    bos.write(baos.toByteArray());
                    bos.flush();
                    bos.close();
                    clientSocket.close();
                } catch (IOException ex) {
                    // Do exception handling
                }
            }
        }
    }
    

    Here is the server side code:

    package wds;
    
    import java.io.*;
    import java.net.*;
    
    class TCPServer {
    
        public static void main(String args[]) {
    
            while (true) {
                ServerSocket welcomeSocket = null;
                Socket connectionSocket = null;
                BufferedOutputStream outToClient = null;
    
                try {
                    welcomeSocket = new ServerSocket(3248);
                    connectionSocket = welcomeSocket.accept();
                    outToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
                } catch (IOException ex) {
                    // Do exception handling
                }
    
                if (outToClient != null) {
                    File myFile = new File("C:\\testserver.pdf");
                    byte[] mybytearray = new byte[(int) myFile.length()];
    
                    FileInputStream fis = null;
    
                    try {
                        fis = new FileInputStream(myFile);
                    } catch (FileNotFoundException ex) {
                        // Do exception handling
                    }
                    BufferedInputStream bis = new BufferedInputStream(fis);
    
                    try {
                        bis.read(mybytearray, 0, mybytearray.length);
                        outToClient.write(mybytearray, 0, mybytearray.length);
                        outToClient.flush();
                        outToClient.close();
                        connectionSocket.close();
    
                        // File sent, exit the main method
                        return;
                    } catch (IOException ex) {
                        // Do exception handling
                    }
                }
            }
        }
    }
    

    I couldn't run these source files and I don't know why.

  • el nino
    el nino over 13 years
    Thank you for the answer. Yes I was running the Server first. With the command prompt by changing the file path, it worked. Being independent of IDE was important. Thank you. :)