Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by niceblock · Oct 15, 2017 at 10:11 PM · tcptcpclient

Problems with tcpclient

i have been trying several ways to instantiate a camera when i spawn a player after logging in the server in unity, so far no sucess , i even added the camera to the player prefab but when it spawn it starts to go on and off for some reason heres the code if anyone can help using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using UnityEngine; using Common; using Newtonsoft.Json; using UnityEngine.UI;

namespace Assets.Scripts { public class TcpClientController : MonoBehaviour { [HideInInspector] public Player Player; private Dictionary _playerGameObjectDictionary;

     public GameObject PlayerPrefab;
     public GameObject ConnectionUi;
     public Camera RunCamera;
     public Text PlayerNameInput;
     public string IpAddress;
     public int TcpPort;
     public int UdpPort;

     void Awake()
     {
         Player = new Player();
         _playerGameObjectDictionary = new Dictionary<Guid, GameObject>();
         Player.GameState = GameState.Disconnected;
         Player.TcpClient = new TcpClient();
         Player.UdpClient = new UdpClient();
     }

     void Update()
     {
         if (Player.TcpClient.Connected)
         {
             switch (Player.GameState)
             {
                 case GameState.Connecting:
                     Debug.Log("Connecting");
                     if (Player.TcpClient.GetStream().DataAvailable)
                     {
                         Message message = ReceiveMessage();
                         Player.Id = message.Player.Id;
                         Player.Name = PlayerNameInput.text;
                         Debug.Log(message.Title);
                         Debug.Log(message.Description);
                         SendMessage(new Message { MessageType = MessageType.PlayerName, Player = Player });
                         Player.GameState = GameState.Connected;
                     }
                     break;
                 case GameState.Connected:
                     Debug.Log("Connected");
                     if (Player.TcpClient.GetStream().DataAvailable)
                     {
                         Message message = ReceiveMessage();
                         Debug.Log(message.Title);
                         Debug.Log(message.Description);
                         Player.PlayerInformation = message.Player.PlayerInformation;
                         Player.GameState = GameState.Syncing;
                     }
                     break;
                 case GameState.Syncing:
                     Debug.Log("Syncing");
                     if (Player.TcpClient.GetStream().DataAvailable)
                     {
                         Message message = ReceiveMessage();
                         if (message.MessageType == MessageType.NewPlayer)
                         {
                             GameObject playerGameObject = Instantiate(PlayerPrefab, new Vector3(message.Player.PlayerInformation.X, message.Player.PlayerInformation.Y, message.Player.PlayerInformation.Z), Quaternion.identity);
                             playerGameObject.GetComponent<PlayerUiController>().PlayerName.text = message.Player.Name;
                             //Camera jogocamera = Instantiate(RunCamera, new Vector3(message.Player.PlayerInformation.X, message.Player.PlayerInformation.Y, message.Player.PlayerInformation.Z), Quaternion.identity);
                             _playerGameObjectDictionary.Add(message.Player.Id, playerGameObject);
                         }
                         else if (message.MessageType == MessageType.FinishedSync)
                         {
                             ConnectionUi.SetActive(false);
                             GameObject playerGameObject = Instantiate(PlayerPrefab, new Vector3(Player.PlayerInformation.X, Player.PlayerInformation.Y, Player.PlayerInformation.Z), Quaternion.identity);
                             playerGameObject.GetComponent<PlayerController>().TcpClientController = this;
                             playerGameObject.GetComponent<PlayerController>().Playable = true;
                             playerGameObject.GetComponent<PlayerController>().enabled =true;
                             playerGameObject.GetComponent<PlayerUiController>().PlayerName.text = Player.Name;
                             
                             playerGameObject.GetComponent<Camera>().enabled = false;
                             _playerGameObjectDictionary.Add(Player.Id, playerGameObject);
                             Player.GameState = GameState.GameStarted;
                         }
                     }
                     break;
                 case GameState.GameStarted:
                     Debug.Log("GameStarted");
                     if (Player.TcpClient.GetStream().DataAvailable)
                     {
                         Message message = ReceiveMessage();
                         if (message.MessageType == MessageType.Information)
                         {
                             Debug.Log(message.Title);
                             Debug.Log(message.Description);
                         }
                         else if (message.MessageType == MessageType.NewPlayer)
                         {
                             GameObject playerGameObject = Instantiate(PlayerPrefab, new Vector3(message.Player.PlayerInformation.X, message.Player.PlayerInformation.Y, message.Player.PlayerInformation.Z), Quaternion.identity);
                             playerGameObject.GetComponent<PlayerUiController>().PlayerName.text = message.Player.Name;
                             playerGameObject.GetComponent<Camera>().enabled = true;
                             _playerGameObjectDictionary.Add(message.Player.Id, playerGameObject);
                         }/*else if (message.MessageType == MessageType.PlayerMovement)
                         {
                             if (_playerGameObjectDictionary.ContainsKey(message.Player.Id) && message.Player.Id != Player.Id)
                             {
                                 _playerGameObjectDictionary[message.Player.Id].transform.position = new Vector3(message.Player.PlayerInformation.X, message.Player.PlayerInformation.Y, message.Player.PlayerInformation.Z);
                             }
                         }*/
                     }
                     else if (Player.UdpClient.Available > 0)
                     {
                         Debug.Log("receiving...");
                         Message message = ReceiveUdpMessage(Player.IpEndPoint);
                         if (message.MessageType == MessageType.PlayerMovement)
                         {
                             if (_playerGameObjectDictionary.ContainsKey(message.Player.Id) && message.Player.Id != Player.Id)
                             {
                                 _playerGameObjectDictionary[message.Player.Id].transform.position = new Vector3(message.Player.PlayerInformation.X, message.Player.PlayerInformation.Y, message.Player.PlayerInformation.Z);
                               
                             }
                         }
                     }
                     break;
             }
         }
         else
         {
             Debug.Log("Disconnected");
         }
     }

