Visualizzazione dei risultati da 1 a 2 su 2

Discussione: [JAVA]Socket

  1. #1

    [JAVA]Socket

    codice:
    import java.net.*;
    import java.io.*;
    
    class Mail
    {
     private Socket out_sock = null;
     public Mail()
     { 
      try
      {
       Socket out_sock = new Socket(InetAddress.getLocalHost(), 8080);
      }
      catch(IOException ioe)
      {
       System.out.println(ioe.toString());
      }
     }
     public Mail(String name)
     {
      try
      {
       Socket out_sock = new Socket(InetAddress.getByName(name), 8080);
      }
      catch(IOException ioe)
      {
       System.out.println(ioe.toString());
      }
     }
     public Mail(String name, int port)
     {
      try
      {
       Socket out_sock = new Socket(InetAddress.getByName(name), port);
      }
      catch(IOException ioe)
      {
       System.out.println(ioe.toString());
      }
     }
     public void doClose()
     {  
      try
      {
       out_sock.close();
      }
      catch(IOException ioe)
      {
       System.out.println(ioe.toString());
      }
     }
     public Socket getSock()
     {
      return out_sock;
     }
     
    }
    
    class Mailer
    {
     public static void main(String[] args)
     {
      Mail ml = new Mail();
      try
      {
          DataOutputStream OUT = new DataOutputStream(ml.getSock().getOutputStream());
          DataInputStream IN = new DataInputStream(ml.getSock().getInputStream());
          OUT.writeChars("CIAO");
          System.out.println("Letto " + IN.readLine());
         }
         catch(IOException ioe)
      {
       System.out.println(ioe.toString());
      }
      ml.doClose();
     }
    }
    Perche IN nn mi legge quello che OUT ha scritto???


    Tnk
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  2. #2
    Perchè il sokcet della classe Mail è a null, devi stare attento.

    codice:
    class Mail
    {
     private Socket out_sock = null;
     public Mail()
     { 
      try
      {
    //Errore
    //Socket out_sock = new Socket(InetAddress.getLocalHost(), 8080);
    //Corretto
    out_sock = new Socket(InetAddress.getLocalHost(), 8080);
      }
      catch(IOException ioe)
      {
       System.out.println(ioe.toString());
      }
     }
     //E così anche per gli altri costruttori
    Lang=Java
    Ambiente = Eclipse forever
    Ubuntu & Win XP Pro

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.