1

Im trying to read the contents of a file in the path that is send by the client to the server, the server is receiving the file path and reading the data but somehow when i send the data my client is not receiving it can you help identify what I'm doing wrong Serverside code:

        ServerSocket myserver = new ServerSocket(5354);
        System.out.println("SERVER RUNNING on 127.0.0.1:5354 \n waiting for connection from client...\n");
        try {
            while (true) {

                Socket socket = myserver.accept();
                try {
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    out.println(new Date().toString() + "Hi there I'm the server,I have recieved your connection");
                    System.out.println("Received connection from a client:\n"+ socket.getRemoteSocketAddress().toString());
                    //specify the filename
                    Scanner input=new Scanner(socket.getInputStream());
                    String filepath=input.next();
                    File file=new File(filepath);
                    FileInputStream fis=new FileInputStream(file);
                    BufferedInputStream  bf=new BufferedInputStream(fis);
                    //GET socket output stream
                    BufferedOutputStream outToClient=new BufferedOutputStream(socket.getOutputStream());
                    byte[] contents=new byte[(int)file.length()];
                    try{
                        bf.read(contents,0,contents.length);
                        outToClient.write(contents,0,contents.length);
                        outToClient.flush();
                        String s=new String(contents, StandardCharsets.UTF_8);
                        out.write(s);
                        out.flush();
                        System.out.println(s);


                    }catch (Exception e){
                        System.out.println(e.getStackTrace().toString());
                    }finally{
                        if(bf!=null)bf.close();
                        if(outToClient!=null)outToClient.close();
                    }
                    //file transfer complete.close the socket or throw an error
                } catch(Exception e){
                    System.out.println("Sorry I got an error" + e.getMessage());
                }finally {

                    socket.close();
                }



            }

        }catch(Exception e){
            System.out.println("Oopps smething got messed up " + e.getMessage());
        }finally{
            myserver.close();
        }
    }

client side code:

        int port;
        InetAddress ipaddress;
        byte[] contents=new byte[1000];
        String filepath;
        //get input for the server you want to connect to
        System.out.print("ENTER THE IP ADDRESS:");
        Scanner input = new Scanner(System.in);
        ipaddress = InetAddress.getByName(input.next());
        System.out.print("ENTER THE PORT NUMBER:");
        port = input.nextInt();
        System.out.print("enter the name of the file you want to read:");
        filepath=input.next();

        Socket myclient = new Socket(ipaddress, port);//creating a socket connection to the server
         myclient.getOutputStream().write(filepath.getBytes(Charset.forName("UTF-8")));
        myclient.getOutputStream().flush();
            Scanner sinput = new Scanner(new InputStreamReader((myclient.getInputStream())));

            String response = sinput.nextLine();

            System.out.print(response);
            myclient.getReceiveBufferSize();
            myclient.getKeepAlive();
            BufferedReader br=new BufferedReader(new InputStreamReader(myclient.getInputStream()));
           String data=br.readLine();
           System.out.println(data);

          myclient.close();

        System.out.println("data transmission finished....:)");
    }
micthaworm
  • 23
  • 6
  • what is the output? – nail fei Nov 29 '16 at 09:05
  • 1
    on my server side I'm getting this SERVER RUNNING on 127.0.0.1:5354 waiting for connection from client... Received connection from a client: /127.0.0.1:54653 this is the content this is the second content this is the third what i want this is for the client to print this file content on the console – micthaworm Nov 29 '16 at 09:16

0 Answers0