Problem with Network Connection

Hello there, i have created chat system. Its working only one system, if i use that file into another system, its not working. Here is the code snippet for network connection.

using UnityEngine;
using System.Collections;

public class Network_View : MonoBehaviour 
{
	public string Connection_IP	= "127.0.0.1";
	public int Port_Number		= 8;

	public static bool Connected{ get; private set;}

	private void OnConnectedToServer()
	{
		Connected = true;
	}

	private void OnServerInitialized()
	{
		Connected = true;
	}

	private void OnDisconnectedFromServer()
	{
		Connected = false;
	}

	private void OnGUI()
	{
		if(!Connected)
		{
			Connection_IP = GUILayout.TextField(Connection_IP);
			int.TryParse(GUILayout.TextField(Port_Number.ToString()), out Port_Number);

			if(GUILayout.Button ("Connect"))
				Network.Connect (Connection_IP, Port_Number);
			if(GUILayout.Button ("Host"))
				Network.InitializeServer (5, Port_Number, true);
		}
		else
		{
			GUILayout.Label ("Connections: " + Network.connections.Length.ToString());
		}
	}
}

-Prasanna

For network enabled systems (say Multiplayer Games or Chat apps) you need a server. The IP you are connecting(from the client app) must belong to a server. A server serves all the clients as a hub. When you want to send an information or command to another client, you must send it via the server.

Some advanced multiplayer games(like starcraft) can be played via connecting each player to each other directly without a server but still the players will need a server to find other players.

For example the games like WOW can’t be played without a server. All the information goes to server and comes from server including the chat.

Your code is working on your own computer because the both clients are connecting to the same IP which is your computer’s local IP (127.0.0.1) This setup can’t run on internet or on local network.

You should have a dedicated server computer for your application. This server will run a server version of your app. It’s task is to transfer the chat data between client. Each client must run on different computers and they all have to connect to the server via server’s real IP (not 127.0.0.1)

As a summary:

  1. Write a server app : waiting for client connections and passes the chat data between clients.

  2. Write a client app : connecting to server via it’s real IP (you can get server’s real ip from its network settings)

  3. run the server app on a dedicated computer.

  4. run the client apps on separate computer.

P.S. : Ofcourse it’s possible to run the clients and the server on the same computer. I’m just explaining the fundamentals of networking here.