Results 1 to 12 of 12

Thread: java&socket

  1. #1
    Lieutenant Junior Grade
    Join Date
    Sep 2004
    Location
    @MyHome
    Posts
    4.362

    Default java&socket

    qualcuno mi sa dire cosa c'è che non và in questo codice?
    Code:
    import java.net.*;
    import java.util.logging.*;
    import java.lang.*;
    public class ServerSomma {
    public static void main(String[] args) throws Exception{
    Logger logger = Logger.getLogger("global");
    //logger.setLevel(Level.OFF);
    if (args.length != 3)
    System.out.println("Need 3 parameter!");
    else{
    int myPort=Integer.parseInt(args[2]);
    int sum=0, num;
    String message;
    final int MAX_LEN = 10;
    try {
    InetAddress receiverHost =InetAddress.getByName(args[0]);
    int receiverPort = Integer.parseInt(args[1]);
    logger.info("Creo il socket");
    DatagramSocket mySocket =new DatagramSocket(myPort);
    logger.info("Creo il datagram packet");
    byte[] buffer = new byte[MAX_LEN];
    DatagramPacket datagram=new DatagramPacket(buffer, MAX_LEN, receiverHost, receiverPort);
    for(int i=0;i<999;i++)
    {
    num=0;
    buffer= new byte[MAX_LEN];
    logger.info("in attesa di ricevere...");
    mySocket.receive(datagram);
    message=new String(buffer);
    message=message.trim();
    num=Integer.parseInt(message);
    sum=sum+num;
    logger.info(""+sum);
    message=""+sum;
    buffer= new byte[MAX_LEN];
    buffer= message.getBytes();
    logger.info("invio la somma parziale");
    mySocket.send(datagram);
    }
    mySocket.close();
    }
    catch (Exception ex){
    ex.printStackTrace();				
    }
    }
    }
    }
    dovrebbe ricevere un numero su una certa porta, poi restituire ad un ip : porta la somma parziale dei numeri ricevuti...
    restituisce somme sballate..
    qualcuno sa aiutarmi?
    Lux.
    Dwightz.

  2. #2
    Tanek's Avatar
    Join Date
    Apr 2004
    Location
    Milano, Midgard
    Posts
    11.225

    Default

    Con /code salta l'indent?

    Tanek™: Game Designer & Algorithm Mastermind, Team Leader & SW Engineer and Dungeon Master!
    "Datte Foco"™ and "Ma KITTESENCULA"™ are registered trademarks of Tanek Entertainment Inc.
    ‎"One of these days, scientists will discover that second X chromosome contains nothing but nonsense and twaddle." - Sheldon Cooper
    Per non dimenticare:
    Spoiler


  3. #3
    Lieutenant Junior Grade
    Join Date
    Sep 2004
    Location
    @MyHome
    Posts
    4.362

    Default

    si
    Lux.
    Dwightz.

  4. #4
    Lieutenant Commander Axet's Avatar
    Join Date
    Sep 2003
    Location
    Ginevra
    Posts
    33.807

    Default

    Per la tesina avevo fatto un programma tipo emule (server e N client) in VB.
    La stragrande maggioranza delle volte capitavano degli errori perchè in pratica lo scambio di dati, regolato da un protocollo "home made", avveniva troppo velocemente.

    Essenzialmente a volte arrivavano le intestazioni delle stringhe + le stringhe + parte della stringa precedente, il tutto quindi risultava sballato in fase di lettura da parte del secondo client.

    All'epoca risolsi il problema inserendo un sistema simile allo stop & wait.. in pratica non trasmetti il pacchetto successivo finchè l'altro client non ti conferma di aver ricevuto quello appena inviato.
    Dopo aver inserito questo accorgimento funzionava correttamente.

    A me pare che il for spari fuori a rullo i pacchetti senza la minima temporizzazione, magari portebbe essere questo

    I'm no hero. Never was. Never will be.
    -----
    Soul of the mind, key to life's ether
    Soul of the lost, withdrawn from its vessel
    May strength be granted so the world might be mended...
    So the world might be mended...

  5. #5
    Lieutenant Commander San Vegeta's Avatar
    Join Date
    Oct 2003
    Location
    Bologna
    Posts
    12.153

    Default

    non c'ho tempo di guardarci ora, magari vedo dopo
    I rubinetti a casa di Chuck Norris non perdono, vincono.

    In the beginning there was nothing...then Chuck Norris Roundhouse kicked that nothing in the face and said "Get a job". That is the story of the universe.

    Quote Originally Posted by Wolfo View Post
    Concordo e propongo ban temporanei per chi critica la topa , la topa non si critica , dal trombabile in su non si commenta in modo sgradevole.
    la tua ignoranza in materia e' raccapricciante
    -cit. Estrema, 2022

  6. #6
    Lieutenant Junior Grade
    Join Date
    Sep 2004
    Location
    @MyHome
    Posts
    4.362

    Default

    forse ho trovato il problema... probabilmente non mi ripulisce l'array di byte...

    come faccio a ripulirlo?
    Lux.
    Dwightz.

  7. #7
    Warrant Officer
    Join Date
    Nov 2003
    Location
    Suisse
    Posts
    2.969

    Default

    ripulire in che senso ? Distruggerlo ? Resettarlo tutto a 0 ?
    I will be back, eventually.

  8. #8
    Lieutenant Junior Grade
    Join Date
    Sep 2004
    Location
    @MyHome
    Posts
    4.362

    Default

    "inizializzarlo"

    non so se array di byte inizializzato va tutto a 0...
    Lux.
    Dwightz.

  9. #9
    Lieutenant Commander Axet's Avatar
    Join Date
    Sep 2003
    Location
    Ginevra
    Posts
    33.807

    Default

    Non credo che c'entri, il problema in quel caso ti si dovrebbe presentare solo sull'ultimo array che invii perchè magari non è pieno e i valori che lui non riempie rimangono settati a quelli del giro precedente.

    Hai controllato se il problema può esser quel che ho detto all'inizio?

    I'm no hero. Never was. Never will be.
    -----
    Soul of the mind, key to life's ether
    Soul of the lost, withdrawn from its vessel
    May strength be granted so the world might be mended...
    So the world might be mended...

  10. #10
    Lieutenant Junior Grade
    Join Date
    Sep 2004
    Location
    @MyHome
    Posts
    4.362

    Default

    il problema non è quello...
    cmq ho notato che se il primo client invia 8... poi somma sempre 8 quindi al secondo client restituisce 16 poi 24 ecc...

    quindi penso che il problema sia di array
    Lux.
    Dwightz.

  11. #11
    Lieutenant Commander San Vegeta's Avatar
    Join Date
    Oct 2003
    Location
    Bologna
    Posts
    12.153

    Default

    non ho una documentazione sottomano, cmq dovrebbe esserci un metodo clear(), reset() o qualcosa di simile... se non c'è ti fai una funzioncina del tipo
    Code:
    for(int i=0; i < ARRAY_LENGTH; i++) {
    myByteArray[i] = 0;
    }
    sfrutta la documentazione ufficiale sul sito della sun, dice tutto quello che serve, inizializzazione, valori di default e metodi degli oggetti

    per il resto avrei bisogno di debuggare il programma per dirti dov'è l'errore, così leggendo non lo noto
    I rubinetti a casa di Chuck Norris non perdono, vincono.

    In the beginning there was nothing...then Chuck Norris Roundhouse kicked that nothing in the face and said "Get a job". That is the story of the universe.

    Quote Originally Posted by Wolfo View Post
    Concordo e propongo ban temporanei per chi critica la topa , la topa non si critica , dal trombabile in su non si commenta in modo sgradevole.
    la tua ignoranza in materia e' raccapricciante
    -cit. Estrema, 2022

  12. #12
    Petty Officer 3rd Class rosc's Avatar
    Join Date
    Mar 2004
    Posts
    273

    Default

    Ricordando esperienze antiche può essere che ti arrivino più pacchetti, cioè devi fare receive finchè la somma della lunghezza dei pacchetti è uguale alla lunghezza del messaggio.
    vedi la documentazione del DatagramSocket.receive (http://java.sun.com/j2se/1.4.2/docs/api/).
    Spero ti sia di aiuto.
    Ciao
    Daoc Ywain (off):
    Jamello, nightshade
    Shashamano, shaman
    Thorello, thane
    LOL USA (off)
    Jamello
    LOL EU (off)
    Jamelloo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
[Output: 83.24 Kb. compressed to 70.27 Kb. by saving 12.97 Kb. (15.58%)]