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

Thread: Java Messenger Program?

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java Messenger Program?

    Well here's basically what I'm trying to accomplish.
    I'm trying to write a program that when run on two computers, both can send messages and the messages will appear on each one of the computers.
    Basically, it's like aim.

    Here's what I have so far, but this only works on the running computer, doesn't really allow other computers to connect to mine.
    I'm wondering what I need to do to make this work so multiple computers can connect with each other.
    Also I wasn't sure were to post this, I don't think this is much of a newbie project, but it isn't that advanced

    Lesson: All About Sockets (The Java™ Tutorials > Custom Networking)

    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    import java.util.Iterator;
     
    public class MusicServer {
    ArrayList<ObjectOutputStream> clientOutputStreams;
    public static void main(String[] args) {
        new MusicServer().go();
     
    }
    public class ClientHandler implements Runnable{
    ObjectInputStream in;
    Socket clientSocket;
    public ClientHandler(Socket socket) {
    try {
    clientSocket = socket;
    in = new ObjectInputStream(clientSocket.getInputStream());
    } catch (Exception e) {
    System.out.println(e);
    }//close constructor
    }
    public void run(){
        Object o2 = null;
        Object o1 = null;
        try {
            while((o1 = in.readObject())!=null) {
            o2 = in.readObject();
            System.out.println("Read two objects");
            tellEveryone(o1,o2);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    }
    public void go() {
    clientOutputStreams = new ArrayList<ObjectOutputStream>();
    try {
        ServerSocket serverSock = new ServerSocket(4444);
        while(true) {
            Socket clientSocket = serverSock.accept();
        ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
        clientOutputStreams.add(out);
        Thread t = new Thread(new ClientHandler(clientSocket));
        t.start();
        System.out.println("Got a connection");
        }
    } catch (Exception e) {
        e.printStackTrace();
        // TODO: handle exception
    }
    }
     
    public void tellEveryone(ObjectOutputStream one, Object two) {
    Iterator it = clientOutputStreams.iterator();
    while(it.hasNext()) {
    try {
    ObjectOutputStream out = (ObjectOutputStream)it.next();
    out.writeObject(one);
    out.writeObject(two);
    } catch (Exception e) {
        // TODO: handle exception
    }
    }
    }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2009
    Location
    Bangalore.India
    Posts
    9
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Java Messenger Program?

    I have done this using RMI instead of socket programming.If you have problem with normal java RMI, then use the Spring
    RMI samples available in to the net.

    Good Luck

Similar Threads

  1. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM
  2. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM
  3. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 01:14 AM
  4. Java Program Help
    By javakid93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 27th, 2009, 11:03 AM
  5. Redirect error and output stream using java program
    By leenabora in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 16th, 2009, 04:12 AM