     public void SendMessage(Message message)
     {
         string messageJson = JsonConvert.SerializeObject(message);
         Player.BinaryWriter.Write(messageJson);
     }

     public Message ReceiveMessage()
     {
         string messageJson = Player.BinaryReader.ReadString();
         Message message = JsonConvert.DeserializeObject<Message>(messageJson);
         return message;
     }

     /*public void SendUdpMessage(Message message)
     {
         string messageJson = JsonConvert.SerializeObject(message);
         int size = Encoding.ASCII.GetByteCount(messageJson);
         byte[] messageBytes = new byte[size];
         messageBytes = Encoding.ASCII.GetBytes(messageJson);
         Player.UdpClient.Send(messageBytes, size, new IPEndPoint(IPAddress.Parse(IpAddress), UdpPort));
     }*/

     public Message ReceiveUdpMessage(IPEndPoint ipEndPoint)
     {
         byte[] messageBytes = new byte[65000];
         messageBytes = Player.UdpClient.Receive(ref ipEndPoint);
         string messageJson = Encoding.ASCII.GetString(messageBytes);
         Message message = JsonConvert.DeserializeObject<Message>(messageJson);
         return message;
     }

     public void StartTcpClient()
     {
         Player.TcpClient.BeginConnect(IPAddress.Parse(IpAddress), TcpPort, AcceptConnection, Player.TcpClient);
         Player.GameState = GameState.Connecting;
     }

     private void AcceptConnection(IAsyncResult asyncResult)
     {
         TcpClient tcpClient = (TcpClient)asyncResult.AsyncState;
         tcpClient.EndConnect(asyncResult);

         if (tcpClient.Connected)
         {
             Debug.Log("Client connected");
             Player.BinaryReader = new BinaryReader(tcpClient.GetStream());
             Player.BinaryWriter = new BinaryWriter(tcpClient.GetStream());
             Player.UdpClient.ExclusiveAddressUse = false;
             Player.IpEndPoint = new IPEndPoint(IPAddress.Any, UdpPort);
             Player.UdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
             Player.UdpClient.ExclusiveAddressUse = false;

             Player.UdpClient.Client.Bind(Player.IpEndPoint);

             IPAddress ipAddress = IPAddress.Parse("239.0.0.254");
             Player.UdpClient.JoinMulticastGroup(ipAddress);
             //Player.UdpClient.Connect(IpAddress, UdpPort);
             //Player.UdpClient = new UdpClient();
             //Player.IpEndPoint = new IPEndPoint(IPAddress.Parse(IpAddress), UdpPort);
             //Player.UdpClient.Connect(Player.IpEndPoint);

         }
         else
         {
             Debug.Log("Client connection refused");
         }
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems with TLS handshake 0 Answers

Streaming EEG data from Qstreamer to Unity 0 Answers

TcpClient not working when building, 1 Answer

how can i send image or texture and text through TCP to send data from mobile to pc 1 Answer

Trouble Connecting Unity with Labview 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges