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");
}
}
}
Tidak ada komentar:
Posting Komentar