client server connection only allows 1 client

I am learning how to do the client server coding in java, and i have created what i thought was a multithreaded server. my understanding of a multithreaded server was one that could accept multiple clients. i have posted my code below in hopes someone could tell me why it will not allow multiple clients. right now i have the client pointing to the ip address of my computer(not the network address ie: 192.168.....) i start the server and then run a client on one computer on the network, and it will work fine, when i then run another client on the other computer it will not even display the gui until i close the 1st client that was opened. my server is connected to a class called conversation that handles all sql commands on a derby database. my server code is posted below.

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.WindowConstants;

publicclass Serverextends Thread{

publicfinalstaticint defaultPort = 5555;

int port;

ServerSocket listenSocket;

Socket clientSocket;

JFrame frame =new JFrame();

publicstaticvoid main(String args[]){

int port = 0;

if (args.length ==1){

try{

port=Integer.parseInt(args[0]);

}catch(NumberFormatException e){

port=0;

}

}

new Server(port);

//new guiServer();

}

public Server(int aport){

if(aport==0)

aport = defaultPort;

this.port = aport;

try{

listenSocket =new ServerSocket(port);

}catch(IOException e){

fail(e,"Exception creating server socket");

}

System.out.println("Server listening on port " + port);

this.start();

}//end Server(int aport)

publicvoid fail(Exception e, String msg){

JOptionPane.showMessageDialog(frame,msg +": " + e,"Warning",JOptionPane.WARNING_MESSAGE);

System.exit(1);

}// end fail

publicvoid run(){

frame =new JFrame("MMSI SERVER");

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

int width = 50;

int height = 50;

frame.setSize(width, height);

frame.setVisible(true);

try{

while(true){

clientSocket = listenSocket.accept();

conversation conv =new conversation(clientSocket);

}

}catch(IOException e){

fail(e,"Exception listening for connections");

}

}// end run

}//end Server

class conversationextends Thread{

..................... on to db connection and sql commands.

[5138 byte] By [developprogramsa] at [2007-11-26 21:47:12]
# 1

> class conversation extends Thread{

>

> ..................... on to db connection and sql commands.

The venom is in the tail. Does your 'conversation' object start another

thread again? If not your server keeps on waiting until the conversation

ctor has finished. I can't supply more detail whithout having read your

conversation class object.

kind regards,

Jos

JosAHa at 2007-7-10 3:37:20 > top of Java-index,Java Essentials,New To Java...
# 2

Socket client = null;

while(true)

{

client = server.accept();

// Open a new thread for the client just obtained and continue with the loop to access new clients

}

qUesT_foR_knOwLeDgea at 2007-7-10 3:37:21 > top of Java-index,Java Essentials,New To Java...
# 3

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

import javax.swing.JFrame;

import javax.swing.WindowConstants;

public class Server {

public final static int defaultPort = 5555;

int port;

JFrame frame;

public Server(String []args){

frame = new JFrame();

frame = new JFrame("MMSI SERVER");

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

int width = 50;

int height = 50;

frame.setSize(width, height);

frame.setVisible(true);

ServerSocket listenSocket;

int port = 0;

if (args.length == 1) {

try {

port = Integer.parseInt(args[0]);

} catch (NumberFormatException e) {

port = defaultPort;

}

}

try {

listenSocket = new ServerSocket(port);

while (true) {

Socket clientSocket = listenSocket.accept();

new Conversation(clientSocket);

}

} catch (Exception e) {

e.printStackTrace();

System.exit(-1);

}

}

public static void main(String args[]) {

new Server(args);

}

class Conversation implements Runnable {

Socket client;

BufferedReader fromClient;

PrintWriter toClient;

public Conversation(Socket client) {

this.client = client;

try {

fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));

toClient = new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);

toClient.println("hello from server.");

} catch (IOException e) {

try {

client.close(); // //////////// important

} catch (IOException ee) {

}

}

new Thread(this).start();

}

public void run() {

try {

boolean end = false;

String request;

while (!end) {

request = fromClient.readLine();

if (request != null) {

System.out.println("From server : " + request);

} else {

toClient.println("client->server");

}

}

} catch (IOException e) {

e.printStackTrace();

}

stop();

}

public void stop() {

try {

toClient.println("Bye");

client.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

hth

java_2006a at 2007-7-10 3:37:21 > top of Java-index,Java Essentials,New To Java...
# 4
thanks for the help. i forgot to implement runnable in the conversation class. all of your ideas helped me come to this conclusion. thanks for your help.
developprogramsa at 2007-7-10 3:37:21 > top of Java-index,Java Essentials,New To Java...