好的,所以我一直在为我正在制作的程序开发服务器/客户端应用程序。它在本地网络上运行良好。我已设置端口转发,并使用我的外部IP地址与服务器应用程序进行通信。一切正常,但当我将客户端应用程序发送给我的朋友启动时,我的服务器没有与他们通信。有什么建议吗?
服务器(标出重要的行,其余部分不是解决此问题所必需的)
class ServerListener
{
private string _IPAddress;
private int _Port;
private static UdpState _UdpState;
private static UdpClient _UdpServer;
private IPEndPoint _IpEndpoint;
private static BinaryFormatter _BinaryFormatter;
public ServerListener(string ipAddress, int port)
{
_IPAddress = ipAddress;
_Port = port;
_UdpServer = new UdpClient(_Port);
_IpEndpoint = new IPEndPoint(_IPAddress , 8000);
_BinaryFormatter = new BinaryFormatter();
ThreadPool.SetMaxThreads(50, 50);
_UdpState = new UdpState();
_UdpState.IpEndPoint = _IpEndpoint;
_UdpState.UdpClient = _UdpServer;
_UdpServer.Client.SendTimeout = 100;
_UdpServer.Client.ReceiveTimeout = 100;
}
public void StartListenCycle()
{
FetchData();
}
private static void FetchData()
{
_UdpServer.BeginReceive(new AsyncCallback(ReceiveCallback), _UdpState);
}
private static void ReceiveCallback(IAsyncResult ar)
{
FetchData();
ClientDataRequest dataRequest;
UdpClient udpClient = (UdpClient)((UdpState)(ar.AsyncState)).UdpClient;
IPEndPoint endpoint = (IPEndPoint)((UdpState)(ar.AsyncState)).IpEndPoint;
//// ----> This lines important Byte[] receiveBytes = udpClient.EndReceive(ar, ref endpoint);
using (var memStream = new MemoryStream())
{
memStream.Write(receiveBytes, 0, receiveBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
dataRequest = (ClientDataRequest)_BinaryFormatter.Deserialize(memStream);
ThreadPool.QueueUserWorkItem(new WaitCallback(ClientRequestHandler.HandleRequest), dataRequest);
}
using (var memStream = new MemoryStream())
{
GameState gameState = new GameState(true);
SerializedGameState dataPacket = new SerializedGameState();
dataPacket.Players = new PlayerInformation[GameState.Players.Count];
gameState.ClientPlayerObjects.CopyTo(dataPacket.Players, 0);
Serializer.Serialize(memStream, dataPacket);
byte[] responseAsBytes = memStream.ToArray();
try
{
//// ----> This lines important udpClient.Send(responseAsBytes, responseAsBytes.Length, endpoint);
}
catch (Exception e)
{
Console.WriteLine("");
}
}
}
}
客户(再次相同)
class ClientNetworkConnection
{
private string _IPAddress;
private int _Port;
UdpClient _UdpClient;
IPEndPoint _IpEndpoint;
public ClientNetworkConnection(string ipAddress, int port)
{
_IPAddress = ipAddress;
_Port = port;
_UdpClient = new UdpClient();
_UdpClient.Ttl = 10;
_UdpClient.AllowNatTraversal(true);
_IpEndpoint = new IPEndPoint(IPAddress.Parse(_IPAddress), _Port);
_UdpClient.Client.SendTimeout = 100;
_UdpClient.Client.ReceiveTimeout = 100;
_UdpClient.Connect(_IpEndpoint);
}
public void FetchData()
{
while (Thread.CurrentThread.IsAlive)
{
ClientDataRequest request = new ClientDataRequest();
request.PlayerID = DataStore.PlayerId;
request.PlayerName = DataStore.PlayerName;
request.PlayerClicked = DataStore.MouseDown;
request.ClickPosition = new Point(DataStore.MousePositionX, DataStore.MousePositionY);
SerializedGameState response = new SerializedGameState();
byte[] objectAsBytes;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, request);
objectAsBytes = ms.ToArray();
}
/////// -------> Important networky stuff _UdpClient.Send(objectAsBytes, objectAsBytes.Length);
try
{
/////// -------> Important networky stuff var data = _UdpClient.Receive(ref _IpEndpoint);
using (var memStream = new MemoryStream())
{
memStream.Write(data, 0, data.Length);
memStream.Seek(0, SeekOrigin.Begin);
response = Serializer.Deserialize<SerializedGameState>(memStream);
GameState.SetClientStaticVariables(response);
}
}
catch (SocketException e)
{
//Handle Socket exception
}
}
}
}