我正在编写一个简单的客户端 - 服务器程序,我的问题是服务器不等待来自客户端的答案,这会导致IOException
。服务器尝试执行
line = bufferStringFromClientReader.readLine();
我做错了什么?
服务器:
import java.io.*;
import java.net.*;
public class Server {
private final static String filePath = "dataFile.txt";
public static void main(String[] arg) {
ServerSocket serverSocket = null;
Socket serverConnection = null;
OutputStream outServerStream = null;
InputStream inServerStream = null;
PrintWriter printWriterDataSender = null;
BufferedReader bufferDataFromFileReader = null;
BufferedReader bufferStringFromClientReader = null;
BufferedWriter bufferedStringToFileWriter = null;
String line;
try {
serverSocket = new ServerSocket(8868);
} catch (IOException e) {
throw new RuntimeException("Nieobsłużony wyjątek - nie można utowrzyć gniazda serwera");
}
try {
serverConnection = serverSocket.accept();
} catch (IOException e) {
throw new RuntimeException("Nieobsłużony wyjątek - nie można nawiązać połączenia z klientem");
}
try {
inServerStream = serverConnection.getInputStream();
} catch (IOException e) {
throw new RuntimeException("Nieobsłużony wyjątek - nie można pobrać strumienia wejścia");
}
try {
outServerStream = serverConnection.getOutputStream();
bufferDataFromFileReader = new BufferedReader(new FileReader(filePath));
bufferedStringToFileWriter = new BufferedWriter(new FileWriter(filePath, true));
} catch (IOException e) {
throw new RuntimeException("Nieobsłużony wyjątek - nie można pobrać strumienia wyjścia");
}
while (true) {
try {
printWriterDataSender = new PrintWriter(outServerStream);
line = bufferDataFromFileReader.readLine();
printWriterDataSender.println(line);
printWriterDataSender.flush();
printWriterDataSender.close();;
bufferStringFromClientReader = new BufferedReader(new InputStreamReader(inServerStream));
System.out.println("get answer from client");
line = bufferStringFromClientReader.readLine();
bufferedStringToFileWriter.write(line);
bufferedStringToFileWriter.close();
} catch (Exception e) {
System.out.println("utracono polaczenie");
try {
serverConnection = serverSocket.accept();
} catch (IOException e1) {
throw new RuntimeException("Nieobsłużony wyjątek - nie można nawiązać połączenia z klientem");
}
try {
inServerStream = serverConnection.getInputStream();
} catch (IOException e2) {
throw new RuntimeException("Nieobsłużony wyjątek - nie można pobrać strumienia wejścia");
}
try {
outServerStream = serverConnection.getOutputStream();
} catch (IOException e3) {
throw new RuntimeException("Nieobsłużony wyjątek - nie można pobrać strumienia wyjścia");
}
}
}
}}
客户端:
import java.awt.EventQueue;
import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
public class Client {
public static void main(String[] args)
{
Socket clientSocket = null;
InputStream clientInputStream = null;
OutputStream clientOutputStream = null;
BufferedReader clientBufferReader = null;
try
{
clientSocket = new Socket("localhost", 8868);
}
catch(UnknownHostException e)
{
throw new RuntimeException("Nieobsłużony wyjątek - nieznana nazwa hosta");
}
catch(IOException e)
{
throw new RuntimeException("Nieobsłużony wyjątek - nie można utowrzyć gniazda klienta");
}
try
{
clientInputStream = clientSocket.getInputStream();
}
catch(IOException e)
{
throw new RuntimeException("Nieobsłużony wyjątek - nie można pobrać strumienia wejścia");
}
try
{
clientOutputStream = clientSocket.getOutputStream();
}
catch(IOException e)
{
throw new RuntimeException("Nieobsłużony wyjątek - nie można pobrać strumienia wyjścia");
}
try
{
clientBufferReader = new BufferedReader(new InputStreamReader(clientInputStream));
}
catch(Exception e)
{
throw new RuntimeException("Nieobsłużony wyjątek - nie można utworzyć bufora odczytu danych od serwera");
}
try
{
String tmpString = clientBufferReader.readLine();
System.out.println(tmpString);
PrintWriter answer = new PrintWriter(clientOutputStream);
answer.println("ans \n");
answer.close();
}
catch(Exception e)
{
throw new RuntimeException("Nieobsłużony wyjątek - odpowiedz");
}
}
答案 0 :(得分:1)
问题是您过早关闭服务器的PrintWriter
。这样做会导致阻止从套接字InputStream
读取任何数据。
printWriterDataSender.close();
所有finally
Socket
操作完成后,将其移至IO
块将解决问题。确保在此之后发生socket.close
:
printWriterDataSender.close();
bufferStringFromClientReader.close();
serverConnection.close(); // last call
此外,一旦完成此操作,您可能希望进行一次读取。目前,您正在while
循环中进行读写,但客户端只写一次。这将导致Socket
读取异常。
答案 1 :(得分:0)
在您的客户PrintWriter answer = new PrintWriter(clientOutputStream, true );
中启用 autoflush 行为。客户端和服务器都可以使用此功能; flush在代码服务器端由pw.flush()
完成。
为了更加清晰和可读,您可以编写如下代码,这里是您的客户端和服务器重写,它可以工作!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class Client {
static final CountDownLatch cdl = new CountDownLatch( 1 );
static class Server extends Thread {
@Override
public void run() {
try( ServerSocket serverSocket = new ServerSocket( 8868 )) {
cdl.countDown();
Socket serverConnection = serverSocket.accept();
OutputStream outServerStream = serverConnection.getOutputStream();
InputStream inServerStream = serverConnection.getInputStream();
PrintWriter dataSender = new PrintWriter( outServerStream, true );
BufferedReader clientReader = new BufferedReader( new InputStreamReader( inServerStream ));
dataSender.println( "Hello, " );
System.out.println( "SERVER: " + clientReader.readLine());
serverConnection.close();
}
catch( IOException x ) {
x.printStackTrace();
}
}
}
public static void main( String[] args ) throws Exception {
new Server().start();
cdl.await( 1, TimeUnit.DAYS );
Socket clientSocket = new Socket( "localhost", 8868 );
InputStream clientInputStream = clientSocket.getInputStream();
OutputStream clientOutputStream = clientSocket.getOutputStream();
PrintWriter answer = new PrintWriter( clientOutputStream, true );
BufferedReader clientBufferReader = new BufferedReader( new InputStreamReader( clientInputStream ));
System.out.println( "CLIENT: " + clientBufferReader.readLine());
answer.println( "world!" );
clientSocket.close();
}
}
输出:
CLIENT: Hello,
SERVER: world!