Results 1 to 3 of 3
Thread: problem for sockets
- 12-24-2012, 11:39 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 1
- Rep Power
- 0
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 clientand this my class ServeurJava 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 } } } }
erreur d'exceptionJava 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()+">>"); } }
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)
- 01-05-2013, 02:23 AM #2
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.
- 01-05-2013, 09:17 AM #3
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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Problem with Datagram-Sockets
By php.freak in forum New To JavaReplies: 1Last Post: 06-03-2012, 05:01 PM -
Problem with Sockets
By shanoger in forum NetworkingReplies: 1Last Post: 04-06-2011, 02:21 PM -
Problem with Sockets
By pinheiroa in forum NetworkingReplies: 15Last Post: 09-05-2010, 10:53 PM -
TCP sockets problem
By duffman.sk in forum NetworkingReplies: 1Last Post: 10-30-2009, 05:06 AM -
Problem (sockets)
By Blacknight962 in forum New To JavaReplies: 3Last Post: 08-16-2009, 12:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks