When to close a UDP socket

不问归期 提交于 2020-04-10 02:07:08

问题


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

Currently I am doing it the following way :

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();
}

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: What is the drawback if I do not invoke the UdpClient.Close() method?)

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?

The code I intend to use would be as follows:

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();
}

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?

Thank you!


回答1:


UDP is connectionless, calling udpChannel.Connect merely specifies a default host endpoint for use with the Send method. You do not need to close the client between sends, leaving it open will not leave any connections or listeners running between sends.




回答2:


You shouldn't connect/close after each send request. When you start working - you connect to socket. And you can send data. You should close UdpClient when you do not want to send/recieve data, for example when you closing Form.

In your case you can check that udpClient != null when close/send client and you can use try/catch, for example:

try
{
    udpClient.Send(sendBytes, sendBytes.Length);
}
catch (Exception exc)
{
    // handle the error
}

Use try/catch when you connecting, because port may be busy or other problem with connection. And look at UdpClient.SendAsync :)




回答3:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Text;
using System.Net.Sockets;
using System;
using System.Net;

public class Server : MonoBehaviour
{
    //int[] ports;
    UdpClient udp; // Udp client

    private void Start()
    {
        udp = new UdpClient(1234);
        udp.BeginReceive(Receive, null);
    }

    void Send(string msg, IPEndPoint ipe)
    {
        UdpClient sC = new UdpClient(0);
        byte[] m = Encoding.Unicode.GetBytes(msg);
        sC.Send(m, msg.Length * sizeof(char), ipe);
        Debug.Log("Sending: " + msg);
        sC.Close();
    }

    void Receive(IAsyncResult ar)
    {
        IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 0);
        byte[] data = udp.EndReceive(ar, ref ipe);
        string msg = Encoding.Unicode.GetString(data);
        Debug.Log("Receiving: " + msg);

        udp.BeginReceive(Receive, null);
    }
}

At the Send() I use new UDP CLient and close it after every time. Its better, u can send and receive at the same time.



来源:https://stackoverflow.com/questions/19819927/when-to-close-a-udp-socket

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!