2016-12-14 85 views
0

我有一个问题与我的UDP客户端服务器应用程序。 我有两个之间的联系。他们都向对方发送数据。只有服务器接收来自客户端的数据,而不是其他方式,但服务器正在发送数据。我究竟做错了什么?UdpClient C#客户端不会从服务器接收数据

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 { } 
    } 


} 
} 

回答

5

首先:UDP不知道'连接',所以你的第一行很混乱。

然后你使用的UdpClient.Send(byte[], int)方法发送数据,但预计以前的(容易混淆的名字命名的)打电话给Connect(string, int)或使用您正在使用客户端的UdpClient构造函数:new UdpClient(string, int)

我非常确定你的空捕获块(不要这样做,特别是如果某些东西不能按预期工作,就不要这么做!)根据[1]的说法捕获并吞下SocketException:

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

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

所以,在你的服务器代码:

1)拆下空catch块!

2)使用another udpClient.Send(byte[], int, IPEndPoint) overload接受客户端的终点 - 你得到的是通过引用传递进Receive(ref IPEndPoint)

1:https://msdn.microsoft.com/en-us/library/08h8s12k(v=vs.110).aspx

+0

尝试{}东西赶上(){// DoNothing}:在“地毯图案”(™)的完美范例,(如果存在某些问题,请将其放在地毯下面并忽略它!) –

相关问题