サーバーからクライアントにファイル情報を送信しようとしていますが、クライアント側で情報を受信できません。ストリームのエラーだと思いますが、見つかりません。
私はコードをデバッグし、情報がうまく読み取れていることを確認しましたが、行に来ると何か悪いことが起こりました:
while ((readLine = read.readLine()) != null) {
それはnull
、すべてが終了します。
public class Server {
private String urlFile = "http://riemann.fmi.uni-ofia.bg/vladov/students/boil.txt";
private ServerSocket serverSocket = null;
private BufferedReader read = null;
private BufferedWriter write = null;
private FileReader fileReader = null;
URLConnection urlConnection = null;
void acceptConnection() {
try {
serverSocket = new ServerSocket(3000);
Socket client = null;
while (true) {
client = serverSocket.accept();
handleConnection(client);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void handleConnection(Socket clientSocket) {
try {
URL url = new URL(urlFile);
urlConnection = url.openConnection();
read = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
write = new BufferedWriter(new OutputStreamWriter(
clientSocket.getOutputStream()));
String readLine = null;
while ((readLine = read.readLine()) != null) {
write.write(readLine);
write.flush();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void tearDownConnection() {
try {
write.close();
read.close();
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
クライアント:
public class Client {
Socket client = null;
BufferedReader reader = null;
BufferedWriter writer = null;
void connectToServer(String hostAddress, int port) {
try {
System.out.println("Client is waitting.");
client = new Socket(hostAddress, port);
reader = new BufferedReader(new InputStreamReader(
client.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(
client.getOutputStream()));
String readedLine = null;
readedLine = reader.toString();
while ((readedLine = reader.readLine()) != null) {
System.out.println(readedLine);
}
} catch (UnknownHostException e) {
System.out.println("Host name is unkown.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
void tearDownConnection() {
try {
if (client != null) {
client.close();
}
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}