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 ;

import java.net.*;

import java.io.*;

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

try

{

while(true)

{

//Loop endlessly waiting for client connections

// Wait for a connection request

socket = ss.accept();

openStreams();

//Client has connected

game.run(); <-- this i presume runs the method run() from the GameSession Class?

closeStreams();

socket.close();

}

}

catch(Exception e)

{

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

{

toClient.close();

os.close();

fromClient.close();

is.close();

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);

acceptPlayer();

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.

[3089 byte] By [ianpwelch85a] at [2007-11-27 4:36:54]
# 1

You must redesign your Server and GameSession class.

Study this code:

/* save and compile as GameServer.java */

import java.net.*;

import java.io.*;

public class GameServer{

static final int PORT_NUMBER = 3000;

private ServerSocket ss;

GameSession game;

public GameServer() throws IOException{

ss = new ServerSocket(PORT_NUMBER);

game = new GameSession();

}

public void acceptPlayer(){

System.out.println("Waiting for player");

try{

while(true){

Socket s = ss.accept();

new Thread(new ClientHandler(s)).start();

}

}

catch(Exception e){

System.out.println("Trouble with a connection "+ e);

}

}

public static void main(String[] args){

try{

GameServer gs = new GameServer();

gs.acceptPlayer();

}

catch (Exception e){

e.printStackTrace();

}

}

class ClientHandler implements Runnable{

private Socket socket;

private PrintWriter toClient;

private BufferedReader fromClient;

public ClientHandler(Socket sc){

socket = sc;

try{

fromClient

= new BufferedReader(new InputStreamReader(socket.getInputStream()));

toClient = new PrintWriter(socket.getOutputStream(), true);

}

catch (Exception e){

e.printStackTrace();

}

}

public void run() throws IOException{

game.addNewClient(socket, fromClient, toClient);

}

}

}

hiwaa at 2007-7-12 9:47:05 > top of Java-index,Java Essentials,Java Programming...
# 2
Fantastic I've been stuck on this for hours! Thanks
ianpwelch85a at 2007-7-12 9:47:05 > top of Java-index,Java Essentials,Java Programming...