Search code examples
javasocketsfile-transfer

java: Transfer a file to the server and get the file from the server in upper case


i want to send a .txt file from the client to server and get it back in upper case. But this code do nothing.can anyone tell what is wrong here..?

SERVER : getting file from client and sending it back in upper case to the client.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;

public class Assignment4_Server {

    public static void main(String[] args) throws IOException {
        byte[] bytearray = new byte[4096];
        try (ServerSocket ss = new ServerSocket(4444)) {
            Socket s = ss.accept();
            InputStream is = s.getInputStream();
            OutputStream os = s.getOutputStream();
            int count;
            String data = null ;
            while((count = is.read(bytearray))>0){
                data = Arrays.toString(bytearray).toUpperCase();
                byte[] bytearrayout = data.getBytes();
                os.write(bytearrayout);
            }
            s.close();
        }
    }
}

CLIENT : sending text.txt file to the server and getting file back after converted in upper case.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;


public class Assignment4_client {
    public static void main(String[] args) throws IOException {
        File file = new File("test.txt");
        byte[] bytearray = new byte[4096];
        Socket sc = new Socket("localhost",4444);
        //send file
        int countS , countR;
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        OutputStream os = sc.getOutputStream();
        while((countS = bis.read(bytearray))>0){
        os.write(bytearray);
        }
        //recieve file in uppercase from server
        InputStream is = sc.getInputStream();
        byte[] bytearray2 = new byte[4096];
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        while((countR = is.read(bytearray2))>0){
            bos.write(bytearray2);
        }
    }   
}

Solution

  • Here is a code that should help you. But before reading it you should be aware of what is happening:

    1. Your client is not sending a 'stop reading' information to the server (read the client code below). That's why the server is stuck in the while loop when it is trying to read the data sent by the client. That is probably why you have tried to send the data back directly to the client. Shut down the socket output from the client side to respect the Socket contract and correctly free the socket (see TCP/IP).
    2. The solution given doesn't take in account that the server should stay up after it has done its duty. Then, the server will not be able to serve more than one client at a time. This server is offering a one time service, which is pointless. To overcome this issue you should put everything in a while loop and bind every new server process into a new thread (I let you do that, its quite a joy).
    3. The server doesn't take in account the whole size of the data an it could possibly run into an out of memory error if the data is too heavy. You should find a way to avoid this problem in a real implementation.
    4. Both program should catch the exception and log it somewhere so you could be aware of any errors.
    5. Writing a server is not so simple. You should normally write some kind of protocol with headers and other stuff like that. To avoid that, use objects like ObjectOutputStream and ObjectInputStream but it has some limitation like constraining your server in the Java world.

    CLIENT

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.net.Socket;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.IOException;
    
    public class Client {
    
    
        public void send(File file)
        {
            Socket sc = null;
            try
            {
                byte[] bytearray = new byte[4096];
                sc = new Socket("localhost", 4444);
    
                // 1. Read the file, send its content, close it. 
                int count;
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis);
                OutputStream os = sc.getOutputStream();
    
                while((count = bis.read(bytearray))>0)
                {
                    os.write(bytearray);
                }
                fis.close();
                sc.shutdownOutput();
    
                // 2. Delete old file, receive data, write it to new File.
                InputStream is = sc.getInputStream();
                bytearray = new byte[4096];
                // Eventually do what you want with the file: new one, append, etc.
                file.delete();
                file.createNewFile();
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
                count = 0;
                while((count = is.read(bytearray)) > 0)
                {
                    bos.write(bytearray, 0, count);
                }
                fos.close();
                bos.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                if (sc != null)
                {
                    try
                    {
                        sc.close();
                    } catch (IOException e) {}
                }
            }
        }
    }
    

    SERVER

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server
    {
        Server()
        {
            Socket s = null;
            byte[] bytearray = new byte[4096];
            try (ServerSocket ss = new ServerSocket(4444))
            {
                s = ss.accept();
                InputStream is = s.getInputStream();
    
                // 1. Recieve data and put it to UpperCase.
                String data = "";
                int count;
                while((count = is.read(bytearray)) > 0)
                {
                    data += new String(bytearray, 0, count);
                }
                data = data.toUpperCase();
                System.out.println(data);
    
                // 2. Send back data.
                OutputStream os = s.getOutputStream();
                os.write(data.getBytes());
                os.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    

    TEST PROGRAM

    This one should help you to test your both programs in the same project in an IDE.

    import java.io.File;
    
    public class Test
    {
        public static void main(String[] args)
        {
            Client c = new Client();
            (new Thread()
            {
                public void run()
                {
                    Server s = new Server();
                }
            }).start();
            c.send(new File("test.txt"));
        }
    }