problème réception udp

Pour les scripts écrits en C#
Règles du forum
Merci de respecter la NOMENCLATURE suivante pour vos TITRES de messages :

Commencez par le niveau de vos scripts
DB = Débutant
MY = Moyen
CF = Confirmé

Puis le domaine d'application
-RS = Réseau
-AL = Algorithmie

Exemple :

[DB-RS] Mouvement perso multijoueur
sawi
Messages : 3
Inscription : 06 Avr 2015 20:34

problème réception udp

Message par sawi » 08 Avr 2015 15:59

salut à tous :)
je suis entrain de faire une communication UDP , le problème c'est que sous l'éditeur de unity je ne reçoit rien quand j'envoie des message à partir d'une tablette mais quand je fait le building sur mon pc et que je renvois des message je reçois ce que j'ai écris sur la tablette :?:
j'arrive pas à comprendre ou se trouve le problème !
voici le code que j'utilise
pour la réception

Code : Tout sélectionner

using UnityEngine;
using System.Collections;

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class udpreceive : MonoBehaviour {
	
	
	// receiving Thread
	Thread receiveThread;
	
	// udpclient object
	UdpClient client;
	
	// public
	// public string IP = "127.0.0.1"; default local
	public int port=26500; // define > init
	
	// infos
	public string lastReceivedUDPPacket="";
	public string allReceivedUDPPackets=""; // clean up this from time to time!
	
	
	// start from shell
	//	private static void Main()
	//	{
	//		
	//		UDPReceive receiveObj=new UDPReceive();
	//		receiveObj.init();
	//		
	//	}
	// start from unity3d
	public void Start()
	{
		
		Application.runInBackground = true;
		init(); 
		
	}
	
	// OnGUI
	void OnGUI()
	{
		
		Rect rectObj=new Rect(40,10,200,400);
		GUIStyle style = new GUIStyle();
		style.alignment = TextAnchor.UpperLeft;
		GUI.Box(rectObj,"# UDPReceive\n192.168.158.53 "+port+" #\n"
		        + "shell> nc -u 192.168.158.53 : "+port+" \n"
		        + "\nLast Packet: \n"+ lastReceivedUDPPacket
		        + "\n\nAll Messages: \n"+allReceivedUDPPackets
		        ,style);
		
	}
	
	// init
	public void init()
	{
		
		// Endpunkt definieren, von dem die Nachrichten gesendet werden.
		Debug.Log("UDPSend.init()");
		
		// define port
		port = 26500;
		
		// status
		Debug.Log("Sending to 192.168.158.53 : "+port);
		//Debug.Log("Test-Sending to this Port: nc -u 127.0.0.1  "+port+"");
		
		
		// ----------------------------
		// Abhören
		// ----------------------------
		// Lokalen Endpunkt definieren (wo Nachrichten empfangen werden).
		// Einen neuen Thread für den Empfang eingehender Nachrichten erstellen.
		receiveThread = new Thread(
			new ThreadStart(ReceiveData));
		receiveThread.IsBackground = true;
		receiveThread.Start();
		
		
	}
	
	// receive thread
	private  void ReceiveData()
	{
		
		
		client = new UdpClient(port);
		while (true)
		{
			
			
			try
			{
				
				// Bytes empfangen.
				IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
				byte[] data = client.Receive(ref anyIP);
				
				// Bytes mit der UTF8-Kodierung in das Textformat kodieren.
				string text = Encoding.UTF8.GetString(data);
				
				// Den abgerufenen Text anzeigen.
				Debug.Log(">> " + text);
				
				// latest UDPpacket
				lastReceivedUDPPacket=text;
				
				// ....
				allReceivedUDPPackets=allReceivedUDPPackets+text;
				
				
			}
			catch (Exception err)
			{
				
				print(err.ToString());
				
			}
			
		}
		
	}
	
	// getLatestUDPPacket
	// cleans up the rest
	public string getLatestUDPPacket()
	{
		
		allReceivedUDPPackets="";
		return lastReceivedUDPPacket;
		
	}
	void OnDisable() 
	{ 
		if ( receiveThread!= null) 
			receiveThread.Abort(); 
		
		client.Close(); 
	} 
	
}
pour l'émission

Code : Tout sélectionner

/*
     
        -----------------------
        UDP-Send
        -----------------------
        // [url]http://msdn.microsoft.com/de-de/library/bb979228.aspx#ID0E3BAC[/url]
       
        // > gesendetes unter
        // 127.0.0.1 : 8050 empfangen
       
        // nc -lu 127.0.0.1 8050
     
            // todo: shutdown thread at the end
    */
