Distributed Calculator - UDP Client/Server

Asked

Viewed 438 times

-1

I am trying to make a distributed calculator using Client and UDP Server, the client sends 3 numbers to the server. The 1st option would be chosen, example (1 - sum, 2 - subtraction, 3 - division) the other two numbers would be the operands. The problem is that when I use the Encoding.ASCII.GetString(data,0, receivedDataLength); I cannot 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 tried to convert the others to double and perform the operations, but without success. Follow full 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();
        }
    }
}
  • UDP ? what if the package is not received ? You need to define a protocol

  • There are 3 simple datagrams, the chance of error is minimal and using UDP is an order from the teacher to work :/

  • although you disagree... that is... you need to define a protocol... suggestion, separate each value by ;

  • I thought about this possibility... but this 3 "Sendto" are sent at once, some idea of how to separate after already received on the server?

  • https://msdn.microsoft.com/pt-br/library/tabh47cf%28v=vs.110%29.aspx? f=255&Mspperror=-2147217396

1 answer

-1

You do 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 package.

Browser other questions tagged

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