Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
UdpClient C#客户端无法从服务器接收数据_C# - Fatal编程技术网

UdpClient C#客户端无法从服务器接收数据

UdpClient C#客户端无法从服务器接收数据,c#,C#,我对我的UDP客户端服务器应用程序有一个问题。 我在两者之间有联系。它们都互相发送数据。只有服务器从客户机接收数据,而不是相反,但服务器正在发送数据。我做错了什么 using System; using System.Globalization; using System.Net; using System.Net.Sockets; using System.Text; namespace PewPewGame { class Client { UdpClient udpClient;

我对我的
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 { }
    }


}
}
使用系统;
利用制度全球化;
Net系统;
使用System.Net.Sockets;
使用系统文本;
名称空间PewPewGame
{
类客户端
{
UDP客户机UDP客户机;
IPEndPoint远程IPEndPoint;
字符串服务器IP;
字符串[]dataAray;
游戏屏游戏屏;
其他玩家;
公共客户端(string serverIp、GameScreen GameScreen、Player otherPlayer)
{
this.gameScreen=gameScreen;
this.otherPlayer=otherPlayer;
udpClient=新的udpClient();
this.serverIp=serverIp;
}
public void clientThread()
{
udpClient=新的udpClient(serverIp,1002);
while(true)
{
RemoteIpEndPoint=newIPendpoint(IPAddress.Parse(serverIp),1002);
接收数据();
}
}
public void receiveData()
{
Byte[]receiveBytes=udpClient.Receive(参考RemoteIpEndPoint);
String clientData=Encoding.ASCII.GetString(receiveBytes);
dataAray=clientData.Split(',');
otherPlayer.x=Convert.ToInt32(dataAray[0]);
otherPlayer.y=Convert.ToInt32(dataAray[1]);
如果(数据集[3]=“1”)
{
gameScreen.otherSprojection=新投射物(Convert.ToInt16(dataAray[0])、Convert.ToInt16(dataAray[1])、2、4、8、8、dataAray[2]);
}
}
公共void sendData(字符串数据)
{
Byte[]senddata=Encoding.ASCII.GetBytes(数据);
udpClient.Send(senddata,senddata.Length);
}
公共静态IPEndPoint CreateIPEndPoint(字符串端点)
{
字符串[]ep=endPoint.Split(':');
如果(ep.Length<2)抛出新的FormatException(“无效端点格式”);
ip地址ip;
如果(ep.Length>2)
{
if(!IPAddress.TryParse(string.Join(“:”,ep,0,ep.Length-1),out-ip))
{
抛出新的FormatException(“无效ip地址”);
}
}
其他的
{
if(!IPAddress.TryParse(ep[0],输出ip))
{
抛出新的FormatException(“无效ip地址”);
}
}
国际港口;
if(!int.TryParse(ep[ep.Length-1],NumberStyles.None,NumberFormatInfo.CurrentInfo,out port))
{
抛出新的FormatException(“无效端口”);
}
返回新的IPEndPoint(ip,端口);
}
}
}
使用制度;
Net系统;
使用System.Net.Sockets;
使用系统文本;
使用System.Threading.Tasks;
名称空间PewPewGame
{
类服务器
{
UDP客户机UDP客户机;
IPEndPoint远程IPEndPoint;
字符串[]dataAray;
游戏屏游戏屏;
其他玩家;
公共服务器(GameScreen GameScreen,Player otherPlayer)
{
this.gameScreen=gameScreen;
this.otherPlayer=otherPlayer;
}
public-void-serverThread()
{
udpClient=新的udpClient(1002);
while(true)
{
RemoteIpEndPoint=新IPEndPoint(IPAddress.Any,0);
接收数据();
}
}
公共void sendData(字符串数据)
{
Byte[]senddata=Encoding.ASCII.GetBytes(数据);
udpClient.Send(senddata,senddata.Length);
}
public void receiveData()
{
Byte[]receiveBytes=udpClient.Receive(参考RemoteIpEndPoint);
尝试
{
String clientData=Encoding.ASCII.GetString(receiveBytes);
dataAray=clientData.Split(',');
otherPlayer.x=Convert.ToInt32(dataAray[0]);
otherPlayer.y=Convert.ToInt32(dataAray[1]);
如果(数据集[3]=“1”)
{
gameScreen.otherSprojection=新投射物(Convert.ToInt16(dataAray[0])、Convert.ToInt16(dataAray[1])、2、4、8、8、dataAray[2]);
}
}
捕获{}
}
}
}

首先:UDP不知道“连接”,所以您的第一行很混乱

然后您将使用
UdpClient.Send(byte[],int)
方法发送数据,但这需要以前调用
Connect(string,int)
或使用用于客户端的UdpClient构造函数:
新建UdpClient(string,int)

我很确定您的空catch块(不要这样做,尤其是当某些东西没有按预期工作时不要这样做!)会根据[1]中的注释捕获并吞下SocketException:

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

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

因此,在服务器代码中:

1) 拆下空的挡块

2) 使用接受客户端端点的函数-通过传入
Receive(ref-IPEndPoint)


1:

首先:UDP不知道“连接”,所以您的第一行很混乱

然后您将使用
UdpClient.Send(byte[],int)
方法发送数据,但这需要以前调用
Connect(string,int)
或使用用于客户端的UdpClient构造函数:
新建UdpClient(string,int)

我很确定你的