JAVA Calculator via Socket (Client-Server)

Asked

Viewed 131 times

1

I see you have other cases here on the site, but in my case it’s different, because it needs to be a server that works on this client, with no power alter it.

All the server you have here I tested, but it didn’t work, by please ask for your help.

I need to create a server that receives customer data process, and return the result to the client, a JAVA calculator that communicates via Soket.

Since the client code is already ready and can not be changed anything, follow the code below.

package Calculadora;

import java.net.*;
import java.io.*;
public class Cliente {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

            try {
            Socket s = new Socket("127.0.0.1", 9999);
            InputStream i = s.getInputStream();
            OutputStream o = s.getOutputStream();
            String str;
            byte[] line1 = new byte[100];// operando 1
            byte[] line2 = new byte[100]; // operando 2
            byte[] line3 = new byte[100]; // operador
            byte[] line4 = new byte[100]; // resultado da operacao
            System.out.println("digite o primeiro numero");
            System.in.read(line1);
            System.out.println("digite o segundo numero");
            System.in.read(line2);
            System.out.println("digite o operador - + * /");
            System.in.read(line3);

            o.write(line1);
            o.write(line2);
            o.write(line3);

            i.read(line4);
            str = new String(line4);
            System.out.println("RESULTADO:" + str.trim());
            s.close();
            }
            catch (Exception err) {
            System.err.println(err);
            }
            }


    }

Server

package Calculadora;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Servidor {

    public static void main (String[] args) throws IOException {

        double line1, line2, line4 = 0.0;
        int line3;
        char opr = '\n';

        ServerSocket receb = new ServerSocket(9999);
        System.out.println("Porta 9999 aberta!");

        System.out.print("Aguardando conexão do cliente...");
        Socket s = receb.accept();

        System.out.println("Nova conexao com o cliente ");


        ObjectInputStream i = new ObjectInputStream(s.getInputStream());
        ObjectOutputStream o = new ObjectOutputStream(s.getOutputStream());


        line1 = i.read();
        line2 = i.read();
        line3 = i.read();

        if (line3 == '+') {

            opr = '+';
            line4 = (line1 + line2);

        } else if (line3 == '-') {

            opr = '-';
            line4 = (line1 - line2);

        } else if (line3 == '*') {

            opr = '*';
            line4 = (line1 * line2);

        } else {

            opr = '/';
            line4 = (line1 / line2);

        }

        o.writeDouble(line4);
        o.writeChar(opr);
        o.flush();

        i.close();
        o.close();
        i.close();

    }

}

When executing this giving error:

java.net.Socketexception: Connection reset

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.