[SOLVED] Multithreaded server crashes
Hi all,
this is my first post, so some greetings are in place:)
I am working on a project, that involves a client server and a separate db connection.
I am still at the begining, but i am stuck at a problem that cant figure it out.
I want to have a client/server connection in order to sent a txt file to the client. I figured to go for multi-threaded server just to be on the safe side.
The threading part actually works, but whenever i open the port and the server starts listening to the port, the rest of the program just crashes! i cant do anything on it. Thats not exactly what i wanted. this is my code: (part of it)
the function from the server
Code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Socket clientSocket = null;
ServerSocket serverSocket = null;
clientThread t[] = new clientThread[10];
int port_number=2222;
System.out.println("Usage: java MultiThread \n"+
"Now using port number="+port_number);
try {
serverSocket = new ServerSocket(port_number);
}
catch (IOException e)
{System.out.println(e);}
while(true){
try {
clientSocket = serverSocket.accept();
for(int i=0; i<=9; i++){
if(t[i]==null)
{
(t[i] = new clientThread(clientSocket,t)).start();
break;
}
}
}
catch (IOException e) {
System.out.println(e);}
}
}
and the threaded class
Code:
import java.io.*;
import java.net.*;
/**
*
* @author ilias
*/
class clientThread extends Thread{
DataInputStream is = null;
PrintStream os = null;
Socket clientSocket = null;
clientThread t[];
public clientThread(Socket clientSocket, clientThread[] t){
this.clientSocket=clientSocket;
this.t=t;
}
public void run()
{
String line;
String name;
try{
os = new PrintStream(clientSocket.getOutputStream());
os.println("Enter your name.");
os.close();
clientSocket.close();
}
catch(IOException e){};
}
}
For now i am just sending a string over, just to make sure that it is working.
any idea how i can make the server to be functioning while running this thread?
thanks a lot
ilias
ps. I am working on Netbeans, if that makes any difference