ObjectOutputStream readInt() doesn't work
I'm doing a calculador in Java with sockets. The server receives the
operation code (Add: 1, Product: 3) and the numbers which send the client.
I have to use ObjectOutputStream and ObjectInputStream for writing and
reading the data, but I have a problem.
The client writes the operation code but the Server doesn't receive, but
if I try to send some data from the server to client, it works.
PD. I'm using threads. And the program doesn't send an exception. The
client write the data but the server is waiting for receving it forever.
This is my code:
Servidor.java
package com.distribuidos.calculadora.servidor;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author Israel
*/
public class Servidor extends Thread{
private ServerSocket serverSocket = null;
private static final int PUERTO = 6666;
public Servidor()
{
try {
serverSocket = new ServerSocket(PUERTO);
System.out.println("Funcionando");
} catch (IOException ioe) {
ioe.printStackTrace();
}
start();
}
@Override
public void run()
{
try {
while(true){
System.out.println("Listo para recibir conexiones");
Socket socketCliente = serverSocket.accept();
System.out.println("Cliente conectado de " +
socketCliente.getInetAddress());
Operaciones operacion = new Operaciones(socketCliente);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String[] args) {
new Servidor();
}
}
Operaciones.java
package com.distribuidos.calculadora.servidor;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
/**
*
* @author Israel
*/
public class Operaciones extends Thread {
private ObjectOutputStream oos = null;
private ObjectInputStream ois = null;
private Socket socketCliente;
private int operacion;
private final static int SUMA = 6;
private final static int RESTA = 1;
private final static int MULTIPLICACION = 2;
private final static int DIVISION = 3;
private final static int FACTORIAL = 4;
private final static int FIBONACCI = 5;
public Operaciones(Socket socketCliente) {
this.socketCliente = socketCliente;
try {
oos = new ObjectOutputStream(socketCliente.getOutputStream());
oos.flush();
ois = new ObjectInputStream(socketCliente.getInputStream());
} catch (IOException ioe) {
ioe.printStackTrace();
try {
socketCliente.close();
} catch (Exception ee) {
ee.printStackTrace();
}
return;
}
start();
}
@Override
public void run(){
try {
oos.writeObject(new String("conectado"));
System.out.println("Escribi al cliente");
System.out.println("Corriendo hilo");
System.out.println(socketCliente.isConnected());
operacion = ois.readInt();
System.out.println("Lei operacion " + operacion);
switch(operacion)
{
case SUMA:
oos.writeFloat(Suma(ois.readFloat(), ois.readFloat()));
break;
case RESTA:
oos.writeFloat(Resta(ois.readFloat(), ois.readFloat()));
break;
case MULTIPLICACION:
oos.writeFloat(Multiplicacion(ois.readFloat(),
ois.readFloat()));
break;
case DIVISION:
oos.writeFloat(Division(ois.readFloat(),
ois.readFloat()));
break;
case FACTORIAL:
oos.writeInt(Factorial(ois.readInt()));
break;
case FIBONACCI:
oos.writeInt(Fibonacci(ois.readInt()));
break;
default:
oos.writeInt(0);
break;
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
socketCliente.close();
ois.close();
oos.close();
} catch (IOException e) {
}
}
}
private float Suma(float numero1, float numero2)
{
return numero1 + numero2;
}
private float Multiplicacion(float numero1, float numero2)
{
return numero1 * numero2;
}
private float Resta(float numero1, float numero2)
{
return numero1 - numero2;
}
private float Division(float numero1, float numero2)
{
return numero1 / numero2;
}
private int Factorial(int numero)
{
int resultado = 1;
for (int i = numero; i > 0 ; i--)
resultado *= numero;
return resultado;
}
private int Fibonacci(int numero)
{
if(numero<2)
return numero;
else
return Fibonacci(numero-1) + Fibonacci(numero-2);
}
}
Cliente.java
package com.distribuidos.calculadora.cliente;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* @author Israel
*/
public class Cliente {
private Socket socket = null;
private ObjectOutputStream oos = null;
private ObjectInputStream ois = null;
private static final int PUERTO = 6666;
public Cliente()
{
try {
socket = new Socket("localhost", PUERTO);
oos = new ObjectOutputStream(socket.getOutputStream());
oos.flush();
ois = new ObjectInputStream(socket.getInputStream());
} catch (UnknownHostException uhe) {
System.err.println(uhe.getMessage());
throw new RuntimeException();
}
catch (IOException ioe)
{
System.err.println(ioe.getMessage());
throw new RuntimeException();
}
}
public float leerFloat()
{
try {
return ois.readFloat();
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
throw new RuntimeException();
}
}
public int leerInt()
{
try {
return ois.readInt();
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
throw new RuntimeException();
}
}
public void escribirFloat(float numero)
{
try {
oos.writeFloat(numero);
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
throw new RuntimeException();
}
}
public void escribirEntero(int numero)
{
try {
oos.writeInt(numero);
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
public void escribirOperacion(int operacion)
{
try {
oos.writeInt(operacion);
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
throw new RuntimeException();
}
}
public void close()
{
try {
socket.close();
ois.close();
oos.close();
} catch (Exception e) {
}
}
public Socket getSocket()
{
return this.socket;
}
public String leerString()
{
String cad="";
try {
cad = (String)ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return cad;
}
}
GUI.java
private void btnIgualActionPerformed(java.awt.event.ActionEvent evt) {
try {
initCliente();
System.out.println(cliente.leerString());
cliente.escribirEntero(operacion);
System.out.println(cliente.getSocket().isConnected());
numeroDos = Integer.parseInt(txtCantidad.getText());
System.out.println("Primer operando " + numeroUno);
System.out.println("Segundo operando " + numeroDos);
System.out.println(cliente.getSocket().isConnected());
System.out.println(cliente.getSocket().isConnected());
System.out.println("Escribi la operacion " + operacion);
switch(operacion)
{
case SUMA:
case RESTA:
case MULTIPLICACION:
case DIVISION:
cliente.escribirFloat(numeroUno);
System.out.println("Escribi el primer numero");
cliente.escribirFloat(numeroDos);
System.out.println("Escribi el segundo numero");
txtCantidad.setText(Float.toString(cliente.leerFloat()));
System.out.println("Recibi el resultado");
break;
case FACTORIAL:
case FIBONACCI:
cliente.escribirEntero((int)numeroUno);
txtCantidad.setText(Integer.toString(cliente.leerInt()));
default:
cliente.escribirFloat(0);
cliente.escribirFloat(0);
txtCantidad.setText("0");
}
cliente.close();
} catch (Exception e)
{
}
}
Thanks for your help.
No comments:
Post a Comment