- Home /
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 =)
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.
$$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.
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
Your answer
Follow this Question
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