3 Replies Latest reply: Jan 5, 2013 2:16 AM by DarrylBurke RSS

    problem for sockets

    981483
      hello I have a problem with the client class
      it shows that there is a client creation in infinite loop
      and when I start the server, there's a problem with exeception of therad
      this my class client
      import java.io.BufferedReader;
      import java.io.BufferedWriter;
      import java.io.File;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.io.OutputStreamWriter;
      import java.io.PrintWriter;
      import java.net.InetAddress;
      import java.net.NetworkInterface;
      import java.net.Socket;
      import java.util.Calendar;
      
      
      public class Client {
      
      
      
          public Client() {
          }
          public static void main(String args[]) {
      
         //Déclaration des variables
           
         
               String systèmedexploitation;
               long totalMemory;
            long freeMemory;
            long maxMemory;
            String dateheur;
            long EspacedisqueTotal;
            long EspacedisqueUtiliser;
            long EspacedisqueLibre;
            String typesystème;
         
        
           
            Socket socket = null;
            
         while(true)
           try{  
            //création d'un socket client
                System.out.println("création de client ");
           socket = new Socket("169.254.168.6", 3078);
            BufferedReader plec = new BufferedReader(new InputStreamReader(socket.getInputStream()));
           PrintWriter pred = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
           
          
           
           //LES méthode de récupération des données local
      
          
              systèmedexploitation =System.getProperty("os.name");
        typesystème= System.getProperty("os.arch");
        File file = new File( "c:");
        EspacedisqueTotal =  file.getTotalSpace()/1024/1024/1024;
           EspacedisqueLibre = file.getFreeSpace()/1024/1024/1024;
           EspacedisqueUtiliser =(EspacedisqueTotal-EspacedisqueLibre);
        dateheur=Calendar.getInstance().getTime().toLocaleString();
        totalMemory = Runtime.getRuntime().totalMemory()/1024/1024;
        freeMemory = Runtime.getRuntime().freeMemory()/1024/1024;
        maxMemory = Runtime.getRuntime().maxMemory()/1024/1024;
        File file1 = new File( "C:\\Program Files" ); 
        File[] files = file1.listFiles();
        String programmes="";
           for ( int fileInList = 0 ; fileInList < files.length; fileInList++)
           programmes+=files[fileInList].getName()+"  ";
           
         //**************************** adress mac
          String am="";
            NetworkInterface  ni = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
            if (ni != null) {
            byte[] mac = ni.getHardwareAddress();
            if (mac != null) 
            {
             
             
             try{
             for (int i = 0; i < mac.length-1; i++) 
              am+=String.format("%02X",mac)+"-";
      am+=String.format("%02X",mac[mac.length-1]);
      }
      catch(Exception e)
      {
      System.out.println(e.getMessage());
      }

      }
      else
      System.out.println("Address doesn't exist or is not accessible.");

      }
      else
      System.out.println("Network Interface for the specified address is not found.");
      //*****************************



      pred.println(dateheur+"__"+am+"__"+systèmedexploitation+"__"+typesystème+"__"+EspacedisqueTotal+"__"+EspacedisqueUtiliser+"__"+EspacedisqueLibre+"__"+totalMemory+"__"+freeMemory+"__"+maxMemory+"__"+programmes);
      plec.readLine();
      socket.close();
      //-------------------------------------------- scan port


      new Thread(){
      public void run()
      {

      int port=3009;
      String hostname = null;
      PrintWriter pred=null;
      Socket socket=null;
      for (port = 79; port <85; port++)
      try {

      socket = new Socket("169.254.195.182", 3016);

      pred = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);


      InetAddress remote = InetAddress.getByName("localhost");

      hostname = remote.getHostName();
      Socket s = new Socket(remote, port);

      pred.println("ouver on port " + port + " of " + hostname);

      s.close();

      }

      catch (IOException ex) {
           try
           {
      pred.println("Error on port " + port + " of " + hostname);
           }catch(Exception f){
                
           }
      }

      try {
      pred.println("5f");

      socket.close();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }

      }
      }.start();

      //----------------------------------------------------------


      //Fermeture de socket
      socket.close();



      }
      catch(IOException e){
           try
           {
                socket.close();
           }catch(Exception e1)
           {
                
           }
           try {
                     Thread.currentThread().sleep(4000);
                     System.out.println("------");
                } catch (InterruptedException e2) {
                     // TODO Auto-generated catch block
                
                }
      }




      }


      }


      and this my class Serveur
      /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */
      package tpjava;
      
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.net.Socket;
      import java.net.UnknownHostException;
      public class Serveur {
       
           public Serveur() {}
           public static void main(String args[]) throws UnknownHostException, IOException {
       
                //Déclaration des variables
       
                int sport=3078;
                final String adress="127.0.0.1";
                Socket socket = null;
       
       
       
                          System.out.println("Client Try to connect to server in@: "+adress+" on port :"+sport);
       
                          socket = new Socket(adress, sport);
       
                          BufferedReader plec = new BufferedReader(new InputStreamReader(socket.getInputStream()));
       
                          System.out.println("Response : id client is :<<"+plec.readLine()+">>");
       
       
           }
      }
      erreur d'exception
      Client Try to connect to server in@: 127.0.0.1 on port :3078
      Exception in thread "main" java.net.ConnectException: Connection refused: connect
           at java.net.DualStackPlainSocketImpl.connect0(Native Method)
           at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
           at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
           at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
           at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
           at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
           at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
           at java.net.Socket.connect(Socket.java:579)
           at java.net.Socket.connect(Socket.java:528)
           at java.net.Socket.<init>(Socket.java:425)
           at java.net.Socket.<init>(Socket.java:208)
           at tpjava.Serveur.main(Serveur.java:27)
      Java Result: 1
      BUILD SUCCESSFUL (total time: 1 second)