Skip to main content

Basic Java Help (BufferedReader and PrintWriter)

2 replies [Last post]
ianwelch81
Offline
Joined: 2007-05-16

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.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ianwelch81
Offline
Joined: 2007-05-16

(And the bit that got cut off ....

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.

tarbo
Offline
Joined: 2006-12-18

The issue is most likely that you are binding two sockets to the same port because you are running both server as client on the same IP address. This doesn't need to be a conceptual issue.

Client sockets can be [i]bound[/i] to virtually any port; what's important is what port they are [i]connected[/i] to. The server socket, however, is listening for connections and should do so on a port that was agreed upon beforehand.

So try creating a client socket that binds to any socket address, but connects to the server socket address.