Portscanner funktioniert nicht!

cdings

Aktives Mitglied
Hi, ich habe ein Problem mit dem Portscanner.

bei :
Code:
Socket s = new Socket(host, i);
kommt: The constructor Socket(String, int) is undefined

dabei wurden die doch ganz klar definiert!

hier der code:

Code:
  import java.net.*; 
 import java.io.*; 
  
  public class Portscanner { 

 public static void main(String[] args) { 
    
   String host = "localhost"; 
 
   if (args.length > 0) { 
    host = args[0]; 
   } 
   for (int i = 1; i < 1024; i++) { 
    try { 
      Socket s = new Socket(host, i); 
       System.out.println("There is a server on port " + i + " of "  
        + host); 
    } 
    catch (UnknownHostException e) { 
      System.err.println(e); 
      break; 
   } 
    catch (IOException e) { 
      // must not be a server on this port 
    } 
  }
 } 
}
 
hi, ne habe ich nicht.

ich habs dann versucht, die klasse so umzuschreiben, wie ichs mir beigebracht habe.
sprich: ich habe rumexperimentiert.

das sieht dann jetzt so aus:

Code:
    import java.net.*; 
import java.io.*; 
  
  public class Portscanner { 

 public static void main(String[] args) { 
    
	 InetAddress host = null;
   
 
	 try
	 {
		 host = InetAddress.getByName( "localhost" );
		 
	 }
	 catch (Exception e)
	 {
		 System.out.println("nicht erreichbar");
	 }
	 
	 
  
   for (int i = 1; i < 1024; i++) { 
	  
	   try { 
      Socket s = new Socket(host, i); 
       System.out.println("There is a server on port " + i + " of "  
        + host); 
    } 
    catch (UnknownHostException e) { 
      System.err.println(e); 
      break; 
   } 
    catch (IOException e) { 
       
    } 
  }
 } 
}

klappt aber trotzdem nicht...
ist ja logisch, ist ja dasselbe in grün
 
Zuletzt bearbeitet:
Hi,
das gleiche Problem tritt auch auf, wenn ich ein Programm habe, welches eingehende TCP-Pakete empfangen soll.

Sobald ich "ServerSocket" zu "DatagramSocket" ändere, funktioniert alles.

Aber bei "ServerSocket" kommt die selbe Fehlermeldung!

Code:
   import java.net.*;
import java.util.*;

public class Empfang
{
  public static void main( String args[] )
  {
    try
    {
      DatagramSocket socket = new DatagramSocket( 4711 );
      DatagramPacket packet;

      while ( true )
      {
        // Auf Anfrage warten

        packet = new DatagramPacket( new byte[1024], 1024 );
        socket.receive( packet );

        // Empfänger auslesen

       // InetAddress address = packet.getAddress();
        // int         port    = packet.getPort();
        // int         len     = packet.getLength();
       // byte        data[]  = packet.getData();

        System.out.println( "läuft");
        		//"Anfrage von " + address +
                     //       " vom Port " + port +
                       //     " Länge " + len +
                         //   "\n" + new String( data, 0, len ) );
      }
    }
    catch ( Exception e )
    {
      System.out.println( e );
    }
  }
}
 
Zuletzt bearbeitet:
mhn unten fehlen halt ein paar } aber grunsätzlich kompiliert das bei mir

Java:
import java.net.*;
import java.io.*;

public class Portscanner {

	public static void main(String[] args) {

		InetAddress host = null;

		try {
			host = InetAddress.getByName("localhost");

		} catch (Exception e) {
			System.out.println("nicht erreichbar");
		}

		for (int i = 1; i < 1024; i++) {

			try {
				Socket s = new Socket(host, i);
				System.out.println("There is a server on port " + i + " of " + host);
			} catch (UnknownHostException e) {
				System.err.println(e);
				break;
			} catch (IOException e) {

			}
		}
	}
}

ach ja...

Java:
catch (IOException e) { 
      // must not be a server on this port 
    }
sowas würde ich mir schnellstens wieder abwöhnen.. exceptions verschlucken ist selten eine gute idee.
 
Zuletzt bearbeitet:
hi und danke für den tip!

es ist schon komisch. das programm lässt sich mit eclipse nicht kompilieren, wohl aber mit bluej.

isses kompiliert, lässt sich das nicht ausführen.

da stimmt was nciht. dem werde ich jetzt auf den grund gehen.
 
Unter Eclipse: Windows -> Show View -> Problems
Dann klickst du auf dein Projekt, wird unter Problems ein Problem angezeigt? Vielleicht kompiliert Eclipse gar nicht, weil es ein Problem gibt...
 
ja bei clipse da kommt halt immer :

Description Resource Path Location Type
The constructor Socket(InetAddress, int) is undefined Portscanner.java /Netzwerk/src line 20 Java Problem


und bluej meckert gar nicht rum aber kanns nicht ausführen
 
The constructor Socket(InetAddress, int) is undefined Portscanner.java /Netzwerk/src line 20

Steht doch in der Fehlermeldung.
Der Konstruktor von java.net.Socket akzeptiert keine Argumente vom Typ InetAddress zusammen mit int, sondern eben nur String und int.

Probiers eben damit:

Java:
Socket s = new Socket(host.toString(), i);
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben