Comunicação full duplex servidor cliente

0 respostas
L

É o seguinte… já fiz uma comunicação entre o servidor e o cliente, consegui enviar pacotes do cliente para o servidor. Agora, tenho que fazer com que o servidor também envie pacotes para os clientes… como faço isso utilizando ObjectOutputStream e ObjectInputStream?

Aqui vão os códigos já feitos…

Classe Servidor

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

public class Servidor{

private ServerSocket server;

private Socket sock;
public Servidor(){}

public void init(){

try{

this.server = new ServerSocket(1234);

this.sock = this.server.accept();

}

catch(IOException e){

System.out.println(e.getMessage());

}

}
public Socket getSocket(){
	return sock;
}

public static void main(String args[]) throws ClassNotFoundException{
	try{
		Servidor serv = new Servidor();
		
		for(;true;){
			serv.init();
			
			ObjectInputStream entradaDeObjetos = new ObjectInputStream(serv.getSocket().getInputStream());
			Pacote pacote = (Pacote) entradaDeObjetos.readObject();
			System.out.println(pacote.getNome());
			System.out.println(pacote.getIdade());
			System.out.println(pacote.getCurso());
			serv.sock.close();
			serv.server.close();
		}
	}
	catch(IOException e){
		System.out.println(e.getMessage());
	}
}

}

Classe Cliente

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

public class Cliente{

private Socket client;

private ObjectOutputStream saidaDeObjetos;
public Cliente(String ip){
	try{
		this.client = new Socket(ip,1234);
	}
	catch(IOException e){
		System.out.println(e.getMessage());
	}
}

public Socket getSocket(){
	return client;
}

public static void main(String args[]){
	Pacote aluno = new Pacote();
	aluno.setNome(args[1]);
	aluno.setIdade(Integer.parseInt(args[2]));
	aluno.setCurso(args[3]);
	try{
		Cliente cli = new Cliente(args[0]);
		ObjectOutputStream saidaDeObjetos = new ObjectOutputStream(cli.getSocket().getOutputStream());
		saidaDeObjetos.writeObject(aluno);
		cli.client.close();
	}
	catch(IOException e){
		System.out.println(e.getMessage());
	}
}

}

class Pacote implements Serializable{

private static final long serialVersionUID = 5649944766337233807L;
private String nome;
private int idade;
private String curso;

public String getNome(){
	return nome;
}

public int getIdade(){
	return idade;
}

public String getCurso(){
	return curso;
}

public void setNome(String nome){
	this.nome = nome;
}

public void setIdade(int idade){
	this.idade = idade;
}

public void setCurso(String curso){
	this.curso = curso;
}

}

Obrigada desde já…

Criado 1 de junho de 2009
Respostas 0
Participantes 1