Basic Java Help (BufferedReader and PrintWriter)
I am studying Java for part of my degree and have become quite stuck on a question. I have set up a seperate client class and that appears to work just fine, yet when i come to set up the server class i encounter no end of problems.
Firstly i have to set up a Server class which handles the communication to the client ;
public class Server
private ServerSocket ss;
private Socket socket;
//Streams for connections
private InputStream is;
private OutputStream os;
//Writer and reader for communication
private PrintWriter toClient;
private BufferedReader fromClient;
private GameSession game;
//use a high numbered non-dedicated port
static final int PORT_NUMBER = 3000;
//set up socket communication
public Server() throws IOException
ss = new ServerSocket(PORT_NUMBER);
//accept a player connection
public void acceptPlayer()
System.out.println("Waiting for player");
//wait for and accept a player
//Loop endlessly waiting for client connections
// Wait for a connection request
socket = ss.accept();
//Client has connected
game.run(); <-- this i presume runs the method run() from the GameSession Class?
System.out.println("Trouble with a connection "+ e);
private void openStreams() throws IOException
final boolean AUTO_FLUSH = true;
is = socket.getInputStream();
fromClient = new BufferedReader(new InputStreamReader(is));
os = socket.getOutputStream();
toClient = new PrintWriter(os, AUTO_FLUSH);
System.out.println ("...Streams set up");
private void closeStreams() throws IOException
System.out.println("...Streams closed down");
// This is the top-level method to handle one game
public void run() throws IOException
game = new GameSession(socket);
System.out.println("Server closing down");
} // end class
which also seems to be about what the question asks, yet i have to link this to the GameSession class sending it a reference of the socket. Which i think i have with the inclusion of the new GameSession(socket).
When i come to write the GameSession class i realise i have to use the BufferedReader and PrintWriter when i try and do this i get a bindException as obviously the port is up and running from the Server class.
I have tried incorporating the openStreams() in the GameSession after rermoving them from the Server class but get more Exceptions! What am i doing wron and where would i find more information on this?
Thanks for any help you may give.