sedikit nambahin thred yang sebelumnya, Ini topik untuk berbagi source java tentang jaringan (pemrograman socket) Langsung aja gue kasih sourcenya nama file IPFinder.java
Gunanya untuk mengetahui no IP website..keren coy (bagi yang belom tau, kalo uda tau ya basi deh).
Gunanya untuk mengetahui no IP website..keren coy (bagi yang belom tau, kalo uda tau ya basi deh).
Code:
import java.net.*;
import java.io.*;
public class IPFinder {
public static void main(String args[]) {
String host="";
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n\nEnter host name: ");
try {
host=input.readLine();
} catch (Exception e) {
}
try {
InetAddress address=InetAddress.getByName(host);
System.out.println("IP Address: "+address);
} catch (UnknownHostException ex) {
System.out.println("Could not find "+host);
}
}
}
Ada juga untuk liat IP sendiri nama file MyLocalIPAddress.java
Code:
import java.net.*;
public class MyLocalIPAddress {
public static void main(String args[]) {
try {
InetAddress address=InetAddress.getLocalHost();
System.out.println(address);
} catch (UnknownHostException e) {
System.out.println("Could not find local address.");
}
}
}
Kalo temen2 pengen test port apa yang terbuka , gue juga ada sourcenya nama file TCPEchoServer.java contoh port yang ditest 1234
Code:
import java.io.*;
import java.net.*;
public class TCPEchoServer {
private static ServerSocket servSock;
private static final int PORT=1234;
public static void main(String args[]) {
System.out.println("Opening PORT...\n");
try {
servSock=new ServerSocket(PORT);
} catch (IOException e) {
System.out.println("Unable to attach port!");
}
do {
run();
} while(true);
}
private static void run() {
Socket link=null;
try {
link=servSock.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(link.getOutputStream(),true);
int numMessages=0;
String message=in.readLine();
while (!message.equals("***CLOSE***")) {
System.out.println("Message reseived");
numMessages++;
System.out.println("Message "+numMessages+" : "+message);
message=in.readLine();
}
System.out.println(numMessages+" message received");
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
System.out.println("\n* Closing connection... *");
link.close();
} catch (IOException e) {
System.out.println("Unable to disconnect!");
System.exit(0);
}
}
}
}
Untuk liat jam diserver gunakan source berikut nama file DayTimeServer.java
Code:
import java.net.*;
import java.io.*;
import java.util.Date;
public class DayTimeServer {
public static void main(String args[]) {
ServerSocket server;
final int DAYTIME_PORT=13;
Socket socket;
try {
server=new ServerSocket(DAYTIME_PORT);
do {
socket=server.accept();
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
Date date=new Date();
out.println(date);
socket.close();
} while(true);
} catch (Exception e) {
}
}
}
No comments:
Post a Comment
thank you for commenting on our blogs