Results 1 to 9 of 9
  1. #1
    pinkette is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default Need Help! Multi-Threading question!

    Hey guys, i would like to multi-thread a Socket connection in Java. So for example its a server which will accept more than one connection - at the moment i have: s = new ServerSocket(socket); for the socket. Any help would be great!

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

    Default

    Quote Originally Posted by pinkette View Post
    Hey guys, i would like to multi-thread a Socket connection in Java. So for example its a server which will accept more than one connection - at the moment i have: s = new ServerSocket(socket); for the socket. Any help would be great!
    When you accept() from your ServerSocket it returns a connected Socket. Start a new Thread and give it the Socket so it can communicate with the client. Your original thread can accept() other connected Sockets.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Go through this useful link : Socket Communications

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    pinkette is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    try {
    s = new ServerSocket(socket);
    } catch (IOException e) { }
    while (true) {
    try {

    s1 = s.accept();

    os = s1.getOutputStream();
    dos = new DataOutputStream (os);

    try{
    Thread.sleep(delay);
    }
    catch(InterruptedException e){}

    dos.writeUTF(sendString);

    dos.close();
    os.close();
    s1.close();

    } catch (IOException e)

    So this is what i have, how would i incorperate it?

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

    Default

    A few remarks:

    1) stick "[code]" before your program code and "[/code]" after it for readablility reasons.
    2) why do you make your thread sleep for a while?
    3) I don't see any attempt to use multiple Threads.
    4) Incorporate that bit of code into what?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    pinkette is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Yeah, basiclly i can input a delay for the server (to simulate latency). At the moment my server will only support one client at a time - thats why id like to be able to support multiple connections. Im new to Java so dont know how to incorprate it! Many thanks.

  7. #7
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    Simply create a connhandler class in your server class
    The idea is to create a new instance starting at the socket connection

    Java Code:
     private class connHandler implements Runnable{
    
            private Socket client;
            public connHandler(){
                try {
                    System.out.println("Waiting.....");
                    client = server.accept();
                    System.out.println("Connection Established");
                    Thread t = new Thread(this);
                    t.start();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    
            public void run() {
                  // whatever code you want to run
            }
    and just write this in the constructor:

    Java Code:
    s = new ServerSocket(socket)
    while(true){
         connHandler handler = new connHandler();
    }
    Last edited by bayan; 01-13-2011 at 06:52 PM.

  8. #8
    pinkette is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Thanks for your reply. Im just confused as to where it would go. I have some variables above the code i pasted. and then the gui information. I have a few text boxes - one for the delay and one for the Socket Port. Would any of that need to change?

  9. #9
    pinkette is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Would i put all my existing code inside where it says "run what ever code you want "?

Similar Threads

  1. null pointer exception in multi threading
    By niteangell21 in forum New To Java
    Replies: 5
    Last Post: 10-20-2010, 05:48 PM
  2. Problem in Multi threading.
    By Chetans in forum Advanced Java
    Replies: 3
    Last Post: 03-23-2010, 04:42 PM
  3. Newbie to multi-threading please direct me :)
    By kminev in forum Threads and Synchronization
    Replies: 8
    Last Post: 11-18-2009, 06:11 PM
  4. Multi-Threading and process control in Java
    By mo_mughrabi in forum Advanced Java
    Replies: 2
    Last Post: 08-16-2009, 01:47 PM
  5. question about Multi threading in Java
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 01:55 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
  •