Hi I'm facing a little problem with eof()
I created a tcp connection between a client and a server to send text to the server and
receive it back in capital letters but the problem is that it's reading one line only so
I would like to modify the code so it reads until I type "stopp". When I type stop both
the client and the receiver terminate at the same time.
This is the client class
import java.io.*;
import java.net.*;
public class Klient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException,
IOException {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(System.in));
Socket clientSocket = new Socket("127.0.0.1", 6789);
DataOutputStream outToServer = new
DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
while (true){
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence '\n');
modifiedSentence = inFromServer.readLine();
System.out.println(modifiedSentence);
}
//clientSocket.close();
}
}
This is the server class
import java.io.*;
import java.net.*;
public class TCPServer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while (true){
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new
DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
CodePudding user response:
It is only reading one sentence because the server does not repeatedly ask for new input. Sure, you have a while(true)
loop, but that loop is repeatedly accepting new connections (this is what welcomeSocket.accept()
does). Once you asked for one line input, you go back to the start of the loop, and waits for another connection to come. Of course, no more connections are coming, so the server just does nothing forever.
You should add an inner loop,
while (true){
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
while(true) {
clientSentence = inFromClient.readLine();
if (clientSentence.equalsIgnoreCase("stop")) {
connectionSocket.close();
break;
}
capitalizedSentence = clientSentence.toUpperCase() '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
Once the server receives "stop", it would close the connection with the current client, and wait for another client to connect as it goes back to the top of the outer loop. If you want the server to terminate completely (so only accepting one connection), just remove the outer loop, and close the welcomeSocket
:
ServerSocket welcomeSocket = new ServerSocket(6789);
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
while(true) {
clientSentence = inFromClient.readLine();
if (clientSentence.equalsIgnoreCase("stop")) {
connectionSocket.close();
break;
}
capitalizedSentence = clientSentence.toUpperCase() '\n';
outToClient.writeBytes(capitalizedSentence);
}
welcomeSocket.close();
As for the client, it can break out of the loop when inFromServer.readLine
returns null, which is indication that the connection has been closed.
while (true){
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence '\n');
modifiedSentence = inFromServer.readLine();
if (modifiedSentence == null) {
break;
}
System.out.println(modifiedSentence);
}