Hi, i am tryng to make a TCP/IP server/client using Unity engine and Visual Studio Console Application. Errors: When i try enter in game the Server Console show the message i Connected, but int the Game aplication(Unity) dont show nothing. And When i try send login data in game to server always give a error null references in NetworkStream. P.S: Iam new using Sockets, the Mysql dataBase works fine, the erros is in sockets data. The Game Application(Unity)CodeNetwork.cs Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Net.Sockets; using System.IO; using System; public class Network : MonoBehaviour { public static Network singleton; [Header("Network Settings")] public string ServerIP = "127.0.0.1"; public int ServerPort = 5500; public bool isConnected; public TcpClient PlayerSocket; private NetworkStream myStream; private byte[] asyncBuff; public bool shouldHandleData; private byte[] myBytes = new byte[1024]; private void Awake() { singleton = this; } void Start() { ConnectGameServer(); } void ConnectGameServer() { PlayerSocket = new TcpClient(ServerIP,ServerPort); PlayerSocket.ReceiveBufferSize = 1024; PlayerSocket.SendBufferSize = 1024; PlayerSocket.NoDelay = false; Array.Resize(ref asyncBuff, 2048); PlayerSocket.BeginConnect(ServerIP, ServerPort, new AsyncCallback(ConnectCallback), PlayerSocket); isConnected = true; } void ConnectCallback(IAsyncResult result) { if (PlayerSocket != null) { PlayerSocket.EndConnect(result); if (PlayerSocket.Connected == false) { isConnected = false; return; } else { PlayerSocket.NoDelay = true; myStream = PlayerSocket.GetStream(); myStream.BeginRead(asyncBuff, 0, 2048, OnReceive, null); } } } private void Update() { if (shouldHandleData) { ClientHandleData.singleton.HandleData(myBytes); shouldHandleData = false; } } void OnReceive(IAsyncResult result) { if (PlayerSocket != null) { if (PlayerSocket == null) return; int byteArray = myStream.EndRead(result); myBytes = null; Array.Resize(ref myBytes, byteArray); Buffer.BlockCopy(asyncBuff, 0, myBytes, 0, byteArray); if (byteArray == 0) { Debug.Log("You got disconnected from the Server."); PlayerSocket.Close(); return; } shouldHandleData = true; if (PlayerSocket == null) return; myStream.BeginRead(asyncBuff, 0, 2048, OnReceive, null); } } public void WriteBytes(byte[] data, int startOfsset, int size) { try { myStream.Write(data, startOfsset, size); } catch (Exception e) { print("Error: " + e.ToString()); // for some rason always execute the exception } } } ClientHandleData.cs Code (CSharp): using System; using UnityEngine; using UnityEngine.UI; using ByteBufferDLL; class ClientHandleData : MonoBehaviour { public static ClientHandleData singleton; private void Awake() { singleton = this; } void HandleMessages(int packetNum, byte[] data) { switch (packetNum) { case 10: HandleLoginSucess(packetNum,data); break; case 11: HandleLoginFailed(packetNum, data); break; case 12: break; case 13: break; case 14: break; case 15: HandleWelcomeMessage(packetNum, data); break; default: HandleUnknowPackage(packetNum, data); break; } } public void HandleData(byte[] data) { int packetnum; ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(data); packetnum = buffer.ReadInt(); buffer = null; if (packetnum == 0) return; HandleMessages(packetnum, data); } void HandleLoginSucess(int packageNum, byte[] data) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(data); buffer.ReadInt(); var msg = buffer.ReadString(); InGameDebug.singleton.Debug("Login Message: " + msg,10f); buffer = null; } void HandleLoginFailed(int packageNum, byte[] data) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(data); buffer.ReadInt(); var msg = buffer.ReadString(); InGameDebug.singleton.Debug( msg, 10f); buffer = null; } private void HandleWelcomeMessage(int packetNum, byte[] data) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(data); buffer.ReadInt(); var msg = buffer.ReadString(); InGameDebug.singleton.Debug(msg, 10f); buffer = null; } private void HandleUnknowPackage(int packetNum, byte[] data) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(data); buffer.ReadInt(); var msg = buffer.ReadString(); InGameDebug.singleton.Debug("Unknow Message: "+msg, 10f); buffer = null; } } ServerSendData.cs Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using ByteBufferDLL; public class ClientSendData : MonoBehaviour { public static ClientSendData sinleton; Network network; // Use this for initialization void Awake() { sinleton = this; network = FindObjectOfType<Network>(); } public void SendDataToServer(byte[] data) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(data); Network.singleton.WriteBytes(buffer.ToArray(), 0, buffer.ToArray().Length); buffer = null; } public void SendLogin(string username, string password) { ByteBuffer buff = new ByteBuffer(); buff.WriteInt(1); buff.WriteString(username); buff.WriteString(password); SendDataToServer(buff.ToArray()); buff = null; } public void SendRegister(string username, string password, string email) { ByteBuffer buff = new ByteBuffer(); buff.WriteInt(2); buff.WriteString(username); buff.WriteString(password); buff.WriteString(email); SendDataToServer(buff.ToArray()); buff = null; } } The Server Console Application CodesServerHandleData.cs Code (CSharp): using System; using System.Collections.Generic; using System.Linq; using System.Text; using ByteBufferDLL; namespace CalixServer2 { class ServerHandleData { public static ServerHandleData singleton = new ServerHandleData(); private delegate void Package(int index, byte[] data); private Dictionary<int, Package> packages; public void StartMessageHandler() { packages = new Dictionary<int, Package>(); packages.Add(1, HandleLogin); } public void HandleData(int index, byte[] data) { int packageNum; Package package; ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(data); packageNum = buffer.ReadInt(); buffer = null; if (packageNum == 0) return; if (packages.TryGetValue(packageNum,out package)) { package.Invoke(index, data); } } private void HandleLogin(int index, byte[] data) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(data); buffer.ReadInt(); string username = buffer.ReadString(); string password = buffer.ReadString(); // mysql if (Database.singleton.AccountExist(username)) { if (Database.singleton.LogginExist(username,password)) { ByteBuffer bufferResponse = new ByteBuffer(); bufferResponse.WriteInt(10); bufferResponse.WriteString("Login Sucessfull!"); ServerSendData.singleton.SendData(index, bufferResponse.ToArray()); } else { ByteBuffer bufferResponse = new ByteBuffer(); bufferResponse.WriteInt(11); bufferResponse.WriteString("Password Error!"); ServerSendData.singleton.SendData(index, bufferResponse.ToArray()); } } else { ByteBuffer bufferResponse = new ByteBuffer(); bufferResponse.WriteInt(11); bufferResponse.WriteString("Account Not Exist!"); ServerSendData.singleton.SendData(index, bufferResponse.ToArray()); return; } } } } ServerSendData.cs Code (CSharp): using System; using System.Collections.Generic; using System.Linq; using System.Text; using ByteBufferDLL; namespace CalixServer2 { class ServerSendData { public static ServerSendData singleton = new ServerSendData(); public void SendData(int index, byte[] data) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(data); Network.clientConnections[index].stream.BeginWrite(buffer.ToArray(), 0, buffer.ToArray().Length, null, null); buffer = null; } } } ClientConnection.cs Code (CSharp): using System; using System.Net.Sockets; namespace CalixServer2 { public class ClientConnection { public int ID; public string IP; public TcpClient Socket; public NetworkStream stream; public byte[] readBuff; public void Start() { Socket.ReceiveBufferSize = 1024; Socket.SendBufferSize = 1024; stream = Socket.GetStream(); Array.Resize(ref readBuff, Socket.ReceiveBufferSize); stream.BeginRead(readBuff, 0,Socket.ReceiveBufferSize,OnReceiveData,null); } private void OnReceiveData(IAsyncResult result) { try { int readBytes = stream.EndRead(result); if (Socket == null) return; if (readBytes <= 0) { CloseConnection(); return; } byte[] newBytes = null; Array.Resize(ref newBytes, readBytes); Buffer.BlockCopy(readBuff,0,newBytes,0,readBytes); ServerHandleData.singleton.HandleData(ID, newBytes); if (Socket == null) return; stream.BeginRead(readBuff,0,Socket.ReceiveBufferSize,OnReceiveData,null); } catch (Exception e) { CloseConnection(); } } void CloseConnection() { Socket.Close(); Socket = null; Network.RemoveClientConnection(ID); } } } Network.cs Code (CSharp): using System; using System.Net; using System.Net.Sockets; using System.Collections.Generic; using System.Threading; using System.Text; using ByteBufferDLL; namespace CalixServer2 { class Network { private TcpListener serverSocket; public static Network singleton = new Network(); public static List<ClientConnection> clientConnections = new List<ClientConnection>(); public void StartServer() { serverSocket = new TcpListener(IPAddress.Any, 5500); serverSocket.Start(); serverSocket.BeginAcceptTcpClient(OnClientConnect,null); Program.WriteConsole("Server Started Sucessfully!"); } private void OnClientConnect(IAsyncResult result) { TcpClient client = serverSocket.EndAcceptTcpClient(result); client.NoDelay = false; int index = clientConnections.Count; var newClient = new ClientConnection(); newClient.Socket = client; newClient.IP = client.Client.RemoteEndPoint.ToString(); newClient.ID = index+1; newClient.Start(); clientConnections.Add(newClient); Program.WriteConsole("Client Connection From: "+newClient.IP + "| ID: "+newClient.ID); serverSocket.BeginAcceptTcpClient(OnClientConnect, null); } public static void RemoveClientConnection(int i) { clientConnections.RemoveAt(i-1); } } }