Results 1 to 3 of 3
  1. #1
    jaafar is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default problem for sockets

    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
    Java Code:
    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[i])+"-";
           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
    Java Code:
    /*
     * 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
    Java Code:
    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)

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: problem for sockets

    It is trying to connect to 127.0.0.1 on port :3078 in the Serveur class.
    I believe you need to use a ServerSocket to start a server.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: problem for sockets

    Cross posted and got the same answer nearly 2 weeks ago (and didn't have the courtesy to reply to responses there)
    https://forums.oracle.com/forums/thr...readID=2479586

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Problem with Datagram-Sockets
    By php.freak in forum New To Java
    Replies: 1
    Last Post: 06-03-2012, 05:01 PM
  2. Problem with Sockets
    By shanoger in forum Networking
    Replies: 1
    Last Post: 04-06-2011, 02:21 PM
  3. Problem with Sockets
    By pinheiroa in forum Networking
    Replies: 15
    Last Post: 09-05-2010, 10:53 PM
  4. TCP sockets problem
    By duffman.sk in forum Networking
    Replies: 1
    Last Post: 10-30-2009, 05:06 AM
  5. Problem (sockets)
    By Blacknight962 in forum New To Java
    Replies: 3
    Last Post: 08-16-2009, 12:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •