0

I am designing a Java application that shares files but while trying to activate the methode recieve file and send file i am getting a exception knowing that the user enters the name of the file and the server gets it and gets the file and send it.

Exception in thread "main" java.net.UnknownHostException: Hasan.txt at 
java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177) at 
java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at 
java.net.Socket.connect(Socket.java:529) at 
java.net.Socket.connect(Socket.java:478) at 
java.net.Socket.<init>(Socket.java:375) at 
java.net.Socket.<init>(Socket.java:189) at 
p2pfilesharingproject.ClientP2P.main(ClientP2P.java:25) Java Result: 1

here is my code

Client Code

import java.io.*;
import java.net.*;
import java.util.*;

public class ClientP2P {

    public static void main(String argv[]) throws Exception {
        Scanner s = new Scanner(System.in);
        String sentence;
        String modifiedSentence;
        System.out.print("which Port  are you going to listen to ?");

        int Socket = s.nextInt();

        String host = s.next();
        BufferedReader inFromUser =
                new BufferedReader(new InputStreamReader(System.in));

        Socket clientSocket = new Socket(host, Socket);

        DataOutputStream outToServer =
                new DataOutputStream(clientSocket.getOutputStream());

        BufferedReader inFromServer =
                new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        sentence = inFromUser.readLine();  
        RecieveFile(sentence);
        outToServer.writeBytes(sentence);


    }

    public static void RecieveFile(String Fname)
    {
        byte[] aByte = new byte[1];
        int bytesRead;

        Socket clientSocket = null;
        InputStream is = null;

        try {
            clientSocket = new Socket("127.0.0.1", 111);
            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("E:\\"+Fname);
                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) {
                ex.getStackTrace();
            }
    }


}
}

Server Cpde

import java.io.*;
import java.net.*;
import java.util.*;

public class ServerP2P {

    public static void listfile() {

 String path = "C:/SAVE"; 

  String files;
  File folder = new File(path);
  File[] listOfFiles = folder.listFiles(); 

  for (int i = 0; i < listOfFiles.length; i++) 
  {

   if (listOfFiles[i].isFile()) 
   {
   files = listOfFiles[i].getName();
   System.out.println(files);
      }
  }
    }

    public static void main(String argv[]) throws Exception {
        Scanner s = new Scanner(System.in);
        String clientSentence;
        String capitalizedSentence;
        System.out.print("Listen on Port Number : ");
        int Socket = s.nextInt();

        ServerSocket welcomeSocket = new ServerSocket(Socket);
        listfile();
        while (true) {

            Socket connectionSocket = welcomeSocket.accept();

            BufferedReader inFromClient =
                    new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));


            DataOutputStream outToClient =
                    new DataOutputStream(connectionSocket.getOutputStream());

            clientSentence = inFromClient.readLine();
            String fn = inFromClient.readLine();
            SendFile(fn);


        }
    }

    public static void SendFile(String FileName) {
        while (true) {
            ServerSocket welcomeSocket = null;
            Socket connectionSocket = null;
            BufferedOutputStream outToClient = null;

            try {
                welcomeSocket = new ServerSocket(111);
                connectionSocket = welcomeSocket.accept();
                outToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
            } catch (IOException ex) {
                // Do exception handling
            }


            if (outToClient != null) {
                File myFile = new File("C:\\" + FileName);

                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) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
6
  • 2
    (i) i am getting a exception => what exception? on what line? can you post the stacktrace? (ii) you have many empty catch blocks, which means that exceptions are ignored => that is not going to help you. You should at least print something (ex.printStackTrace();) to help you debug the issue. Commented Jan 12, 2013 at 17:33
  • Seconding @assylias here: what is the exception? Commented Jan 12, 2013 at 17:35
  • Exception in thread "main" java.net.UnknownHostException: Hasan.txt at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at java.net.Socket.connect(Socket.java:478) at java.net.Socket.<init>(Socket.java:375) at java.net.Socket.<init>(Socket.java:189) at p2pfilesharingproject.ClientP2P.main(ClientP2P.java:25) Java Result: 1 Commented Jan 12, 2013 at 17:40
  • @RaymondBouChaaya It is easier if you include all the information in your question - I have added the exception. Commented Jan 12, 2013 at 17:46
  • In examples, use constants instead of Scanner. That makes it easier to test your program just as you are. Commented Jan 12, 2013 at 17:51

1 Answer 1

2

The server called "Hassan.txt" could not be found. Make sure that you can reach that socket via a network. IP addresses are usually numeric (unless you are using "localhost" or your host is listed on DNS); it looks like you are using the name of a file instead of the host itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.