I am creating a java web server that is a part of a game of quiz. For testing purposes, I have included a counter. The server has a FixedThreadPool that are handling the threads.
The server has a Bufferedreader that will collect data from a database and a Printstream which I will use to write the output to a client. Now, the intention with Printstream is to print out the result of the counter. But when I run the server nothing happens, the printStreamn is not working as intended. There is no output in the terminal and the client is not receiving anything from the server. What is wrong?
Server
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class QuizServer {
private static final int serverPort = 9001;
public static void main(String[] args) throws Exception {
ExecutorService theExecutor = Executors.newFixedThreadPool(4);
System.out.println("The chat server is running.");
ServerSocket quizSocket = new ServerSocket(serverPort);
while (true) { //Skriver ut While True
Socket connection = quizSocket.accept();
theExecutor.execute(new Broadcaster(connection));
}
}
}
class Broadcaster extends Thread {
private BufferedReader read;
private PrintStream write;
private Socket broadcastSocket;
static int counter = 0;
public Broadcaster(Socket broadcastSocket) throws IOException{
read = new BufferedReader(new
InputStreamReader(broadcastSocket.getInputStream()));
write = new PrintStream(broadcastSocket.getOutputStream());
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
counter += 1;
write.println("Räknare: " + counter);
} catch (InterruptedException e) {
System.out.println("Sleep exception: " + e);
}
}
}
}
Debug
Connected to the target VM, address: '127.0.0.1:1925', transport: 'socket'
The chat server is running.
Exception in thread "main" java.net.BindException: Address already in use:
JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind
(DualStackPlainSocketImpl.java:106)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at java.net.ServerSocket.<init>(ServerSocket.java:128)
at QuizServer.main(QuizServer.java:19)
Disconnected from the target VM, address: '127.0.0.1:1925', transport:
'socket'
Process finished with exit code 1
Client
public class Client implements Runnable {
@FXML
private Button bQuit = new Button();
@FXML
private TextArea Out = new TextArea();
@FXML
private TextField Input = new TextField();
@FXML
private TextField IP = new TextField();
@FXML
private TextField aliasField = new TextField();
String alias;
String ServerAddress = "localhost";
String userAnswer;
BufferedReader reader;
PrintWriter writer;
@FXML
public void initialize() {
bQuit.setOnAction(event -> Platform.exit());
aliasField.setOnAction(a -> {
alias = aliasField.getText();
Input.setOnAction(b -> {
userAnswer = Input.getText();
});
});
aliasField.setPromptText("Alias");
Input.setPromptText("And your answer is:");
}
@Override
public void run() {
try {
Socket clientSocket = new Socket(ServerAddress, 9001);
reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
writer = new PrintWriter(clientSocket.getOutputStream(),true); //Koppla user input hit.
Scanner newObject= new Scanner(reader);
while(newObject.hasNextLine())
Out.setText(newObject.nextLine());
//
} catch (IOException e) {
}
}
}
Most likely client is wrong.
reader = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
writer = new PrintWriter(clientSocket.getOutputStream(),true);
Out.setText(String.valueOf(writer));
You try to set a Out.text
from clientSocket.getOutputStream()
but... clientSocket.getOutputStream()
is a stream to the server, not from the server.
What do you expect to get out of String.valueOf(writer)
?
On the client You need to read what server sends to you from input reader into String and set it to Out.text