George
George

Reputation: 157

How to ignore exception

I am using a UDP socket to send then receive a message.

So when I receive I set the timeout exception to 4 seconds...

sending_socket.ReceiveTimeout = 4000;
sending_socket.ReceiveFrom(ByteFromListener, ref receiving_end_point);

Now I get this exception (which I am expecting) : An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll

Additional information: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

I wanted to know how can i ignore this exception?

Basicly I want the UDPSOCKET to listen for 4 seconds and if no answer then try to send a message again.. My code is the following (part of it)

 IPEndPoint sending_end_point = new IPEndPoint(sendto, sendPort);
        EndPoint receiving_end_point = new IPEndPoint(IPAddress.Any, 0);           

        Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        text_to_send = ("hello");
        byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);
        sending_socket.SendTo(send_buffer, sending_end_point);
        Byte[] ByteFromListener = new byte[8000];
        sending_socket.ReceiveTimeout = 4000;
        sending_socket.ReceiveFrom(ByteFromListener, ref receiving_end_point);
        string datafromreceiver;
        datafromreceiver = Encoding.ASCII.GetString(ByteFromListener).TrimEnd('\0');
        datafromreceiver = (datafromreceiver.ToString());

Upvotes: 1

Views: 1648

Answers (2)

Manish Dalal
Manish Dalal

Reputation: 1796

Instead of checking for exception, I suggest that you use sending_socket.Available (Read on MSDN) property.

You can add a logic where you check for the time elapsed since you sent the data and then if Available is not yet true, try to send again. Something like below:

bool data_received = false;
do
{
    DateTime dtSent;
    sending_socket.SendTo(send_buffer, sending_end_point);
    dtSent = DateTime.Now;

    while(DateTime.Now - dtSent < TimeSpan.FromSeconds(4))
    {
       while(sending_socket.Available)
       {
          int bytes_available = sending_socket.Available;
          // you can use bytes_available variable to create a buffer of the required size only.
          //read data... and concatenate with previously received data, if required
          data_received = true;  
       }
       Thread.Sleep(100); // sleep for some time to let the data arrive
    }
}while(!data_received);

The above code is only a simple sample logic. Kindly modify it as per your requirement.

I strongly suggest that you do not depend on exceptions to handle cases which you already know may happen. Exceptions are meant to handle those cases which cannot be known in advance and where there is no mechanism to check for.

Also, SocketException can be raised for other reasons like Endpoint was not available, connection get lost due to any reason. Exception should be handled for these scenarios so that your code can handle those scenarios properly.

Upvotes: 1

Paviel Kraskoŭski
Paviel Kraskoŭski

Reputation: 1419

try
{
     sending_socket.ReceiveFrom(ByteFromListener, ref receiving_end_point);
}
catch (SocketException ex) { }

Upvotes: 5

Related Questions