ファイルを共有する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();
}
}
}
}
}