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 } } } }