Java Ağ Programlama
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
/*Bu program klavyeden alan adı isteyerek bu alan adının host edildiği bilgisayar üzerinde ilk 100 portu tarayarak açık
olup olmadığını tespit eder.Her tarama arasında 500ms bekler */
import java.net.*;
import java.util.*;
import java.awt.*;
import java.io.*;
public class PortScanner {
public PortScanner() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner klavye=new Scanner(System.in);
System.out.println(“Bir alan adı giriniz”);
String host=klavye.next();
//Sonra çözünüz:Bu kodun çalıştığını bilgisayar kendindeki
//açık portları nasıl tarar? Kodda ne gibi düzeltmeler yapmak lazım?
for(int i=0;i<100;i++)
{
try {
InetSocketAddress socketaddress=new InetSocketAddress(host,i);
//Belirtilen host üzerinde yine belirtilen porta göre Socket adresi döndürür
Socket s=new Socket();
s.connect(socketaddress, 500);
//Belirtilen soket adresine belirtilen ms süresine bağlanır
Toolkit.getDefaultToolkit().beep();
System.out.println(" Port"+i+"açık ve çalışıyor");
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(i+"nolu port hizmet vermiyor");
e.printStackTrace();
}
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import java.net.*;
import java.util.*;
public class IPFinderh {
public IPFinderh() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String host;
Scanner input=new Scanner(System.in);
System.out.println(“\n Enter host name:”);
host=input.next();
try
{
InetAddress address=InetAddress.getByName(host);
System.out.println(“IP address:”+address.toString());
}
catch(UnknownHostException uhEx){
System.out.println(host+”bulunamadı”);
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
//bu program new URL içerisindeki tüm bilgiyi text olarak çeker
package deneme;
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public URLConnectionReader() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws Exception{
URL site=new URL(“http://www.oracle.com/”);
URLConnection con=site.openConnection();
//belirtilen URL adresine bağlanır
BufferedReader buf=new BufferedReader(
new InputStreamReader(con.getInputStream()));
//Siteden gelen veriler 8 bitlik stream şeklinde olduğu için bufferlayarak bir diziye yerleştirmek daha sonra üzerinde
//işlem yapmayı mark gibi kolaylaştırır.
String inputline;
//Okunann stream boşluk olana kadar buffera almaya devam eder
while((inputline=buf.readLine())!=null)
System.out.println(inputline);
System.out.println(site.getHost());
buf.close();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import java.net.*;
import java.util.*;
import java.io.*;
public class URLParser {
public URLParser() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println(“Bir alan adı giriniz”);
Scanner klavye=new Scanner(System.in);
String host=klavye.next();
URL u=new URL(“http://”+host+”/”);//Çalışmayacak
URLConnection yc=u.openConnection();
System.out.println(“URL Adresi:”+u);
System.out.println(“\n Protocol=”+u.getProtocol());
System.out.println(“Host=”+u.getHost());
System.out.println(“Port=”+u.getPort());
System.out.println(“Ref=”+u.getRef());
System.out.println(“Authority=”+u.getAuthority());
System.out.println(“Path=”+u.getPath());
System.out.println(“Host=”+u.getQuery());
}
}
|
Leave a reply