Hi Experts
Can anyone help me with a sockets problem? I have an application I am adding sockets to in order to access it with telnet.
I am able to get the sockets working and connect to the server but I cannot access the output stream from any other class in order to print to the telnet session i.e.
public class Game {
ServerSocket serverSocket = null;
// Make the server run on the net at port 4444
try {
serverSocket = new ServerSocket(4444);
}
catch (IOException e) {
System.out.println("Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader ir = null;
// wait until a client connects to the server
try {
// accept client connection -- store clients connection in
// clientSocket
clientSocket = serverSocket.accept();
// out is used to output strings to the client
out = new PrintWriter(clientSocket.g
etOutputSt
ream(), true);
// in is used to read input from the client
ir = new BufferedReader(
new InputStreamReader(clientSo
cket.getIn
putStream(
)));
} catch (Exception e) {
System.out.println("Accept
failed.");
System.exit(1);
}
public class Commands {
public void look(){
out.handle().print(Game.cu
rrentRoom.
roomDetail
);
out.print("is/are in the room ");
for (Enumeration e = Game.currentRoom.getEntity
().element
s(); e.hasMoreElements();)
{
Entities theEntity = (Entities) e.nextElement();
//Print Objects
Game.out.print(theEntity + " ");
}
Game.out.println();
Game.out.print("The room contains ");
for (Enumeration e = Game.currentRoom.getObject
s().elemen
ts(); e.hasMoreElements();)
{
Objects anObject = (Objects) e.nextElement();
//Print Objects
Game.out.print(anObject + " ");
}
System.out.println();
System.out.print("you can go ");
for (Enumeration e = Game.currentRoom.getDirect
ion().elem
ents(); e.hasMoreElements();)
{
Direction anExit = (Direction) e.nextElement();
//Print directions
System.out.print(anExit + " ");
}
System.out.println();
}
I cannot find a way to access the PrintWriter out from the Commands class do I need to make a server class? Do I need to make a method to return the PrintWriter? Can I make any of these static? Tried all sorts of variations cannot find much in the way of examples.
Any help would be much appreciated.
Thanks
Clive