Class ClientDatagram
=========================================================================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package udpdatagram;
/**
*
* @author MAHASISWA PU
*/
import java.net.*;
import java.io.*;
public class ClientDatagram {
public static DatagramSocket d;
public static DatagramPacket p;
public static byte buffer[] = new byte [1024];
public static BufferedReader dis;
public static int clientport = 800, serverport = 900;
public static void main(String args[]) throws IOException {
d= new DatagramSocket (clientport);
//BufferedReader dis = new BufferedReader (new InputStreamReader (System.in));
dis = new BufferedReader (new InputStreamReader (System.in));
p = new DatagramPacket (buffer,buffer.length);
//System.out.println("Client sedang menunggu server mengirimkan data...");
InetAddress i = InetAddress.getByName("localhost");
while (true)
{
//System.out.print("Masukkan pesan anda :");
//System.out.print("Masukkan pesan anda :");
//DatagramPacket p = new DatagramPacket (buffer,buffer.length);
//d.receive(p);
//System.out.print(p);
System.out.print("Masukkan pesan anda :");
String ps = new String (dis.readLine());
//System.out.println("Server : "+ps+"\n");
buffer = ps.getBytes();
//System.out.println("Server : ");
//System.out.println(ps);
//System.out.println("\n");
//String str = dis.readLine();
if((ps==null || ps.equals ("end")))
{
d.send (new DatagramPacket(buffer,ps.length(),i,serverport));
break;
}
//buffer =str.getBytes();
d.send (new DatagramPacket(buffer,ps.length(),i,serverport));
d.receive(p);
String ps2 = new String(p.getData(),0,p.getLength());
System.out.println("Pesan dari server : " +ps2+"\n");
}
}
}
========================================================================
Class ServerDatagram
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package udpdatagram;
/**
*
* @author MAHASISWA PU
*/
import java.net.*;
import java.io.*;
public class ServerDatagram {
public static DatagramSocket ds;
public static DatagramPacket p;
public static BufferedReader dis;
public static byte buffer[] = new byte [1024];
public static int clientport = 800, serverport = 900;
/**
* @param args the command line arguments
*/
//public static byte buffer[] = new byte [10];
public static void main(String[] args) throws IOException {
// TODO code application logic here
//byte buffer[] = new byte [100];
ds = new DatagramSocket (serverport);
p = new DatagramPacket (buffer,buffer.length);
dis = new BufferedReader (new InputStreamReader (System.in));
//BufferedReader dis = new BufferedReader (new InputStreamReader (System.in));
InetAddress i = InetAddress.getByName("localhost");
System.out.println("Menuggu pesan dari client,...");
while (true)
{
//byte buffer[] = new byte [65535];
//InetAddress i = InetAddress.getByName("localhost");
// System.out.print("Masukkan pesan anda :");
ds.receive(p);
//String str = dis.readLine();
String str = new String(p.getData(),0,p.getLength());
if((str==null || str.equals ("end")))
{
break;
}
//buffer =str.getBytes();
//ds.send (new DatagramPacket(buffer,str.length(),i,clientport));
//DatagramPacket p = new DatagramPacket (buffer,buffer.length);
//ds.receive(p);
System.out.println("Pesan dari client : " +str+"\n");
System.out.print("Masukkan pesan anda :");
String ps = new String (dis.readLine());
buffer =ps.getBytes();
//System.out.print("Masukkan pesan anda :");
ds.send(new DatagramPacket(buffer,ps.length(),i,clientport));
//String ps = new String (p.getData(),0,p.getLength());
//System.out.println("test");
//System.out.println("Client : ");
//System.out.println(ps);
//System.out.println("\n");
}
}
}
IT Information
Senin, 05 Mei 2014
Contoh FTP di Java
Class FTP Client
=========================================================================
import java.net.*;
import java.io.*;
import java.util.*;
class FTPClient
{
public static void main(String args[]) throws Exception
{
Socket soc=new Socket("127.0.0.1",5217);
transferfileClient t=new transferfileClient(soc);
t.displayMenu();
}
}
class transferfileClient
{
Socket ClientSoc;
DataInputStream din;
DataOutputStream dout;
BufferedReader br;
transferfileClient(Socket soc)
{
try
{
ClientSoc=soc;
din=new DataInputStream(ClientSoc.getInputStream());
dout=new DataOutputStream(ClientSoc.getOutputStream());
br=new BufferedReader(new InputStreamReader(System.in));
}
catch(Exception ex)
{
}
}
void SendFile() throws Exception
{
String filename;
System.out.print("Enter File Name :");
filename=br.readLine();
File f=new File(filename);
if(!f.exists())
{
System.out.println("File not Exists...");
dout.writeUTF("File not found");
return;
}
dout.writeUTF(filename);
String msgFromServer=din.readUTF();
if(msgFromServer.compareTo("File Already Exists")==0)
{
String Option;
System.out.println("File Already Exists. Want to OverWrite (Y/N) ?");
Option=br.readLine();
if(Option=="Y")
{
dout.writeUTF("Y");
}
else
{
dout.writeUTF("N");
return;
}
}
System.out.println("Sending File ...");
FileInputStream fin=new FileInputStream(f);
int ch;
do
{
ch=fin.read();
dout.writeUTF(String.valueOf(ch));
}
while(ch!=-1);
fin.close();
System.out.println(din.readUTF());
}
void ReceiveFile() throws Exception
{
String fileName;
System.out.print("Enter File Name :");
fileName=br.readLine();
dout.writeUTF(fileName);
String msgFromServer=din.readUTF();
if(msgFromServer.compareTo("File Not Found")==0)
{
System.out.println("File not found on Server ...");
return;
}
else if(msgFromServer.compareTo("READY")==0)
{
System.out.println("Receiving File ...");
File f=new File(fileName);
if(f.exists())
{
String Option;
System.out.println("File Already Exists. Want to OverWrite (Y/N) ?");
Option=br.readLine();
if(Option=="N")
{
dout.flush();
return;
}
}
FileOutputStream fout=new FileOutputStream(f);
int ch;
String temp;
do
{
temp=din.readUTF();
ch=Integer.parseInt(temp);
if(ch!=-1)
{
fout.write(ch);
}
}while(ch!=-1);
fout.close();
System.out.println(din.readUTF());
}
}
public void displayMenu() throws Exception
{
while(true)
{
System.out.println("[ MENU ]");
System.out.println("1. Send File");
System.out.println("2. Receive File");
System.out.println("3. Exit");
System.out.print("\nEnter Choice :");
int choice;
choice=Integer.parseInt(br.readLine());
if(choice==1)
{
dout.writeUTF("SEND");
SendFile();
}
else if(choice==2)
{
dout.writeUTF("GET");
ReceiveFile();
}
else
{
dout.writeUTF("DISCONNECT");
System.exit(1);
}
}
}
}
=========================================================================
Class FTP Server
package ftpserver;
import java.net.*;
import java.io.*;
import java.util.*;
public class FTPServer1
{
public static void main(String args[]) throws Exception
{
ServerSocket soc=new ServerSocket(5217);
System.out.println("FTP Server Started on Port Number 5217");
while(true)
{
System.out.println("Waiting for Connection ...");
transferfile t=new transferfile(soc.accept());
}
}
}
class transferfile1 extends Thread
{
Socket ClientSoc;
DataInputStream din;
DataOutputStream dout;
transferfile1(Socket soc)
{
try
{
ClientSoc=soc;
din=new DataInputStream(ClientSoc.getInputStream());
dout=new DataOutputStream(ClientSoc.getOutputStream());
System.out.println("FTP Client Connected ...");
start();
}
catch(Exception ex)
{
}
}
void SendFile() throws Exception
{
String filename=din.readUTF();
File f=new File(filename);
if(!f.exists())
{
dout.writeUTF("File Not Found");
return;
}
else
{
dout.writeUTF("READY");
FileInputStream fin=new FileInputStream(f);
int ch;
do
{
ch=fin.read();
dout.writeUTF(String.valueOf(ch));
}
while(ch!=-1);
fin.close();
dout.writeUTF("File Receive Successfully");
}
}
void ReceiveFile() throws Exception
{
String filename=din.readUTF();
if(filename.compareTo("File not found")==0)
{
return;
}
File f=new File(filename);
String option;
if(f.exists())
{
dout.writeUTF("File Already Exists");
option=din.readUTF();
}
else
{
dout.writeUTF("SendFile");
option="Y";
}
if(option.compareTo("Y")==0)
{
FileOutputStream fout=new FileOutputStream(f);
int ch;
String temp;
do
{
temp=din.readUTF();
ch=Integer.parseInt(temp);
if(ch!=-1)
{
fout.write(ch);
}
}while(ch!=-1);
fout.close();
dout.writeUTF("File Send Successfully");
}
else
{
return;
}
}
public void run()
{
while(true)
{
try
{
System.out.println("Waiting for Command ...");
String Command=din.readUTF();
if(Command.compareTo("GET")==0)
{
System.out.println("\tGET Command Received ...");
SendFile();
continue;
}
else if(Command.compareTo("SEND")==0)
{
System.out.println("\tSEND Command Receiced ...");
ReceiveFile();
continue;
}
else if(Command.compareTo("DISCONNECT")==0)
{
System.out.println("\tDisconnect Command Received ...");
System.exit(1);
}
}
catch(Exception ex)
{
}
}
}
}
=========================================================================
import java.net.*;
import java.io.*;
import java.util.*;
class FTPClient
{
public static void main(String args[]) throws Exception
{
Socket soc=new Socket("127.0.0.1",5217);
transferfileClient t=new transferfileClient(soc);
t.displayMenu();
}
}
class transferfileClient
{
Socket ClientSoc;
DataInputStream din;
DataOutputStream dout;
BufferedReader br;
transferfileClient(Socket soc)
{
try
{
ClientSoc=soc;
din=new DataInputStream(ClientSoc.getInputStream());
dout=new DataOutputStream(ClientSoc.getOutputStream());
br=new BufferedReader(new InputStreamReader(System.in));
}
catch(Exception ex)
{
}
}
void SendFile() throws Exception
{
String filename;
System.out.print("Enter File Name :");
filename=br.readLine();
File f=new File(filename);
if(!f.exists())
{
System.out.println("File not Exists...");
dout.writeUTF("File not found");
return;
}
dout.writeUTF(filename);
String msgFromServer=din.readUTF();
if(msgFromServer.compareTo("File Already Exists")==0)
{
String Option;
System.out.println("File Already Exists. Want to OverWrite (Y/N) ?");
Option=br.readLine();
if(Option=="Y")
{
dout.writeUTF("Y");
}
else
{
dout.writeUTF("N");
return;
}
}
System.out.println("Sending File ...");
FileInputStream fin=new FileInputStream(f);
int ch;
do
{
ch=fin.read();
dout.writeUTF(String.valueOf(ch));
}
while(ch!=-1);
fin.close();
System.out.println(din.readUTF());
}
void ReceiveFile() throws Exception
{
String fileName;
System.out.print("Enter File Name :");
fileName=br.readLine();
dout.writeUTF(fileName);
String msgFromServer=din.readUTF();
if(msgFromServer.compareTo("File Not Found")==0)
{
System.out.println("File not found on Server ...");
return;
}
else if(msgFromServer.compareTo("READY")==0)
{
System.out.println("Receiving File ...");
File f=new File(fileName);
if(f.exists())
{
String Option;
System.out.println("File Already Exists. Want to OverWrite (Y/N) ?");
Option=br.readLine();
if(Option=="N")
{
dout.flush();
return;
}
}
FileOutputStream fout=new FileOutputStream(f);
int ch;
String temp;
do
{
temp=din.readUTF();
ch=Integer.parseInt(temp);
if(ch!=-1)
{
fout.write(ch);
}
}while(ch!=-1);
fout.close();
System.out.println(din.readUTF());
}
}
public void displayMenu() throws Exception
{
while(true)
{
System.out.println("[ MENU ]");
System.out.println("1. Send File");
System.out.println("2. Receive File");
System.out.println("3. Exit");
System.out.print("\nEnter Choice :");
int choice;
choice=Integer.parseInt(br.readLine());
if(choice==1)
{
dout.writeUTF("SEND");
SendFile();
}
else if(choice==2)
{
dout.writeUTF("GET");
ReceiveFile();
}
else
{
dout.writeUTF("DISCONNECT");
System.exit(1);
}
}
}
}
=========================================================================
Class FTP Server
package ftpserver;
import java.net.*;
import java.io.*;
import java.util.*;
public class FTPServer1
{
public static void main(String args[]) throws Exception
{
ServerSocket soc=new ServerSocket(5217);
System.out.println("FTP Server Started on Port Number 5217");
while(true)
{
System.out.println("Waiting for Connection ...");
transferfile t=new transferfile(soc.accept());
}
}
}
class transferfile1 extends Thread
{
Socket ClientSoc;
DataInputStream din;
DataOutputStream dout;
transferfile1(Socket soc)
{
try
{
ClientSoc=soc;
din=new DataInputStream(ClientSoc.getInputStream());
dout=new DataOutputStream(ClientSoc.getOutputStream());
System.out.println("FTP Client Connected ...");
start();
}
catch(Exception ex)
{
}
}
void SendFile() throws Exception
{
String filename=din.readUTF();
File f=new File(filename);
if(!f.exists())
{
dout.writeUTF("File Not Found");
return;
}
else
{
dout.writeUTF("READY");
FileInputStream fin=new FileInputStream(f);
int ch;
do
{
ch=fin.read();
dout.writeUTF(String.valueOf(ch));
}
while(ch!=-1);
fin.close();
dout.writeUTF("File Receive Successfully");
}
}
void ReceiveFile() throws Exception
{
String filename=din.readUTF();
if(filename.compareTo("File not found")==0)
{
return;
}
File f=new File(filename);
String option;
if(f.exists())
{
dout.writeUTF("File Already Exists");
option=din.readUTF();
}
else
{
dout.writeUTF("SendFile");
option="Y";
}
if(option.compareTo("Y")==0)
{
FileOutputStream fout=new FileOutputStream(f);
int ch;
String temp;
do
{
temp=din.readUTF();
ch=Integer.parseInt(temp);
if(ch!=-1)
{
fout.write(ch);
}
}while(ch!=-1);
fout.close();
dout.writeUTF("File Send Successfully");
}
else
{
return;
}
}
public void run()
{
while(true)
{
try
{
System.out.println("Waiting for Command ...");
String Command=din.readUTF();
if(Command.compareTo("GET")==0)
{
System.out.println("\tGET Command Received ...");
SendFile();
continue;
}
else if(Command.compareTo("SEND")==0)
{
System.out.println("\tSEND Command Receiced ...");
ReceiveFile();
continue;
}
else if(Command.compareTo("DISCONNECT")==0)
{
System.out.println("\tDisconnect Command Received ...");
System.exit(1);
}
}
catch(Exception ex)
{
}
}
}
}
Contoh Socket TCP di Java
Class JavaSocketClient.
=========================================================================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javasocketserver;
/**
*
* @author Hakim
*/
import java.io.*;
import java.net.*;
public class JavaSocketClient {
public final static int REMOTE_PORT = 5000 ;
public static void main (String args[]) throws Exception {
Socket cl = null;
BufferedReader is = null;
DataOutputStream os = null;
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
String userInput = null;
String output = null;
// Membuka koneksi ke server menggunakan remote port
try {
cl = new Socket("LocalHost", REMOTE_PORT);
is = new BufferedReader(new InputStreamReader (cl.getInputStream()));
os = new DataOutputStream(cl.getOutputStream());
} catch (UnknownHostException e1) {
System.out.println("Unknown Host : " + e1);
} catch (IOException e2) {
System.out.println("Error io: " + e2);
}
//Menulis ke server
do {
System.out.print("Masukkan kata kunci: ");
userInput = stdin.readLine();
os.writeBytes(userInput + "\n");
//} catch (IOException ex) {
// System.out.println("Error writing to server..." + ex);
// }
//Menerima tanggapan dari server
try {
output = is.readLine();
System.out.println("Dari server : " + output);
} catch (IOException e) {
e.printStackTrace();
}
//close input stream, output stream dan koneksi
try {
is.close();
os.close();
cl.close();
while(true);
} catch (IOException x ) {
System.out.println("Error writing..." + x);
}
=========================================================================
Class JavaSocketServer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javasocketserver;
/**
*
* @author Hakim
*/
import java.io.*;
import java.net.*;
public class JavaSocketServer {
public final static int TESTPORT = 5000;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ServerSocket checkServer = null;
String line;
BufferedReader is = null ;
DataOutputStream os = null;
Socket clientSocket = null;
try {
checkServer = new ServerSocket(TESTPORT);
System.out.println("Applikasi server hidup");
} catch (IOException e){
System.out.println(e);
}
try {
clientSocket = checkServer.accept();
is = new BufferedReader ( new InputStreamReader(clientSocket.getInputStream()));
os = new DataOutputStream (clientSocket.getOutputStream());
} catch (Exception ei) {
ei.printStackTrace();
}
try {
line = is.readLine();
System.out.println("Terima : " + line);
if (line.compareTo("Horas") == 0) {
os.writeBytes("Horas juga");
} else {
os.writeBytes("Maaf... saya tidak mengerti");
}
}
catch (IOException e) {
System.out.println(e);
}
try {
os.close();
is.close();
clientSocket.close();
} catch (IOException ic) {
ic.printStackTrace();
}
}
}
=========================================================================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javasocketserver;
/**
*
* @author Hakim
*/
import java.io.*;
import java.net.*;
public class JavaSocketClient {
public final static int REMOTE_PORT = 5000 ;
public static void main (String args[]) throws Exception {
Socket cl = null;
BufferedReader is = null;
DataOutputStream os = null;
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
String userInput = null;
String output = null;
// Membuka koneksi ke server menggunakan remote port
try {
cl = new Socket("LocalHost", REMOTE_PORT);
is = new BufferedReader(new InputStreamReader (cl.getInputStream()));
os = new DataOutputStream(cl.getOutputStream());
} catch (UnknownHostException e1) {
System.out.println("Unknown Host : " + e1);
} catch (IOException e2) {
System.out.println("Error io: " + e2);
}
//Menulis ke server
do {
System.out.print("Masukkan kata kunci: ");
userInput = stdin.readLine();
os.writeBytes(userInput + "\n");
//} catch (IOException ex) {
// System.out.println("Error writing to server..." + ex);
// }
//Menerima tanggapan dari server
try {
output = is.readLine();
System.out.println("Dari server : " + output);
} catch (IOException e) {
e.printStackTrace();
}
//close input stream, output stream dan koneksi
try {
is.close();
os.close();
cl.close();
while(true);
} catch (IOException x ) {
System.out.println("Error writing..." + x);
}
=========================================================================
Class JavaSocketServer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javasocketserver;
/**
*
* @author Hakim
*/
import java.io.*;
import java.net.*;
public class JavaSocketServer {
public final static int TESTPORT = 5000;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ServerSocket checkServer = null;
String line;
BufferedReader is = null ;
DataOutputStream os = null;
Socket clientSocket = null;
try {
checkServer = new ServerSocket(TESTPORT);
System.out.println("Applikasi server hidup");
} catch (IOException e){
System.out.println(e);
}
try {
clientSocket = checkServer.accept();
is = new BufferedReader ( new InputStreamReader(clientSocket.getInputStream()));
os = new DataOutputStream (clientSocket.getOutputStream());
} catch (Exception ei) {
ei.printStackTrace();
}
try {
line = is.readLine();
System.out.println("Terima : " + line);
if (line.compareTo("Horas") == 0) {
os.writeBytes("Horas juga");
} else {
os.writeBytes("Maaf... saya tidak mengerti");
}
}
catch (IOException e) {
System.out.println(e);
}
try {
os.close();
is.close();
clientSocket.close();
} catch (IOException ic) {
ic.printStackTrace();
}
}
}
Source Code untuk menampilkan HostName di java
import java.net.*;
public class TampilkanHostName {
public static void main(String args[]) throws Exception {
InetAddress host = null;
host = InetAddress.getLocalHost();
System.out.println("Nama komputer anda : " + host.getHostName());
}
}
public class TampilkanHostName {
public static void main(String args[]) throws Exception {
InetAddress host = null;
host = InetAddress.getLocalHost();
System.out.println("Nama komputer anda : " + host.getHostName());
}
}
Source Code untuk melihat IPAddress di java
import java.net.*;
public class LihatIP {
public static void main(String[] args) throws Exception {
InetAddress host = null;
host = InetAddress.getLocalHost();
byte ip[] = host.getAddress();
for (int i=0; i<ip.length; i++){
if (i > 0) {
System.out.print(".");
}
System.out.print(ip[i] & 0xff);
}
System.out.println();
}
}
public class LihatIP {
public static void main(String[] args) throws Exception {
InetAddress host = null;
host = InetAddress.getLocalHost();
byte ip[] = host.getAddress();
for (int i=0; i<ip.length; i++){
if (i > 0) {
System.out.print(".");
}
System.out.print(ip[i] & 0xff);
}
System.out.println();
}
}
Jumat, 02 Mei 2014
Kamis, 30 Agustus 2012
Langganan:
Postingan (Atom)