you are getting
Cannot implicitly convert string to UDPDualReceive
because of this line. UDPDualReceive is not a string. it’s a UDPDualReceive.
getLatestUDPPacket() returns a string
change the line to
string ReceiveLeft = HatSprite.GetComponent().getLatestUDPPacket
and Malleck666 is correct, you’ll never get to lastReceivedRight
Thanks for replying…actually i m receiving both the values in the inspector as i am maintaining using threads…I just want to use these values for motion of a game object. @johne5 whenever i replace UDPDualReceive with string
string Leftvalue = ReceiveLeft.lastReceivedLeft
I get system.string could not find lastReceived error…
Can you tell me any way i can get both the variables from other script?
Just came to know…thanks to @Malleck666 for pointing it out i am not receiving right value and @johne5 thanks to you too. now i am completely disappointed how to get data over 2 ports iam posting my entire code…please help me out.I am totally stuck…
/*
-----------------------
UDP-Receive (send to)
-----------------------
// [url]http://msdn.microsoft.com/de-de/library/bb979228.aspx#ID0E3BAC[/url]
// > receive
// 127.0.0.1 : 8051
// send
// nc -u 127.0.0.1 8051
*/
using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class UDPDualReceive : MonoBehaviour {
// receiving Thread
// Thread receiveThread;
Thread LeftData;
Thread RightData;
// udpclient object
UdpClient client1;
UdpClient client2;
// public
// public string IP = "127.0.0.1"; default local
public int port1; // define > init
public int port2;
// infos
public string lastReceivedLeft="";
public string lastReceivedRight=""; // clean up this from time to time!
// start from shell
private static void Main()
{
UDPDualReceive receiveObj1=new UDPDualReceive();
UDPDualReceive receiveObj2=new UDPDualReceive();
receiveObj1.init(33555);
receiveObj2.init (33557);
string text="";
do
{
text = Console.ReadLine();
}
while(!text.Equals("exit"));
}
// start from unity3d
public void Start()
{
init(33555);
init (33557);
}
// OnGUI
void OnGUI()
{
Rect rectObj=new Rect(40,10,200,400);
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.UpperLeft;
GUI.Box(rectObj,"# UDPLeftReceive\n127.0.0.1 "+port1+" #\n"
+ "shell> nc -u 127.0.0.1 : "+port1+" \n"
+ "\nLast Packet: \n"+ lastReceivedLeft
+ "\n\nAll Messages: \n"+lastReceivedLeft
,style);
GUI.Box(rectObj,"# UDPRightReceive\n127.0.0.1 "+port2+" #\n"
+ "shell> nc -u 127.0.0.1 : "+port2+" \n"
+ "\nLast Packet: \n"+ lastReceivedRight
+ "\n\nAll Messages: \n"+lastReceivedRight
,style);
}
// init
private void init(int port)
{
print("UDPSend.init()");
// define port
//port1= 33555;
//port2 = 33557;
// status
print("Sending to 127.0.0.1 : "+port);
print("Test-Sending to this Port: nc -u 127.0.0.1 "+port+"");
LeftData= new Thread(new ThreadStart(ReceiveLeftData));
RightData = new Thread(new ThreadStart(ReceiveRightData));
LeftData.IsBackground = true;
RightData.IsBackground = true;
LeftData.Start();
RightData.Start ();
}
// receive thread
private void ReceiveLeftData()
{
client1= new UdpClient(port1);
while (true)
{
try
{
// Bytes empfangen.
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 61556);
byte[] data1 = client1.Receive(ref anyIP);
//byte[] data2 = client1.Receive(ref anyIP);
string text1 = Encoding.UTF8.GetString(data1);
// latest UDPpacket
lastReceivedLeft=text1;
print("Left_data:11"+ lastReceivedLeft);
}
catch (Exception err)
{
print(err.ToString());
}
}
}
private void ReceiveRightData()
{
client1 = new UdpClient (port2);
while (true)
{
try
{
// Bytes empfangen.
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 61558);
byte[] data2 = client1.Receive(ref anyIP);
//string text1 = Encoding.UTF8.GetString(data1);
string text2 = Encoding.UTF8.GetString(data2);
lastReceivedRight = text2;
// latest UDPRightpacket
print("Right_data:"+lastReceivedRight);
//allReceivedUDPPackets=allReceivedUDPPackets+text;
//return lastReceivedRight;
}
catch (Exception err)
{
print(err.ToString());
}
}
}
// getLatestUDPPacket
// cleans up the rest
public string getLatestUDPPacket()
{
//allReceivedUDPPackets="";
return lastReceivedLeft += "";
return lastReceivedRight += "";
}
}
using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class UDPDualReceive : MonoBehaviour {
// receiving Thread
// Thread receiveThread;
Thread LeftData;
Thread RightData;
// udpclient object
UdpClient client1;
UdpClient client2;
// public
// public string IP = "127.0.0.1"; default local
public int port1; // define > init
public int port2;
// infos
//these are public so that means you can access them from other scripts.
public string lastReceivedLeft = ""; // Start with a blank string
public string lastReceivedRight = ""; // Start with a blank string
// start from shell, I don't see this function being used : main()
private static void Main()
{
UDPDualReceive receiveObj1=new UDPDualReceive();
UDPDualReceive receiveObj2=new UDPDualReceive();
receiveObj1.init(33555);
receiveObj2.init (33557);
string text="";
do
{
text = Console.ReadLine(); //this does nothing but print out the text so you can view it. it does not store it.
}
while(!text.Equals("exit"));
}
// start from unity3d
public void Start()
{
init(33555);
init (33557);
}
// OnGUI
void OnGUI()
{
Rect rectObj=new Rect(40,10,200,400);
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.UpperLeft;
GUI.Box(rectObj,"# UDPLeftReceive\n127.0.0.1 "+port1+" #\n"
+ "shell> nc -u 127.0.0.1 : "+port1+" \n"
+ "\nLast Packet: \n"+ lastReceivedLeft
+ "\n\nAll Messages: \n"+lastReceivedLeft
,style);
GUI.Box(rectObj,"# UDPRightReceive\n127.0.0.1 "+port2+" #\n"
+ "shell> nc -u 127.0.0.1 : "+port2+" \n"
+ "\nLast Packet: \n"+ lastReceivedRight
+ "\n\nAll Messages: \n"+lastReceivedRight
,style);
}
// init
private void init(int port)
{
print("UDPSend.init()");
LeftData= new Thread(new ThreadStart(ReceiveLeftData));
RightData = new Thread(new ThreadStart(ReceiveRightData));
LeftData.IsBackground = true;
RightData.IsBackground = true;
LeftData.Start();
RightData.Start ();
}
// receive thread
private void ReceiveLeftData()
{
client1= new UdpClient(port1);
while (true)
{
try
{
// Bytes empfangen.
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 61556);
byte[] data1 = client1.Receive(ref anyIP);
//byte[] data2 = client1.Receive(ref anyIP);
string text1 = Encoding.UTF8.GetString(data1);
// latest UDPpacket
lastReceivedLeft = text1; // this is the part that stores a received string the the lastReceivedLeft string.
}
catch (Exception err)
{
print(err.ToString());
}
}
}
private void ReceiveRightData()
{
client1 = new UdpClient (port2);
while (true)
{
try
{
// Bytes empfangen.
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 61558);
byte[] data2 = client1.Receive(ref anyIP);
string text2 = Encoding.UTF8.GetString(data2);
lastReceivedRight = text2; // again this the part that is storing the string
}
catch (Exception err)
{
print(err.ToString());
}
}
}
// getLatestUDPPacket
// cleans up the rest
// i'm not sure why you have this. but nothing is calling it, so thats good.
public string getLatestUDPPacket()
{
//allReceivedUDPPackets="";
return lastReceivedLeft += "";
return lastReceivedRight += "";
}
}
i am getting float values… i have tried TryParse too but the above code always gives incorrect format exception…
as in the above complete script i am getting data in the form of string i want to typecast/parse into float so as to get the calculations done…
float LeftValue = 0; /// default value
if(float.TryParse(ReceiveLeft, out LeftValue))
{
/// everything is ok
Debug.Log(LeftValue);
}
else
{
/// something wrong. has incorrect float value
}