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

}

}

[10152 byte] By [dub_styleea] at [2007-11-27 11:07:13]
# 1

Sorry, that post may be a little long, and I don't know if I was clear on the problem.. basically, I have a Server object that listens for connections, and a Connection object that handles interaction with the user. In order to allow multiple users to connect to my Server object, does the Server need to be a Thread as well, or do I only need the Connection object to be a Thread?

Like I said in the original post, my problem is the Server will not allow 2 simultaneous connections. Once the 1st connection is finished, it will allow a 2nd connection. I _THINK_ this is related to Threading, but I'm not sure. Any help is greatly appreciated. Thanks,

dub stylee

dub_styleea at 2007-7-29 13:20:43 > top of Java-index,Java Essentials,New To Java...
# 2

Ok, I found my mistake. I was calling the run() method directly, rather than calling start().

dub_styleea at 2007-7-29 13:20:43 > top of Java-index,Java Essentials,New To Java...