title: When to close a UDP socket
body: <p>I have a client-server application that uses a UDP socket to send the data , the data only have to travel from client to server , and the server will always have the same IP. The only requirement is that I have to send messages about 10 messages per second</p>
  
  <p>Currently I am doing it the following way :</p>
  
  <pre><code>public void SendData(byte[] packet)
  {
      IPEndPoint end_point = new IPEndPoint(serverIP, serverPort);
      UdpClient udpChannel = new UdpClient(sourcePort);
      udpChannel.Connect(end_point);
      udpChannel.Send(packet, packet.Length);
      udpChannel.Close();
  }
  </code></pre>
  
  <p>The problem I have is that when I use the command "udpChannel.Close()" it takes 2-3 seconds to be performed when the server is not listening. (I've seen the same problem in: <a href="https://stackoverflow.com/questions/12776563/what-is-the-drawback-if-i-do-not-invoke-the-udpclient-close-method">What is the drawback if I do not invoke the UdpClient.Close() method?</a>)</p>
  
  <p>My question would be, if I always send packets to the same IP address and port, is it necessary to connect the socket and close it after each send request?</p>
  
  <p>The code I intend to use would be as follows:</p>
  
  <pre><code>UdpClient udpChannel;
  
  public void SendData(byte[] packet)
  {
      udpChannel.Send(packet, packet.Length);
  }
  
  public void Initialize(IPAddress IP, int port)
  {
      IPEndPoint end_point = new IPEndPoint(serverIP, serverPort);
      UdpClient udpChannel = new UdpClient(sourcePort);
      udpChannel.Connect(end_point);
  }
  
  public void Exit()
  {
      udpChannel.Close();
  }
  </code></pre>
  
  <p>Doing it this way, would it be necessary to do some checking in the "SendData" method before sending the data?
  Is there any problem in the above code?</p>
  
  <p>Thank you!</p>