Announcement

Collapse
No announcement yet.

keine Verbindung

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • keine Verbindung

    Hallo<br>
    Wenn ich mit urlConnection eine Verbindung aufbaue ohne das ich online bin, bekomme ich eine UnknownHostException. Wenn ich nun online gehe ohne die Application neu zu starten erhalte ich auch weiterhin diese Exception und kann somit keine Verbindung aufbauen. Erst nachdem ich meine Application neu gestartet habe funktioniert alles.
    <br><br>
    URL url = new URL("http://www.java.de");
    URLConnection connection = url.openConnection();
    <br><br>
    Leider habe ich noch keine Lösung für dieses Problem gefunden.<br>
    Ich würde mich freuen wenn mir hier jemand weiterhelfen kann.
    Gruß
    Andreas

  • #2
    hi andy,

    tja, da bleibt nur ein pollen, bzw. testen der gueltigkeit des
    hosts:

    <pre>
    public class Client extends Thread {
    //...
    String sHostAddress, sName;
    int port;
    BufferedReader br = null;
    Socket client = null;
    boolean showAt1st;
    public Client( String sName, int port ) {
    showAt1st = true;
    this.sName = sName;
    this.port = port;
    }//modified constructor
    public void getConnection() {
    while( true ) {
    try {
    client = new Socket( sName, port );
    br = new BufferedReader( new InputStreamReader( client.getInputStream() ) );
    InetAddress addr = null;
    sHostAddress = new String( "null" );
    try {
    addr = InetAddress.getByName( "localhost" );
    sHostAddress = addr.getHostAddress();
    } catch( UnknownHostException uhe ) {
    System.out.println( "> unknown host!" );
    }
    break;
    } catch( IOException ioe ) {
    if( showAt1st ) {
    showAt1st = false;
    System.err.println( "> server is down (host exists? - break with \"STRG+C\"). waiting for server..." );
    }//if
    }//catch
    }//while
    }
    public void run() {
    getConnection();
    //...
    }
    public static void main( String[] args ) {
    String sName = new String( "localhost" );
    int port = 5000;
    if( args.length > 0 )
    sName = args[0];
    if( args.length > 1 )
    port = Integer.valueOf( args[1] ).intValue();
    Client my = new Client( sName, port );
    Thread clientThread = new Thread( my );
    clientThread.start();
    }//main

    }
    </pre>

    gruss<br>
    thomas<br>
    --<br>
    <i>http://www.cs.uni-magdeburg.de/~funke/index.html
    mailto://[email protected]</i&gt

    Comment


    • #3
      Danke für die Antwort<br>
      das werd ich dann gleich mal ausprobieren.<br>
      <br>
      Gruß<br>
      Andrea

      Comment

      Working...
      X