Bom, na apostila da caelum tem um exemplo muito interessante disso, se não me engano na primeira.
aqui vai um exemplo implementado, dá uma olhada, pois ele funciona em rede local, na net nunca testei:
package testes;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
*
* @author xjunior
*/
public class Cliente {
private String host;
private int porta;
public Cliente (String host, int porta) {
this.host = host;
this.porta = porta;
}
public void executa() throws UnknownHostException, IOException {
Socket cliente = new Socket(this.host, this.porta);
System.out.println("O cliente se conectou ao servidor!");
// thread para receber mensagens do servidor
Recebedor r = new Recebedor(cliente.getInputStream());
new Thread(r).start();
// le msgs do teclado e manda pro servidor
Scanner teclado = new Scanner(System.in);
PrintStream saida = new PrintStream(cliente.getOutputStream());
while (teclado.hasNextLine()) {
saida.println(teclado.nextLine());
}
saida.close();
teclado.close();
cliente.close();
}
}
package testes;
import java.io.IOException;
import javax.swing.JOptionPane;
/**
*
* @author xjunior
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[]args) throws IOException{
String Door;
String IP;
if(JOptionPane.showConfirmDialog(null, "Deseja Usar a conta Servidor?")==0){
Door = JOptionPane.showInputDialog("Favor Inserir o numero da porta");
Servidor Serv = new Servidor(Integer.parseInt(Door));
Serv.executa();
}else{
Door = JOptionPane.showInputDialog("Favor Inserir o numero da porta do Servidor:");
IP = JOptionPane.showInputDialog("Insira o endereço do servidor:");
Cliente Cli = new Cliente(IP, Integer.parseInt(Door));
Cli.executa();
}
}
}
package testes;
import java.io.InputStream;
import java.util.Scanner;
/**
*
* @author xjunior
*/
class Recebedor implements Runnable {
private InputStream servidor;
public Recebedor(InputStream servidor) {
this.servidor = servidor;
}
public void run() {
// recebe msgs do servidor e imprime na tela
Scanner s = new Scanner(this.servidor);
while (s.hasNextLine()) {
System.out.println(s.nextLine());
}
}
}
package testes;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author xjunior
*/
public class Servidor {
private int porta;
private boolean encerrar;
private List<PrintStream> clientes;
public Servidor (int porta) {
this.porta = porta;
this.clientes = new ArrayList<PrintStream>();
}
public void executa () throws IOException {
ServerSocket servidor = new ServerSocket(this.porta);
System.out.println("Porta "+this.porta+" aberta!");
while (!encerrar) {
// aceita um cliente
Socket cliente = servidor.accept();
System.out.println("Nova conexão com o cliente " +
cliente.getInetAddress().getHostAddress()
);
// adiciona saida do cliente à lista
PrintStream ps = new PrintStream(cliente.getOutputStream());
this.clientes.add(ps);
// cria tratador de cliente numa nova thread
TrataCliente tc = new TrataCliente(cliente.getInputStream(), this);
new Thread(tc).start();
}
}
public void distribuiMensagem(String msg) {
// envia msg para todo mundo
for (PrintStream cliente : this.clientes) {
cliente.println(msg);
}
}
}
package testes;
import java.io.InputStream;
import java.util.Scanner;
/**
*
* @author xjunior
*/
class TrataCliente implements Runnable{
private InputStream cliente;
private Servidor servidor;
public TrataCliente(InputStream cliente, Servidor servidor) {
this.cliente = cliente;
this.servidor = servidor;
}
public void run() {
// quando chegar uma msg, distribui pra todos
Scanner s = new Scanner(this.cliente);
while (s.hasNextLine()) {
servidor.distribuiMensagem(s.nextLine());
}
s.close();
}
}
Cara, implementei em java SE, num sei se vai ajudar, mas testa aí, e vê se vai funcionar fora da rede, pois nao tenho nem como testar agora, flw mano, espero ter ajudado!