2016-12-14 118 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}:在「地毯圖案」(™)的完美範例,(如果存在某些問題,請將其放在地毯下面並忽略它!) –

相關問題