I'm very new to networking, so I've been trying to develop my own server and client to use with Unity. I 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:
- The coroutine freezes as soon as it hits
Server.Main()
. - DIY timers react the same as coroutine.
while(true)
loop in coroutine doesn't appear to make a difference.Server.Main()
freezes even from start or awake, with all loops that I can find removed.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.
why is the static members thing an issue? No sarcasm, genuinely curious.
– schnondle Jul 22 '19 at 20:28