3
\$\begingroup\$

I know there are a lot of UDP examples out there, but I really need to know specifically what I am doing wrong. I have Wireshark monitoring my data traffic and according to that program Unity is not sending data at all. Could some of you coding wizards look over this and help me find my mistake?

public class heatLampControl : MonoBehaviour { //Ben Stewart, june 6th, 2016 //this code controls the "Position" of the heat lamps, ie what angle the heat should be coming from. The // value will be added to a new UDP connection to the dSPACE machine //Update 6-10: Added UDP functionality, as accessing the UDP class that was created for this purpose turned out to not function correctly public float lampIntensity = 0.0f; //Multiplier for relative intensity of the sunlight GameObject sun; public GameObject player; // dropped in via GUI public bool isNight = false; // is it night? if so, heat lamp should turn off public double sunAngle; // angle about the y axis. that is the only angle that can be controlled. public bool isOn = false; public string keyDown; public int port; public string dataStr; byte[] data; public GameObject cycle; //following three keep track of day/night cycle public Day_Night.TimeOfDay tod; public Day_Night dayNight; public int strLength; private IPAddress ip; private UdpClient udp; private IPEndPoint ipep; public float fruitIntensity; public float hayIntensity; OlfactoryControl olfactory; string hostname; IPAddress[] ipList; void Start () { hostname = Dns.GetHostName (); ipList = Dns.GetHostEntry (hostname).AddressList; port = 13722; cycle = GameObject.Find ("DayNight"); dayNight = cycle.GetComponent<Day_Night> (); sun = this.gameObject; olfactory = GameObject.Find ("Olfactory").GetComponent<OlfactoryControl>(); ip = ipList [0]; ipep = new IPEndPoint (ip, port); udp = new UdpClient (13722); } // Update is called once per frame void Update () { sunAngle = player.transform.eulerAngles [1] - Math.Atan2 (sun.transform.position [2], sun.transform.position [0]);// gets the positional angle of the sun compared to the player keyDown = Input.inputString; if (isNight || Input.GetKeyUp (KeyCode.L)) { // IF L is pressed down, light will come on lampIntensity = 0f; isOn = false; } if (!isNight && Input.GetKeyDown (KeyCode.L)) { lampIntensity = 1f; isOn = true; } tod = dayNight.tod; // if (tod == Day_Night.TimeOfDay.Idle) // lampIntensity = 1.0f; // if (tod == Day_Night.TimeOfDay.SunRise) { // lampIntensity = (dayNight.timeOfDay / (dayNight.sunSet - dayNight.StartTime)); // } hayIntensity = olfactory.hayIntensity; fruitIntensity = olfactory.fruitIntensity; sendUdp (); } //june 30th edit void sendUdp(){ dataStr = hayIntensity.ToString () + fruitIntensity.ToString () + lampIntensity.ToString (); byte[] sendBytes = System.Text.Encoding.ASCII.GetBytes (dataStr); Debug.Log (dataStr); udp.Send (sendBytes, sendBytes.Length, ipep); Debug.Log ("Data sent to IP Address " + ip.ToString() + " on port " + port.ToString()); } } 
\$\endgroup\$
1
  • \$\begingroup\$june 30th edit You know, these could be solved with git.\$\endgroup\$
    – Bálint
    Jan 19 '17 at 9:02
1
\$\begingroup\$

It's hard to say without some more information, but we can certainly try and rule out some obvious candidates first.

1) Are you seeing your Debug.log statements print out to the console? If not, then the code just isn't executing because of an error/exception on a previous line.

2) If it is sending the packets, and you're not seeing them, then maybe its a problem with how you capture it. It looks like you are sending udp packets to the machine that is running (localhost). Wireshark doesn't always capture loopback packets (ones sent to localhost) without some special settings. Try changing the IP you are sending packets to be something that is not your local machine, like google.com, and see if that resolves it.

\$\endgroup\$
1
  • \$\begingroup\$RawCap is a good tool for capturing UDP packages sent to localhost. I recently also had issues with Wireshark recognizing UDP packages sent to localhost, but RawCap detected and dumped them. So your code may just work, but Wireshark is the problem here.\$\endgroup\$Jan 19 '17 at 10:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.