Jump to content
Unity Insider Forum

Netzwerk Anfänger ratlos


xXSiggiXx

Recommended Posts

Hallo,

 

ich hab ein Server/Client Tutorial angefangen. Das liegt in JS vor, ich setz es in C# um.

 

Hier mein Code:

 

Server...

using UnityEngine;
using System.Collections;
public class ServerMain : MonoBehaviour {

public int listenPort = 25024;
public int maxClients = 5;

void startServer() {
 Network.InitializeServer(maxClients, listenPort, false);
}
void stopServer() {
 Network.Disconnect();
}
void showServerInformation(){
 GUILayout.Label("IP: " + Network.player.ipAddress + "Port: " + Network.player.port);
}
void showClientInformation(){
 GUILayout.Label("Clients: " + Network.connections.Length + "/" + maxClients);
 foreach ( NetworkPlayer p in Network.connections) {
  GUILayout.Label("Player from IP/Port: " + p.ipAddress + "/" + p.port);
 }
}

void OnGUI() {
 if(Network.peerType == NetworkPeerType.Disconnected) {
  GUILayout.Label("Server is not running");
  if(GUILayout.Button("Start Server")) {
startServer();
  }
 }
 else {
  if(Network.peerType == NetworkPeerType.Connecting){
GUILayout.Label("Server is starting...");
  }
  else {
GUILayout.Label("Server is running");
showServerInformation();
showClientInformation();
  }
  if(GUILayout.Button("Stop server")){
stopServer();
  }
 }
}
}

 

Client...

using UnityEngine;
using System.Collections;
public class ClientMain : MonoBehaviour {
string remoteIP = "192.168.178.10";
int remotePort = 25024;

void connectToServer() {
 Network.Connect(remoteIP, remotePort);
}
void disconnectFromServer() {
 Network.Disconnect();
}
void OnGUI() {
 if(Network.peerType == NetworkPeerType.Disconnected) {
  GUILayout.Label("Not connected to server");
  if(GUILayout.Button("Connect to server")) {
connectToServer();
  }
 }
 else {
  if(Network.peerType == NetworkPeerType.Connecting) {
GUILayout.Label("Connection to server...");
  }
  else {
GUILayout.Label("Connected to server");
GUILayout.Label ("IP/Port: " + Network.player.ipAddress + "/" + Network.player.port);
  }
  if(GUILayout.Button("Disconnect")){
disconnectFromServer();
  }
 }
}
}

 

Beide scripts sind jeweils in einem Empty. Ich starte beide in Unity und versuche zu verbinden. Die Verbindung will aber nicht. Ich bekomme dafür vom Client in der Console die Meldung:

The connection request to 192.168.178.10:25024 failed. Are you sure the server can be connected to?

 

Im code find ich kein Problem. Dazu hab ich probiert einen anderen Port zu nehmen und die Windows Firewall auszumachen. Hilft aber alles nichts.

 

Was mache ich falsch?

 

 

Gruß

Siggi

Link zu diesem Kommentar
Auf anderen Seiten teilen

Archiviert

Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.

×
×
  • Neu erstellen...