gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:20:36 26 4
gpt4 key购买 nike

我正在设计一个共享文件的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();
}
}
}
}
}

最佳答案

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

关于java - Java 客户端-服务器应用程序中的 UnknownHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14295931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com