Tentativa de fazer uma conexão entre Cliente Servidor e Fornecedor

0 respostas
R

Eis o código

Para o servidor:

package tcpservergroup;

import java.io.*;
import java.net.*;
import messageFactory.myMsg;

public class TCPServerGroup {

	public static final int SERVICE_PORT = 13;


	public static void main(String args[]) {
		try {
			
			ServerSocket server = new ServerSocket (SERVICE_PORT);
			System.out.println ("Group server start...");
			
			while(true) {
				TCPServerGroupThread myServer = new TCPServerGroupThread(server.accept());
	            myServer.start();
			}
			
		}
		catch (BindException be) {
			System.err.println ("Service already running on port " + SERVICE_PORT );
		}
		catch (IOException ioe) {
			System.err.println ("I/O error - " + ioe);
		}
	}
	
}

Thread do Servidor:

package tcpservergroup;

import java.net.*;
import java.io.*;
import messageFactory.myMsg;


public class TCPServerGroupThread extends Thread {
	
	  private Socket conexao;
	  private ObjectInputStream input;
	  private ObjectOutputStream output;

	  
	  
	  public TCPServerGroupThread(String str) {
		  super(str);
	  }

	  public TCPServerGroupThread(Socket con) {
		  conexao = con;
		  try {
			  input = new ObjectInputStream(conexao.getInputStream());
			  output = new ObjectOutputStream(conexao.getOutputStream());
	      } catch(IOException e) {
	    	  e.printStackTrace();
	    	  System.exit(1);
	      }
	  }

	  public void run(){
		  while(true) {
			  try {
				  
				  myMsg message = (myMsg) input.readObject();
				  System.out.println("A wholesaler is registered on the server.");
				  System.out.println("Port: "+ message.getPort() +"Product's Code: " + message.getProductCode());
				  
			  } catch (ClassNotFoundException cnfe) {
				  cnfe.printStackTrace();
			  } catch (IOException ioe) {
				  ioe.printStackTrace();
			  }
		  }
	 }

}

E o primeiro carinha que eu quero registrar seria um fornecedor que eu chamei de Atacadista(Wholesaler):

package wholesaler;

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

import messageFactory.*;


public class Wholesaler {
	
	public Socket wsSocket;
	public int wsPort;
	public int productCode;
	public int[] productPrices = new int[3];
	public int[] productQty = new int[3];
	
	public Wholesaler(String port, String pCode) {
		try {
			
			this.wsSocket = new Socket("localhost",Integer.valueOf(port));
			this.productPrices[0] = (int) Math.random() * 100;
			this.productPrices[1] = (int) Math.random() * 100;
			this.productPrices[2] = (int) Math.random() * 100;
			this.productQty[0] =(int) Math.random() * 10;
			this.productQty[1] =(int) Math.random() * 10;
			this.productQty[2] =(int) Math.random() * 10;
			this.productCode = Integer.valueOf(pCode);
			this.wsPort = Integer.valueOf(port);
			
		} catch (IOException ioe) {
			System.err.println ("I/O error - " + ioe);
		}

	}

	public int[] getProductPrices() {
		return productPrices;
	}

	public void setProductPrices(int[] productPrices) {
		this.productPrices = productPrices;
	}

	public int obterProposta(int codProduto, int qtd, String nomeCliente){
		return 0;
	}
	
	public boolean fazerCompra(int notaEmpenho, String codProposta) {
		return false;
	}
	
	public boolean registrarFornecedor(String servidor, int porta, int codProduto) throws IOException {
		
		ObjectOutputStream output = new ObjectOutputStream(wsSocket.getOutputStream());
		myMsg message = new myMsg();
		message.setPort(porta);
		message.setProductCode(codProduto);
		output.writeObject(message);
		output.flush();
		wsSocket.close();
		
		return true;
	}
	
	public static void main(String args[]) throws IOException {
				
		Wholesaler ws = new Wholesaler(args[0], args[1]);
		ws.registrarFornecedor("",ws.wsPort,ws.productCode);

	}

	
}

Bem, ao executar o TCPServerGroup ele imprime a mensagem dizendo que o servidor foi iniciado daí eu configuro a execução do Wholesaler com a porta 13 e código de produto 100. O problema é que está gerando uma exceção EOFE. Como eu sou novo nessa parte de enviar Objetos por sockets e tal por favor me ajudem. Qualquer dica é válida e se conseguirem constatar o problema por favor me respondam. Obrigado desde já

Criado 27 de março de 2011
Respostas 0
Participantes 1