How to modify the script ? (socket/udp)

hi there
I now have a program to send messages, and then the script receive it, however, the unity test is too slowly.
how to modify it? Any reply is appreciated.

--------------------------------------
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class udpServer2 : MonoBehaviour 
{
	Socket socket;
	IPEndPoint localIP;
	IPEndPoint clientIP;
	EndPoint remote;
	
	// Use this for initialization
	void Awake () 
	{
		socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
		localIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
		socket.Bind(localIP);
	}
	
	
	int recv = 0;
	byte[] data = null;
	string msg = "";
	
	// Update is called once per frame
	void Update () 
	{
		data = new byte[1024];
		clientIP = new IPEndPoint(IPAddress.Any, 0);
		remote = (EndPoint)clientIP;
		recv = socket.ReceiveFrom(data, ref remote);
		msg = (Encoding.ASCII.GetString(data, 0, recv)).Trim('|');
		Debug.Log(msg);
	}
	
}
--------------------------------------

340582–11918–$socketudptest_170.rar (96 KB)

Are you saying the data transfer is too slow for your game? Can you give any more detail about what you are trying to do?

Yes, I soon upload a picture for you.
It’s just a test, all the details are in the attachment.
And the welcome.exe code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[ ] args)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint localIP = new IPEndPoint(IPAddress.Parse(“127.0.0.1”), 11000);

string msg = “Welcome!”;
byte[ ] msgBytes = Encoding.ASCII.GetBytes(msg);

Console.WriteLine(“This is a Client!”);

while (true)
{
socket.SendTo(msgBytes, msgBytes.Length, SocketFlags.None, localIP);
Console.WriteLine(msg);
}

Console.ReadKey();
}
}
}

you are likely doing suboptimal networking

when working with sockets you should define fixed points in time at which point you send data and you should combine multiple “messages” into one send packet so you don’t have hundreds of micro messages floating around, they have a horrible overhead.

alternative: did you consider using lidgren? or at least looking at it as learning resource?

Today i rewrite the script, it looks like it could work normally, but in fact it is time delays, in other words, it’s not realtime.

test2 screenshot below:

test2.unitypackage

341469–11954–$test2_411.unitypackage (96.1 KB)

Thats a joke right?
You have always latency when using networking. Actually you have to expect that the difference between send and receive is up to 150-200ms and handle it appropriately!

Just cause it has latency its not “non realtime”. realtime means that A sends, B receives and B handles it, potentially sending something back directly. thats what is called realtime

ps:
But I use similar code in vs c#, which can real-time receiving messages and output to Console.

Not “real-time”,but sync.
A continuous send some data, and B(unity) simultaneously receive.