我的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 { }
}
}
}
Run Code Online (Sandbox Code Playgroud)
首先:UDP不知道'连接',所以你的第一行令人困惑.
然后你使用该UdpClient.Send(byte[], int)方法发送数据,但是这需要先前(容易混淆地命名)调用Connect(string, int)或使用你正在为客户端使用的UdpClient构造函数:new UdpClient(string, int).
我很确定你的空catch块(不要这样做,特别是如果某些东西没能按预期工作就不要这样做!)根据[1]的注释捕获并吞下一个SocketException:
此重载将数据报发送到Connect方法中建立的远程主机,并返回发送的字节数.如果在调用此重载之前未调用Connect,则Send方法将抛出SocketException.如果收到SocketException,请使用SocketException.ErrorCode获取特定的错误代码.获得此代码后,可以参考MSDN中的Windows套接字版本2 API错误代码文档,以获取错误的详细说明.
如果要将数据报发送到其他远程主机,则必须调用Connect方法并指定所需的远程主机.使用其他任何Send方法重载将数据报发送到广播地址.
所以,在你的服务器代码中:
1)取下空挡块!
2)使用另一个udpClient.Send(byte[], int, IPEndPoint)接受客户端点的重载 - 通过传入的引用得到它Receive(ref IPEndPoint)
1:https://msdn.microsoft.com/en-us/library/08h8s12k(v=vs.110).aspx
| 归档时间: |
|
| 查看次数: |
385 次 |
| 最近记录: |