UdpClient C#Client不会从服务器接收数据

时间:2016-12-14 10:25:55

标签: c#

我对UDP客户端服务器应用有疑问。 我和两者之间有联系。他们都互相发送数据。只有服务器从客户端接收数据,但不是相反,但服务器正在发送。我做错了什么?

using System;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace PewPewGame
{
class Client
{
    UdpClient udpClient;
    IPEndPoint RemoteIpEndPoint;
    String serverIp;
    String[] dataAray;

    GameScreen gameScreen;
    Player otherPlayer;

    public Client(string serverIp, GameScreen gameScreen, Player otherPlayer)
    {
        this.gameScreen = gameScreen;
        this.otherPlayer = otherPlayer;
        udpClient = new UdpClient();
        this.serverIp = serverIp;

    }
    public void clientThread()
    {
        udpClient = new UdpClient(serverIp,1002);
        while (true)
        {
            RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse(serverIp), 1002);
            receiveData();
        }
    }

    public void receiveData()
    {
        Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);


            String clientData = Encoding.ASCII.GetString(receiveBytes);
            dataAray = clientData.Split(',');
            otherPlayer.x = Convert.ToInt32(dataAray[0]);
            otherPlayer.y = Convert.ToInt32(dataAray[1]);
            if (dataAray[3] == "1")
            {
                gameScreen.otherProjectile = new Projectile(Convert.ToInt16(dataAray[0]), Convert.ToInt16(dataAray[1]), 2, 4, 8, 8, dataAray[2]);
            }


    }

    public void sendData(string data)
    {
        Byte[] senddata = Encoding.ASCII.GetBytes(data);
        udpClient.Send(senddata, senddata.Length);
    }

    public static IPEndPoint CreateIPEndPoint(string endPoint)
    {
        string[] ep = endPoint.Split(':');
        if (ep.Length < 2) throw new FormatException("Invalid endpoint format");
        IPAddress ip;
        if (ep.Length > 2)
        {
            if (!IPAddress.TryParse(string.Join(":", ep, 0, ep.Length - 1), out ip))
            {
                throw new FormatException("Invalid ip-adress");
            }
        }
        else
        {
            if (!IPAddress.TryParse(ep[0], out ip))
            {
                throw new FormatException("Invalid ip-adress");
            }
        }
        int port;
        if (!int.TryParse(ep[ep.Length - 1], NumberStyles.None, NumberFormatInfo.CurrentInfo, out port))
        {
            throw new FormatException("Invalid port");
        }
        return new IPEndPoint(ip, port);
    }
}
}


using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace PewPewGame
{
class Server
{
    UdpClient udpClient;
    IPEndPoint RemoteIpEndPoint;

    String[] dataAray;

    GameScreen gameScreen;
    Player otherPlayer;

    public Server(GameScreen gameScreen, Player otherPlayer)
    {
        this.gameScreen = gameScreen;
        this.otherPlayer = otherPlayer;
    }

    public void serverThread()
    {
        udpClient  = new UdpClient(1002);
        while (true)
        {
            RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
            receiveData();
        }
    }

    public void sendData(string data)
    {
        Byte[] senddata = Encoding.ASCII.GetBytes(data);
        udpClient.Send(senddata, senddata.Length);
    }

    public void receiveData()
    {
        Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);

        try
        {
            String clientData = Encoding.ASCII.GetString(receiveBytes);
            dataAray = clientData.Split(',');
            otherPlayer.x = Convert.ToInt32(dataAray[0]);
            otherPlayer.y = Convert.ToInt32(dataAray[1]);
            if (dataAray[3] == "1")
            {
                gameScreen.otherProjectile = new Projectile(Convert.ToInt16(dataAray[0]), Convert.ToInt16(dataAray[1]), 2, 4, 8, 8, dataAray[2]);
            }

        }
        catch { }
    }


}
}

1 个答案:

答案 0 :(得分:5)

首先:UDP不知道'连接',所以你的第一行令人困惑。

然后你使用UdpClient.Send(byte[], int)方法发送数据,但是这需要先前(令人困惑地命名)调用Connect(string, int)或使用你用于客户端的UdpClient构造函数:{{ 1}}。

我很确定你的空catch块(不要这样做,特别是如果某些东西没能按预期工作就不要这样做!)根据[1]的注释捕获并吞下一个SocketException:

  

此重载将数据报发送到Connect方法中建立的远程主机,并返回发送的字节数。如果在调用此重载之前未调用Connect,则Send方法将抛出SocketException。如果收到SocketException,请使用SocketException.ErrorCode获取特定的错误代码。获得此代码后,可以参考MSDN中的Windows套接字版本2 API错误代码文档,以获取错误的详细说明。

     

如果要将数据报发送到其他远程主机,则必须调用Connect方法并指定所需的远程主机。使用其他任何Send方法重载将数据报发送到广播地址。

所以,在您的服务器代码中:

1)取出空挡块!

2)使用接受客户端点的enter image description here - 您可以通过传入new UdpClient(string, int)

的引用获得该端点

1:another udpClient.Send(byte[], int, IPEndPoint) overload