Results 1 to 8 of 8
  1. #1
    skarosg3 is offline Member
    Join Date
    May 2010
    Posts
    20
    Rep Power
    0

    Default [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
    Java 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
    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){};
        }
    }
    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
    Last edited by skarosg3; 05-26-2010 at 07:04 PM.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    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

  3. #3
    skarosg3 is offline Member
    Join Date
    May 2010
    Posts
    20
    Rep Power
    0

    Default

    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

  4. #4
    skarosg3 is offline Member
    Join Date
    May 2010
    Posts
    20
    Rep Power
    0

    Default

    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:

    Java Code:
       private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
           new ThreadedServer().run();
        }
    and
    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);}
    	}
         }
    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.

    thanks for any help you might have

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by skarosg3 View Post
    The thread is been activated when the run method is called. right?
    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

  6. #6
    skarosg3 is offline Member
    Join Date
    May 2010
    Posts
    20
    Rep Power
    0

    Default

    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?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by skarosg3 View Post
    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?
    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

  8. #8
    skarosg3 is offline Member
    Join Date
    May 2010
    Posts
    20
    Rep Power
    0

    Default

    Got it, thanks man!
    I am not sure if it is the best way to go, but it is working. When i am done, i might have a look to improve.
    cheers

Similar Threads

  1. Multithreaded Client/Server Chat program
    By f0ns in forum Threads and Synchronization
    Replies: 3
    Last Post: 10-21-2009, 05:26 PM
  2. Java crashes
    By Nicole in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 07:22 AM
  3. IE7 crashes on starting java web application.
    By gosia.gabriel in forum Advanced Java
    Replies: 1
    Last Post: 01-19-2009, 02:29 PM
  4. A simple multithreaded server
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:15 PM
  5. Java Crashes on Mac 10.3.9 not sure how to update
    By patricknowow in forum New To Java
    Replies: 1
    Last Post: 11-30-2007, 03:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •