Results 1 to 8 of 8
Thread: Multithreaded server crashes
- 05-22-2010, 07:50 PM #1
Member
- Join Date
- May 2010
- Posts
- 20
- Rep Power
- 0
[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
and the threaded classJava 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);} } }
For now i am just sending a string over, just to make sure that it is working.Java 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){}; } }
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 differenceLast edited by skarosg3; 05-26-2010 at 07:04 PM.
- 05-22-2010, 11:04 PM #2
It looks like have button that in your GUI that you press to start your server (listen for connections)... you have a blocking infinite loop there so you never give back control to your GUI and thus you lock your application.
You need to create a separate thread for your connection listener that you start when you press the button instead of using the GUI's thread.My Hobby Project: LegacyClone
- 05-23-2010, 08:32 AM #3
Member
- Join Date
- May 2010
- Posts
- 20
- Rep Power
- 0
So the thread should start at the press of the button. that makes sense. In the button action method i should call a "threaded" method.
will give it a try.
thanks
- 05-23-2010, 08:50 AM #4
Member
- Join Date
- May 2010
- Posts
- 20
- Rep Power
- 0
Could you help me a bit more. I havent used threads since college (8 years now) and i cant find something helpful on the web.
I quickly changed my code to this:
andJava Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { new ThreadedServer().run(); }
which in its turns calls the method i mentioned b4. I still get the same problem. The thread is been activated when the run method is called. right? so by calling it inside the buttonAction method, should allow the main program to run.Java Code:public ThreadedServer(){ } public void run(){ Socket clientSocket = null; ServerSocket serverSocket = null; clientThread t[] = new clientThread[10]; int port_number=2222; System.out.println("Usage: java MultiThreadChatServer \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);} } }
thanks for any help you might have
- 05-23-2010, 10:03 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Nope, a new Thread is activated when you call its start() method. The new Thread will call the run method (preferable from a Runnable object you passed in when you constructed your Thread) itself and the Thread is running from that point on; so don't call the Thread's run() method yourself, it won't start the Thread, it simply runs the code in your current Thread.
kind regards,
Jos
- 05-23-2010, 10:34 AM #6
Member
- Join Date
- May 2010
- Posts
- 20
- Rep Power
- 0
Ok, i am more confused now. I really cant remember all those stuff from college!
how should i implement this in order to work then?
- 05-23-2010, 11:20 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Find and bookmark the API documentation; it's all in there. You define your Runnable (it's an implementation of the Runnable interface) and create a Thread object with it. Once you start() your Thread, it does a bit of bookkeeping and calls the run() method on your Runnable implementation in another thread..
kind regards,
Jos
- 05-26-2010, 09:24 AM #8
Member
- Join Date
- May 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Multithreaded Client/Server Chat program
By f0ns in forum Threads and SynchronizationReplies: 3Last Post: 10-21-2009, 05:26 PM -
Java crashes
By Nicole in forum Advanced JavaReplies: 2Last Post: 04-06-2009, 07:22 AM -
IE7 crashes on starting java web application.
By gosia.gabriel in forum Advanced JavaReplies: 1Last Post: 01-19-2009, 02:29 PM -
A simple multithreaded server
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:15 PM -
Java Crashes on Mac 10.3.9 not sure how to update
By patricknowow in forum New To JavaReplies: 1Last Post: 11-30-2007, 03:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks