Hi there! I have been struggling for several days now to resolve an issue I am having with a game I am designing.
Basically I am using sockets to make my game run over the internet, and despite it working, I am have issues with the ReadLine() on my client.
If the Game returns 1 line of text to the client, it is fine. If I try and return multiple lines of text FROM the server, such as..
out.println("Hello there!" + "\n how are you?";
it will only read the first line (Hello There). I have tried looping the Realine() method until it reaches null, and this works. However, when I send the next command to the server, the ReadLine method won't return anything, almost as though its "stopped". I have tried resetting it, looping it, but it just isn't working. Im not 100% sure if this is the right section, as the problem could be to do with my sockets.
Not asking anyone to write this for me, just asking if anyone could help point me in the right direction or explain what's going wrong. Any help is greatly appreciated!
Below is my Client
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket mySocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
mySocket = new Socket("86.7.165.118",4450);
out = new PrintWriter(mySocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Can't find the Host");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to the IP");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput); //Send data to server
System.out.println(in.readLine()); //Print data received from server
}
}
}