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 diecast93 · Jul 22, 2019 at 09:39 AM · networkingbeginnerlearningudp

Why do my server and client crash on startup?

I'm very new to Networking, so I've been trying to develop my own Server and Client to use with Unity. I of course did some copy and pasting to learn the basics, but now I'm trying to get a real grasp of how to accomplish what I am looking for. As for now, I just want to connect a client to a server(UDP), and get a confirmation that it is working before I continue.

My current issue is this: Unity crashes immediately upon running the game.

Server:

 using System;
 using System.Net;
 using System.Net.Sockets;
 using System.Text;
 
 public class Server
 {
     private const int port = 8080;
 
     public static int Main()
     {
         //bool done = false;
         UdpClient listener = new UdpClient(port);
         IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, port);
         string receivedData;
         byte[] receivedBytes;
 
         try
         {
                 Console.WriteLine("Waiting for broadcast...");
                 receivedBytes = listener.Receive(ref groupEP);
 
                 Console.WriteLine("Receiving broadcast from: {0}", groupEP.ToString());
 
                 receivedData = Encoding.ASCII.GetString(receivedBytes, 0, receivedBytes.Length);
                 Console.WriteLine("Data follow\n{0}\n\n", receivedData);
         }
 
         catch (Exception e)
         {
             Console.WriteLine(e.ToString());
         }
         listener.Close();
         return 0;
     }
 }

Client:

 using System;
 using System.Net;
 using System.Net.Sockets;
 using System.Text;
 
 public class Client
 {
     public static void Main()
     {
         //Boolean done = false;
         Boolean exception_Thrown = false;
 
         Socket sending = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 
         IPAddress sendTo = IPAddress.Parse("192.168.1.127");
 
         IPEndPoint sendEndPoint = new IPEndPoint(sendTo, 8080);
 
         Console.WriteLine("Enter text to broadcast");
         Console.WriteLine("Enter a blank line to exit the program");
 
 
         string sendText = "Success!";
         /*
         if (sendText.Length == 0)
         {
             done = true;
         }
         
         else
         {
         */
             byte[] sendBuffer = Encoding.ASCII.GetBytes(sendText);
             Console.WriteLine("Send to address: {0}, port: {1}", sendEndPoint, sendEndPoint.Port);
 
             try
             {
                 sending.SendTo(sendBuffer, sendEndPoint);
             }
 
             catch (Exception send_Exception)
             {
                 exception_Thrown = true;
                 Console.WriteLine("Exception {0}", send_Exception);
             }
 
             if (!(exception_Thrown))
             {
                 Console.WriteLine("Message has been sent to the broadcast address");
             }
 
             else
             {
                 exception_Thrown = false;
                 Console.WriteLine("The exception indicates the message was not sent");
             }
         //}
     }
 }

The script I'm going to use to call the other scripts without MonoBehaviour:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 
 public class ServerManager : MonoBehaviour
 {
     private GameObject netGameObject;
     private Server serverScript;
     private Client clientScript;
     //private float timer = 0.0f;
     //private float nextServerUpdate = 2.0f;
     //private bool runUpdate = true;
     //private bool s = false;
 
     // Start is called before the first frame update
     void Start()
     {
         netGameObject = this.gameObject;
         serverScript = new Server();
         clientScript = new Client();
         StartCoroutine(ServerUpdate(2f));
         //Server.Main();
         //Client.Main();
     }
     /*
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey(KeyCode.Y))
         {
             s = true;
         }
     }
     */
     private IEnumerator ServerUpdate(float waitTime)
     {
         while (true)
         {
             Server.Main();
             Debug.Log("Working");
             yield return new WaitForSeconds(waitTime);
         }
     }
 }
 

I've been toying with various things for a while now, and I really need some help. Things I've tested: 1. the coroutine freezes as soon as it hits Server.Main(). 2. DIY timers react the same as coroutine. 3. while(true) loop in coroutine doesn't appear to make a difference. 4. Server.Main() freezes even from start or awake, with all loops that I can find removed. 5. Client.Main() gives me the exact same problems.

Just a note: I have been avoiding Unity's built in systems (UNET and the like) because networking is something I would really like to learn to manage for myself, where I could use it outside of Unity as well.

I would really appreciate any assistance with this, thanks in advance =)

Comment
Add comment · Show 2
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
avatar image troien · Jul 22, 2019 at 10:27 AM 0
Share

Considering the exact same question was asked here and was answered an hour ago. I'm assu$$anonymous$$g you asked both of these questions.


Is there something about that answer that you don't understand? If so commenting to that answer there might be a good idea. Or did your question get stuck in the moderation queue of Unity answers for that long that you just asked the question there aswell?


If the latter is the case and noone here has answered (and the answer on gamedev stackexchange was good) feel free to post your own answer, linking to the correct answer there and mark it as the accepted answer.

avatar image diecast93 troien · Aug 05, 2019 at 04:28 AM 1
Share

$$anonymous$$y question got stuck in moderation for a few days, so I moved it over to the link you pointed out. I'll post the link to the answer.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by diecast93 · Aug 05, 2019 at 04:34 AM

For anyone interested in the answer (with a good, in depth explanation of some parts of it for beginners like me), the answer is here: https://gamedev.stackexchange.com/questions/173965/why-does-my-game-crash-on-startup/173967?noredirect=1#comment309394_173967

An additional question that is related, also with a very helpful answer: https://gamedev.stackexchange.com/questions/174097/why-can-i-only-connect-one-client-to-my-server

Comment
Add comment · Share
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

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

165 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 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 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

SendByChannel is not sending anything to the server (getting disconnected) 0 Answers

UDP Packet and EC2 0 Answers

Android build is not receiving UDP broadcasts 4 Answers

How to find what you're looking for in the Documentation? 3 Answers

What's the best approach for creating a secure authorative server? 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