slight problem with socket/threads?
I am beginning development of a Java MUD, and I have coded a Server class that will be the actual server that handles the sockets, and I have a Connection class, that will basically contain most of the user-interaction. This project is in early development, and I am kinda stuck. The server will allow a connection, and print "What is your name, adventurer? " but when I try to connect a second time, the second connection will just sit there until the first connection inputs a name. Then it will prompt the second user "What is your name, adventurer? " I am sure it is something I am doing wrong with the Threads, as I am not very experienced with this aspect of Java. I will post the code for both classes.. probably about 150 lines total...
/*
* File:Connection.java
*
* Author:Brian Williams
* Usage:Handle incoming connections.
*/
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
publicclass Connectionextends Thread{
Server server;// reference back to the server
Socket socket;
BufferedReader in;
PrintWriter out;
public Connection(Server ss, Socket s)throws IOException
{
server = ss;
socket = s;
in =new BufferedReader(new InputStreamReader(s.getInputStream()));
out =new PrintWriter(s.getOutputStream());
}
publicvoid log(String text)
{
server.log(text);
}
publicvoid run()
{
try
{
while (!doLogin()){}// basically, CON_GET_NAME
socket.close();
}catch (SocketException se){
}catch (IOException ioe){
}
}
privateboolean doLogin()throws SocketException
{
boolean okUser =false;
log("Connection received from " + socket.getInetAddress().getHostName() +".");
try
{
// THIS IS WHERE IT'S GETTING HUNG UP
print("What is your name, adventurer? ");
if (socket ==null)thrownew SocketException("WARNING: null socket in doLogin()");
String user = readln();
if (!user.equals(""))
{
log("Player " + user +" has connected.");
returntrue;
}
}catch (SocketException se){
throw se;
}catch (Exception e){
log("EXCEPTION: " + e.toString());
}
returnfalse;
}
publicvoid print(String text)throws IOException
{
out.write(text, 0, text.length());
out.flush();
}
publicvoid println(String text)throws IOException
{
print(text +"\r\n");
}
public String readln()throws IOException
{
return in.readLine();
}
}
/*
* Realms of Luminari
*
* File:Server.java
*
* Author: Brian Williams
* Usage: Create a ServerSocket and listen for incoming Socket connections.
*/
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.text.SimpleDateFormat;
publicclass Serverextends Thread{
staticint port = 5555;// what port shall we be running on?
privatestatic ServerSocket server;
privatestatic Socket connection;
static SimpleDateFormat dateFormatter =new SimpleDateFormat("MM/dd/yyyy HH:mm");
privatestatic Date bootTime;
staticboolean shutDown =false;
static Server s =new Server();
public Server(){
}
publicstaticvoid main(String[] args)
{
s.run();
}
publicvoid run()
{
// initialize server
try{
log("Beginning server on port " + port +".");
initGame(port);
}catch (IOException ioe){}
}
publicstaticvoid initGame(int port)throws IOException
{
server =new ServerSocket(port);
bootTime =new Date();
log("Entering game loop.");
gameLoop();
// anything after this means the server is no longer running
// close all sockets here
log("Normal termination of game server.");
}
publicstaticvoid gameLoop()
{
while (!shutDown)
{
try{
new Connection(s, server.accept()).run();
}catch (java.io.InterruptedIOException iioe){
}catch (IOException ioe){}
// process input
// process output
// if wait state, continue;
// if (state != con_playing) nanny()
// else commandInterpreter()
}
}
publicstaticvoid log(String str)
{
System.out.println("[" + dateFormatter.format(new Date()) +"]\t" + str);
}
}