using UnityEngine;
using System.Collections;

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class udpsend : MonoBehaviour
{
	private static int localPort;
	//private string textFieldString = "";
	// prefs
	public static string IP="192.168.158.53";  // define in init
	public static int port= 26500;  // define in init
	IPEndPoint remoteEndPoint;
	UdpClient client;
	//private EasyJoystick sendjoystick;
	//private iGUICode_start Menu;
	//public static double x1;
	//public static double y1;
	// "connection" things
	//public GameObject G;
	// gui
	string strMessage="";
	//string st="";
	//public int P;
	//public string IPadd;
	// call it from shell (as program)
	public void Start()
	{
		init(); 
		//sendjoystick = GameObject.Find("Move_Turn_Joystick").GetComponent<EasyJoystick>();
		//G.gameObject.renderer.material.color = new Color(0.5f,1,1);
		
		//P=int.Parse(iGUICode_start.getInstance().textfield1.value);
		//IPadd = iGUICode_start.getInstance ().textfield2.value;
		//Debug.Log (IPadd);
		//port = P;
	}
	private static void Main()
	{
		udpsend sendObj=new udpsend();
		sendObj.init();
		
		// testing via console
		// sendObj.inputFromConsole();
		
		// as server sending endless
		sendObj.sendEndless(" endless infos \n");
		
	}
	// start from unity3d
	
	
	// OnGUI
	void OnGUI()
	{
		//		Debug.Log (iGUICode_start.getInstance().textfield1.value);
		
		Rect rectObj=new Rect(40,380,200,400);
		GUIStyle style = new GUIStyle();
		style.alignment = TextAnchor.UpperLeft;
		GUI.Box(rectObj,"# UDPSend-Data\n192.168.158.53 "+port+" #\n"
		        + "shell> nc -lu 192.168.158.53  "+port+" \n"
		        ,style);
		
		// ------------------------
		// send it
		// ------------------------
		strMessage=GUI.TextField(new Rect(40,420,140,20),strMessage);
		if (GUI.Button(new Rect(190,420,40,20),"send"))
		{
			sendString(strMessage);
			
		}  
		
		//port=P;
		//		textFieldString = GUI.TextField (new Rect (25, 25, 100, 30),"le port est "+ port.ToString());
		//		x1 = System.Convert.ToDouble (sendjoystick.JoystickAxis.x);
		//		x1 = Math.Round (x1,2);
		//		y1 = System.Convert.ToDouble (sendjoystick.JoystickAxis.y);
		//		y1 = Math.Round (y1,2);
		
		//		Debug.Log (x1);
		//sendString (x1+";"+y1+";"+"conect");
		//System.Threading.Thread.Sleep(10);
		
	}
	
	// init
	public void init()
	{
		// Endpunkt definieren, von dem die Nachrichten gesendet werden.
		print("UDPSend.init()");
		//Debug.Log (sendjoystick.inertia.x);
		// define
		IP="192.168.158.53";
		//IP="127.0.0.1";
		//IP=iGUICode_start.getInstance ().textfield2.value;
		Debug.Log (IP);
		//port=int.Parse(textFieldString);
		
		//port=P;
		//Debug.Log (port);
		// ----------------------------
		// Senden
		// ----------------------------
		remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
		client = new UdpClient();
		
		// status
		print("Sending to "+IP+" : "+port);
		print("Testing: nc -lu "+IP+" : "+port);
		
	}
	
	// inputFromConsole
	private void inputFromConsole()
	{
		try
		{
			string text;
			do
			{
				text = Console.ReadLine();
				
				// Den Text zum Remote-Client senden.
				if (text != "")
				{
					
					// Daten mit der UTF8-Kodierung in das Binärformat kodieren.
					byte[] data = Encoding.UTF8.GetBytes(text);
					
					// Den Text zum Remote-Client senden.
					client.Send(data, data.Length, remoteEndPoint);
				}
			} while (text != "");
		}
		catch (Exception err)
		{
			print(err.ToString());
		}
		
	}
	
	// sendData
	private void sendString(string message)
	{
		try
		{
			//if (message != "")
			//{
			
			// Daten mit der UTF8-Kodierung in das Binärformat kodieren.
			byte[] data = Encoding.UTF8.GetBytes(message);
			
			// Den message zum Remote-Client senden.
			client.Send(data, data.Length, remoteEndPoint);
			//}
		}
		catch (Exception err)
		{
			print(err.ToString());
		}
	}
	
	
	// endless test
	private void sendEndless(string testStr)
	{
		do
		{
			sendString(testStr);
			
			
		}
		while(true);
		
	}
	public void CloseClient()
	{
		if(client!=null)
		{	
			client.Close();
		}
		
	}
	
}

Lenrys
Messages : 5
Inscription : 18 Fév 2015 17:40

Re: problème réception udp

Message par Lenrys » 08 Avr 2015 17:30

[delete]
Dernière édition par Lenrys le 25 Oct 2018 20:36, édité 1 fois.

sawi
Messages : 3
Inscription : 06 Avr 2015 20:34

Re: problème réception udp

Message par sawi » 09 Avr 2015 16:03

merci pour m'avoir répondre :)
j'ai résolu le problème en fait c'était le pare-feu de mon pc est activé donc mon pc ne peut rein recevoir par contre quand je fait le building unity autorise la réception c'est pourquoi que je reçois les messages !

Répondre

Revenir vers « (C#) CSharp »