Pagina 1 di 4 1 2 3 ... ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 37
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2005
    Messaggi
    2,125

    struttura dati (buffer)

    ciao....
    ho un buffer dati in c# e vorrei sapere l'equivalente in php....come deve essere?
    Codice PHP:

    typedef struct _CMProduct
    {
        
    char          product[20];      // Stringa del nome prodotto
        
    char          name[20];         // Nome device
        
    char          serial[8];        // Serial number per WT sono le ultime 3 cifre del MAC Address
        
    unsigned char bUsedhcp;
        
    unsigned char abFill[3];
        
    unsigned long uIp;              // Indirizzo IP della deviced
        
    unsigned long uPort;
        
    unsigned long uSw_ver;
        
    unsigned long uHw_ver;
        
    unsigned long uFlags;       // FLAG_...
        
    unsigned long uGp0;
        
    unsigned long uGp1;
        
    unsigned long uGp2;
        
    unsigned long uGp3;



    devo creare una classe? un array ? come posso definire le variabili assegnando quandi char hanno? il tipo di variabile ecc?

  2. #2
    Utente di HTML.it
    Registrato dal
    Jun 2005
    Messaggi
    2,125
    up

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2005
    Messaggi
    2,125
    up

  4. #4
    un esempio molto basilare ...
    codice:
    <?php
    
    class Sharp_char {
    	
    	private	$__len = 0;
    	private	$__str = "";
    
    
    	public function __construct($len = 0){
    		$this->__len = $len;
    	}
    
    	public function __call($key, $values){
    		if($key === 'set')
    			$this->__str = $this->__len ? substr($values[0], 0, $this->__len) : $values[0];
    		return $this->__str;
    	}
    };
    
    class Sharp_long {
    
    	private	$__value = 0;
    
    	public function __call($key, $values){
    		if($key === 'set')
    			$this->__value = (double) $values[0];
    		return $this->__value;
    	}
    };
    
    class _CMProduct {
    
    	private function __throw(&$key) {
    		throw(new Exception("Undefined key: {$key}"));
    	}
    
    	private $__product;
    
    	public function __construct(){
    		$this->__product = array(
    			'product'	=> new Sharp_char(20),
    			'name'		=> new Sharp_char(20),
    			'serial'	=> new Sharp_char(8),
    			'bUsedhcp'	=> new Sharp_char(1),
    			'abFill'	=> new Sharp_char(3),
    			'uIp'		=> new Sharp_long,
    			'uPort'		=> new Sharp_long,
    			'uSw_ver'	=> new Sharp_long,
    			'uHw_ver'	=> new Sharp_long,
    			'uFlags'	=> new Sharp_long,
    			'uGp0'		=> new Sharp_long,
    			'uGp1'		=> new Sharp_long,
    			'uGp2'		=> new Sharp_long,
    			'uGp3'		=> new Sharp_long
    		);
    	}
    	
    	public function __get($key){
    		$result = null;
    		if(isset($this->__product[$key]))
    			$result = $this->__product[$key]->get();
    		else
    			$this->__throw($key);
    		return $result;
    	}
    
    	public function __set($key, $value){
    		$result = null;
    		if(isset($this->__product[$key]))
    			$result = $this->__product[$key]->set($value);
    		else
    			$this->__throw($key);
    		return $result;
    	}
    };
    
    $test = new _CMProduct();
    $test->product = "01234567890123456789a";
    $test->abFill = "abcdefghi";
    $test->uFlags = "1234.5678";
    echo implode('
    ', array($test->product, $test->abFill, $test->uFlags));
    ?>

    Produce:
    codice:
    01234567890123456789
    abc
    1234.5678
    ma solo con PHP 5 e superiori.

    Per simulare al meglio il comportamento di C# con PHP che non è Strict Typed devi fare uso massiccio di controlli, creare tipi di oggetti analoghi e sfruttare molto le eccezioni
    Formaldehyde a new Ajax PHP Zero Config Error Debugger

    WebReflection @WebReflection

  5. #5
    Utente di HTML.it
    Registrato dal
    Jun 2005
    Messaggi
    2,125
    grazie mille per avermi risposto....un po complicatino il dafarsi...cmq ci provo...

    una domanda.... io avrei necessita di passare quella struttura dati alla socket_sendto()....come posso fare?
    Devo concatenare i contenuti di tutti gli attributi della classe che hai postato?
    perche a quanto pare accetta stringhe la funzione sendto
    int socket_sendto ( resource socket, STRING BUF, int len, int flags, string addr [, int port] )

  6. #6
    il complicato l'hai voluto tu
    per il semplice fatto che un linguaggio typeless come è php diventa addirittura più lento se si vincola tutto a strict causa implementazione dei controlli in code.

    Come vedi seppur lievemente contorto puoi farci un pò di tutto, anche emulare C# ma secondo me il problema è a monte, ovvero cercare in PHP il C# e/o viceversa

    Per inviare dati puoi usare la serializzazione (serialize/unserialize) ma in C# non mi sembra ci sia una valida unserialize degna di tale nome e la mia l'avevo praticamente abbandonata perchè mi avevano detto fosse poco utile ... in caso se serve la riposto ma fa uso, ovviamente, di metodi per creare runtime tramite DLL dedicata e precompilata di classi esistenti ed analoghe in C#, insomma è decisamente molto più complicato di quello che stai cercando di fare.

    Perchè non sfrutti J# e tramite il PHP interagisci col Java e crei oggetti Java?
    Magari riesce meglio l'interfacciamento PHP esterno => .NET


    [edit]
    al limite JSON ma devi implementare un metodo apposito per convertire i dati ed inviarli via socket come stringa
    Formaldehyde a new Ajax PHP Zero Config Error Debugger

    WebReflection @WebReflection

  7. #7
    Utente di HTML.it
    Registrato dal
    Jun 2005
    Messaggi
    2,125
    purtroppo devo realizzare il tutto con php....e ci sono quasi riuscito...prima creavo un socket udp e attendevo risposte da any address.....il fatto e' che devo passare al send to una struttura dati complessa. Il codice che mi hai postato sembra ottimo....non ci capisco molto ma sembra la stessa struttura del C#.....

    A questo punto...devo passare tale struttura al sendto..... Posteresti un po di codice di come posso fare?

    meglio cose semplici...sono nabbo.

    cmq grazie della disponibilita

  8. #8
    la via più semplice è JSON ... puoi fare un overload del ToString nella classe _CMProducts aggiungendo questo metodo in fondo:
    codice:
    	public function __toString(){
    		$result = array();
    		while(list($key,) = each($this->__product))
    			$result[$key] = $this->__product[$key]->get();
    		return json_encode($result);
    	}
    Poi l'utilizzo sarà qualcosa tipo
    codice:
    $test = new _CMProduct();
    $test->product = "01234567890123456789a";
    $test->abFill = "abcdefghi";
    $test->uFlags = "1234.5678";
    echo $test;
    Che produce:
    codice:
    {"product":"01234567890123456789","name":"","serial":"","bUsedhcp":"","abFill":"abc","uIp":0,"uPort":0,"uSw_ver":0,"uHw_ver":0,"uFlags":1234.5678,"uGp0":0,"uGp1":0,"uGp2":0,"uGp3":0}
    Formaldehyde a new Ajax PHP Zero Config Error Debugger

    WebReflection @WebReflection

  9. #9
    Utente di HTML.it
    Registrato dal
    Jun 2005
    Messaggi
    2,125
    mi sa che ci stiamo proprio avvicinando a quello che volevo ottenere. GRAZIE MILLE....

    un piccolo errore mi blocca....
    "call to undefined function json_encode()" .....devo abilitare qualche modulo?

    mi sa che devo passare alla 5.2 giusto...? io ho la PHP Version 5.1.6

  10. #10
    devi avere l'estensione json abilitata ... l'host è tuo ? In caso passi a PHP 5.2.X e te la ritrovi in automatico altrimenti devi andare sul sito PECL a cercarla ed installarla a mano oppure andare su Pear e prendere la classe JSONService ed usare quella per fre l'encode
    Formaldehyde a new Ajax PHP Zero Config Error Debugger

    WebReflection @WebReflection

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.