Distributed Calculator - UDP Client / Server

0

I'm trying to make a Distributed Calculator using Client and UDP Server, the client sends 3 numbers to the server. The 1st would be the chosen option, example (1 - sum, 2 - subtraction, 3 - division) the other two numbers would be the operands. The problem is that when I use Encoding.ASCII.GetString(data,0, receivedDataLength); I can not handle the string separately.

   if (recv.Substring(0,1).Equals(1))
            {
                double num1 = Double.Parse(recv.Substring(1,1));
                double num2 = Double.Parse(recv.Substring(2,1));
                double resul = num1 + num2;
                Console.WriteLine("ESSE É O RESULTADO: "+resul);
            }

I tried this to say that the first char was the sum, then I tried to convert the others to double and perform the operations, but to no avail. Follow the complete code

Server:

//SERVIDOR
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MainClass
{
    public static void Main()
    {
        string recv;

        int receivedDataLength;
        byte[] data = new byte[1024];

        IPEndPoint ip = new IPEndPoint(IPAddress.Any,55555);

        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        socket.Bind(ip);

        IPEndPoint sender = new IPEndPoint(IPAddress.Any,55555);
        EndPoint Remote = (EndPoint)(sender);

        while (true)
        {

            int i = 0;

            data = new byte[1024];
            receivedDataLength = socket.ReceiveFrom(data, ref Remote);

            //Console.WriteLine(Encoding.ASCII.GetString(data, 0, receivedDataLength));
            recv = Encoding.ASCII.GetString(data,0, receivedDataLength);

            if (recv.Substring(0,1).Equals(1))
            {
                double num1 = Double.Parse(recv.Substring(1,1));
                double num2 = Double.Parse(recv.Substring(2,1));
                double resul = num1 / num2;
                Console.WriteLine("ESSE É O RESULTADO: "+resul);
            }

            socket.SendTo(data, receivedDataLength, SocketFlags.None, Remote);
        }


    }
}

Client:

//CLIENTE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Digite a opção desejada");

            Console.WriteLine("1 - SOMA");
            Console.WriteLine("2 - SUBTRAÇÃO");
            Console.WriteLine("3 - DIVISÃO");
            Console.WriteLine("4 - MULTIPLICAÇÃO");
            Console.WriteLine("0 - SAIR\n");
            String op = Console.ReadLine();

            Console.Write("Digite um numero:  ");
            string num = Console.ReadLine();
            Console.Write("Digite o outro numero:  ");
            string num2 = Console.ReadLine();

            byte[] pkg = System.Text.ASCIIEncoding.ASCII.GetBytes(op);
            byte[] pkg2 = System.Text.ASCIIEncoding.ASCII.GetBytes(num);
            byte[] pkg3 = System.Text.ASCIIEncoding.ASCII.GetBytes(num2);

            string IP = "127.0.0.1";
            int porta = 55555;

            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), porta);



            Socket cliente = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            cliente.SendTo(pkg, ep);
            cliente.SendTo(pkg2, ep);
            cliente.SendTo(pkg3, ep);

            Console.ReadKey();
        }
    }
}
    
asked by anonymous 11.03.2018 / 01:10

1 answer

0

You make 3 SendTo on the client, so you need to do 3 ReceiveFrom on the server. The operation and the 2 operands are being sent separately, each in its own UDP packet.

    
12.03.2018 / 11:40