Der Titel sagt es schon. Ich habe einen TCP-Listener und Sie können mit ipadress.any darauf zugreifen. Aber wenn ich es auf anderen Computern teste, funktioniert meine Verbindung nicht. Hier ist mein Server-Code:Multiplayer funktioniert nicht mit anderen Computern. Nur auf dem gleichen
namespace AsyncClientServer
{
public class Server
{
public TcpListener Listener;
private volatile bool Running; //wordt gebruikt door meerdere threads zonder lock te gebruiken (lock zorgt ervoor dat een thread niet doorgaat naar belangrijke code terwjil een andere thread nog bezig is om naar de locked code te gaan.
private List<BinaryWriter> writers = new List<BinaryWriter>();
bool playerCount = false;
public Server(int port)
{
Listener = new TcpListener(IPAddress.Any, port);
}
public void Start()
{
Listener.Start(10);
Running = true;
while (Running)
{
var connection = Listener.AcceptTcpClient();
ProcessConnection(connection);
}
}
public async Task ProcessConnection(TcpClient connection)
{
var writer = new BinaryWriter(connection.GetStream());
lock (writers)
{
writers.Add(writer);
}
using (var stream = new BinaryReader(connection.GetStream()))
{
//hvlheid connectie
await Task.Factory.StartNew(() => {
byte[] data = System.Text.Encoding.UTF8.GetBytes(Convert.ToString(writers.Count));
playerCount = true;
ProcessCommand(connection, writer, data);
});
//loop
while (Running && connection.Connected)
{
await Task.Factory.StartNew(() => {
var count = stream.ReadInt32();
var data = stream.ReadBytes(count);
ProcessCommand(connection, writer, data);
});
}
connection.Close();
}
lock (writers)
{
writers.Remove(writer);
}
}
private void ProcessCommand(TcpClient connection, BinaryWriter writer, byte[] data)
{
//var info = connection.Client.RemoteEndPoint as IPEndPoint;
if (playerCount)
{
playerCount = false;
lock (writers)
{
foreach (var w in writers)
{
if (w != null)
{
try
{
w.Write((Int32)data.Length);
w.Write(data);
w.Flush();
}
catch
{
}
}
}
}
}
else
{
//var line = System.Text.Encoding.UTF8.GetString(data);
//var response = String.Format("{1}:{2}: {0}", line, info.Address.ToString(), info.Port);
//Console.WriteLine(response);
lock (writers)
{
foreach (var w in writers)
{
if (w != null)
{
try
{
w.Write((Int32)data.Length);
w.Write(data);
w.Flush();
}
catch
{
}
}
}
}
}
}
public void Stop()
{
Running = false;
Listener.Stop();
}
~Server()
{
Running = false;
Listener.Stop();
}
}
}
Program.cs:
public static class MainClass
{
public static readonly int Port = 5000;
public static void Main(string[] args)
{
var server = new Server(Port);
server.Start();
}
}
Auftraggeber:
Start(5000);
public async Task Start(int port)
{
Connection.Connect("localhost", port);
Console.WriteLine("Connected");
Running = true;
Writer = new BinaryWriter(Connection.GetStream());
using (var stream = new BinaryReader(Connection.GetStream()))
{
//infinite loop
while (Running && Connection.Connected)
{
await Task.Factory.StartNew(() => {
var count = stream.ReadInt32();
var data = stream.ReadBytes(count);
ProcessCommand(data);
});
}
Stop();
}
}
//send player coordinates naar server
int[] temporary = new int[4];
public async Task Coordinates()
{
if (Client1)
{
temporary[0] = (int)Player1_x;
temporary[1] = (int)Player1_y;
}
else
{
temporary[2] = (int)Player2_x;
temporary[3] = (int)Player2_y;
}
await Task.Factory.StartNew(() =>
{
//send player 1 coordinates naar server
var player_coordinates = new byte[temporary.Length * sizeof(int) + sizeof(bool) + sizeof(bool)];
//zet coordinates in de byte array
Buffer.BlockCopy(temporary, 0, player_coordinates, 0, temporary.Length * sizeof(int));
//zet collision in de byte array
Buffer.BlockCopy(BitConverter.GetBytes(Collision_player1), 0, player_coordinates, temporary.Length * sizeof(int), sizeof(bool));
Buffer.BlockCopy(BitConverter.GetBytes(Collision_player2), 0, player_coordinates, temporary.Length * sizeof(int) + sizeof(bool), sizeof(bool));
Writer.Write((Int32)player_coordinates.Length);
Writer.Write(player_coordinates);
Writer.Flush();
});
}
//send data voor chat
public async Task Send(String line)
{
if (Writer == null)
return;
await Task.Factory.StartNew(() => {
var data = System.Text.Encoding.UTF8.GetBytes(line);
Writer.Write((Int32)data.Length);
Writer.Write(data);
Writer.Flush();
});
}
//krijg het aantal spelers connected
private void ProcessCommand(byte[] data)
{
if (players)
{
var playerCount = System.Text.Encoding.UTF8.GetString(data);
PlayerCountMethod(Convert.ToInt32(playerCount));
if (playerCount == "1") {
Client1 = true;
}
else if (playerCount == "2")
{
players = false;
}
} else
{
if (Client1)
{
Player2_x = BitConverter.ToInt32(data, 2 * sizeof(int));
Player2_y = BitConverter.ToInt32(data, 3 * sizeof(int));
} else
{
Player1_x = BitConverter.ToInt32(data, 0);
Player1_y = BitConverter.ToInt32(data, sizeof(int));
}
//
if (Collision_player1 == false)
{
Collision_player1 = BitConverter.ToBoolean(data, temporary.Length * sizeof(int));
}
//
if (Collision_player2 == false)
{
Collision_player2 = BitConverter.ToBoolean(data, temporary.Length * sizeof(int) + sizeof(bool));
}
//try-catch want anders argument out of range als er geen message wordt gestuurd
//try
//{
// var line = BitConverter.ToString(data, temporary.Length * sizeof(int));
// ////stuur data van server naar method
// textboxChat(line);
//}
//catch
//{
//}
}
}
public void Stop()
{
Running = false;
Writer.Close();
Writer = null;
Connection.Close();
}
~GamePlay()
{
Stop();
}
Es ist wahrscheinlich eine wirklich einfache Fehler, aber ich weiß nicht wissen, warum es funktioniert, wenn ich mehrere Programme auf meinem PC öffne. Aber es funktioniert nicht, wenn ich es auf anderen PCs öffne.
Hat Ihr Computer haben eine Firewall läuft? – DavidG