Java客户端 - 服务器应用程序中的UnknownHostException

时间:2013-01-12 17:32:06

标签: java client-server

我正在设计一个共享文件的Java应用程序,但在尝试激活方法接收文件并发送文件时,我知道用户输入文件的名称并且服务器获取文件并获取文件并发送它

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

这是我的代码

客户代码

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();
            }
    }


}
}

服务器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();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

找不到名为“Hassan.txt”的服务器。确保您可以通过网络访问该套接字。 IP地址通常是数字的(除非您使用“localhost”或您的主机列在DNS上);看起来您使用的是文件名而不是主机本身。

相关问题