- Home /
Client and server communication
I am new to unity, i want to control my Game objects over WiFi. I followed this tutorial.
I have created the 2 scenes (server and client) and have the server running through unity editor and client running on my mobile phone where both server and client are connected to the same WiFi.
The IP address varies on mobile phone and gives me a status "false". I turned off the Windows firewall, i also made sure that the port number was not occupied but nothing worked for me. Below is the client and server code. Please help me fix if i am doing a mistake.
Server end code( Which is in unity Editor)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
using UnityStandardAssets.CrossPlatformInput;
public class NetworkServerUI : MonoBehaviour {
CrossPlatformInputManager.VirtualAxis m_HVAxis;
CrossPlatformInputManager.VirtualAxis m_VVAxis;
string horizontalAxisName = "Horizontal";
string verticalAxisName = "Vertical";
void OnGUI()
{
string ipaddress = Network.player.ipAddress;
GUI.Box(new Rect(10, Screen.height - 50, 100, 50), ipaddress);
GUI.Label(new Rect(20, Screen.height - 35, 100, 20), "Status" + NetworkServer.active);
GUI.Label(new Rect(20, Screen.height - 20, 100, 20), "Connected" + NetworkServer.connections.Count);
}
void Start () {
m_HVAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_HVAxis);
m_VVAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_VVAxis);
NetworkServer.Listen(25000);
NetworkServer.RegisterHandler(888, ServerReceiveMessage);
}
private void ServerReceiveMessage(NetworkMessage message)
{
StringMessage msg = new StringMessage();
msg.value = message.ReadMessage<StringMessage>().value;
string[] deltas = msg.value.Split('|');
m_HVAxis.Update(Convert.ToSingle(deltas[0]));
m_VVAxis.Update(Convert.ToSingle(deltas[1]));
}
}
Client End code which is running on my mobile
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
public class NetworkClientUI : MonoBehaviour {
static NetworkClient client;
void OnGUI()
{
string ipaddress = Network.player.ipAddress;
GUI.Box(new Rect(10, Screen.height - 50, 100, 50), ipaddress);
GUI.Label(new Rect(20, Screen.height - 30, 100, 20), "Status" + client.isConnected);
if(!client.isConnected)
{
Debug.Log("Waiting to connect");
if(GUI.Button(new Rect(10,10,60,50),"Connect"))
{
Connect();
}
}
}
void Start () {
client = new NetworkClient();
}
void Connect()
{
client.Connect("192.168.43.205", 25000);
}
static public void SendJoystickInfo(float hDelta, float vDelta)
{
if(client.isConnected)
{
StringMessage msg = new StringMessage();
msg.value = hDelta + "|" + vDelta;
client.Send(888, msg);
}
}
}
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Unity UDP freezes if disconnected from server 2 Answers
connecting two people over the Internet 1 Answer
Server not receiving message? 1 Answer
Networking in a 2 player web game using a player as the server 0 Answers