我有一个问题与我的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 { }
}
}
}
尝试{}东西赶上(){// DoNothing}:在“地毯图案”(™)的完美范例,(如果存在某些问题,请将其放在地毯下面并忽略它!) –