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 4 of 4

Thread: asynchronous communication instant messenger program......

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default asynchronous communication instant messenger program......

    I am in networking and did not expect to run into this, this is my code below. I must turn this instant messenger program into an asynchronous communication instant messenger program. What to do to make it to make it asychronous? Here is my code:

    import java.io.*;
    import java.net.*;

    class server
    {
    public static void main(String[] args)
    {
    try {
    Socket s = (new ServerSocket(2010)).accept();
    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    String str = br.readLine();
    System.out.println(str);
    (new DataOutputStream(s.getOutputStream())).writeBytes( str + "\n");
    } catch (IOException e) {
    System.out.println(e);
    }
    }
    }






    import java.io.*;
    import java.net.*;

    class client
    {
    public static void main(String[] args)
    {
    try {
    Socket s = new Socket("localhost", 2010);
    (new DataOutputStream(s.getOutputStream())).writeBytes( "Hello, World\n");
    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    System.out.println(br.readLine());
    } catch (IOException e) {
    System.out.println(e);
    };
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: asynchronous communication instant messenger program......

    See the following link for a tutorial on the tools you need: Lesson: Concurrency (The Java™ Tutorials > Essential Classes)

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: asynchronous communication instant messenger program......

    Reposted with proper highlights and code tags.... O_o
    [CODE]
    import java.io.*;
    import java.net.*;
     
    class server
    {
     public static void main(String[] args)
     {
      try {
       Socket s = (new ServerSocket(2010)).accept();
       BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
       String str = br.readLine();
       System.out.println(str);
       (new DataOutputStream(s.getOutputStream())).writeBytes(str + "\n");
      } catch (IOException e) {
       System.out.println(e);
      }
     }
    }
     
     
     
     
     
     
    import java.io.*;
    import java.net.*;
     
    class client 
    {
     public static void main(String[] args)
     {
      try {
       Socket s = new Socket("localhost", 2010);
       (new DataOutputStream(s.getOutputStream())).writeBytes("Hello, World\n");
       BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
       System.out.println(br.readLine());
      } catch (IOException e) {
       System.out.println(e);
      };
     }
    }
    [/CODE]

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: asynchronous communication instant messenger program......

    So what do you mean by asynchronous? You can't have one run without the other... Do you mean run them within the same program?

    All that requires is multi-threading. Host code in the main method and client code goes in a Runnable object, executed by a new Thread.

Similar Threads

  1. I am trying to make a messenger program!
    By piulitza in forum Java Networking
    Replies: 13
    Last Post: December 21st, 2011, 12:38 PM
  2. Objects communication issue
    By vasemax in forum Object Oriented Programming
    Replies: 10
    Last Post: August 24th, 2011, 11:44 AM
  3. [SOLVED] Asynchronous Imaging Problem
    By dr3wmurphy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2011, 12:59 AM
  4. communication protocol
    By isaac in forum Java Networking
    Replies: 1
    Last Post: February 5th, 2010, 07:20 AM
  5. Java Messenger Program?
    By MysticDeath in forum Java Networking
    Replies: 1
    Last Post: September 8th, 2009, 11:25 PM