I am new to programming. Need just one program for my bachelors project.
I have a problem with running a thread in my program which uses buffered reader to save some data from an internet stream. When I run the thread for the first time everything works perfectly but when i finish it and run a new one, thread seems working but the cycle with buffered reader part somehow doesn't work. Can you help me?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextField;
volatile boolean stopRequested = false;
private Socket socket = null;
private BufferedReader in = null;
public void runMe() {
stopRequested = false;
Thread vypocet = new Thread(mytask);
vypocet.start();
}
public void stopMe() {
stopRequested = true;
}
public Runnable mytask = () -> {
try {
socket = new Socket(Gui.iP.getText(), Integer.parseInt(Gui.port.getText()));
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection");
System.exit(1);
}
try {
for (String line = in.readLine(); stopRequested != true; line = in.readLine()) {
//some calculation I need
}
catch (IOException ex1) {
Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex1);
System.err.println("Error in loading receiver's data. Please turn the program off and on again.");
System.exit(1);
}
System.out.println("End of the calculation");
};