Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: one java progarm handle two socket connection

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default one java progarm handle two socket connection

    Hi,
    I need to write a java program handle two sockect connection A ,B .
    So, in normal case ,one connection , the program will read data from connection and process it .
    Then read data from connection again and process it .
    Work like a while loop .

    But if I need to handle two socket connection ,how can I write in one program .
    Apart from read data from A ,process it , then read data from B , process it .
    work like A ,B ,A,B sequential .

    Any other method I can write in this program to handle two connection.
    As, it is not promised that there are data in the connection , sometime there are no data in the message queue of the two connection .


    Please advice ...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: one java progarm handle two socket connection

    Is the program to work as a server or as a client?
    You can use more than one ServerSocket or Socket statement in your program.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: one java progarm handle two socket connection

    Quote Originally Posted by Norm View Post
    Is the program to work as a server or as a client?
    You can use more than one ServerSocket or Socket statement in your program.
    I understand I can creat more than one socket connection .
    but how can I read the data from socket connection .
    If I have A,B connection , so, I read A , then B ,then A ,then B ..
    Apart from this , any interrupt method , so, if there is data in connection A , it can call the read function auto ????
    Please mention more detailed for your suggestion

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: one java progarm handle two socket connection

    When the Socket connects you can get I/O streams from the socket object and read from that. Look at the API doc for the methods to get the streams.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: one java progarm handle two socket connection

    Quote Originally Posted by Norm View Post
    When the Socket connects you can get I/O streams from the socket object and read from that. Look at the API doc for the methods to get the streams.
    Any example/sample code for eventhandler to monitor connection or string ????
    Any example ???

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: one java progarm handle two socket connection

    Do a search for code that uses Sockets.

    When a method that is receiving input over a socket gets data, it could call another method and pass it the input it received.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 12
    Last Post: September 3rd, 2011, 07:07 AM
  2. Creating an API to do socket connection J2ME
    By mikeotieno in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: July 13th, 2011, 02:45 AM
  3. Replies: 0
    Last Post: March 26th, 2011, 11:07 AM
  4. Replies: 0
    Last Post: February 24th, 2011, 06:31 AM
  5. problem with closing connection to client socket
    By sunitha in forum Java Networking
    Replies: 1
    Last Post: December 11th, 2010, 04:28 AM