Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Nama : Yoga Putra Utama

NPM : 18.1.03.02.0055

Kelas : 3D

Server
import java.io.IOException;
import java.net.*;

public class server {


public static void main (String args[]) throws SocketException,
UnknownHostException, IOException
{
DatagramSocket ds = new DatagramSocket(1000);
//kirim
String pesan="Yoga Putra Utama 0055";
DatagramPacket out = new DatagramPacket(pesan.getBytes(), pesan.length(),
InetAddress.getLocalHost(), 1234);
ds.send(out);
ds.close();

}
}

Client

import java.io.IOException;

import java.net.*;

public class client {

public static void main(String args[]) throws IOException

try (DatagramSocket dc = new DatagramSocket(1234)) {

//terima

byte[] buff = new byte[256];

DatagramPacket in = new DatagramPacket(buff, buff.length);


dc.receive(in);

String terima = new String(in.getData(),0,in.getLength());

System.out.println(terima);

Outputnya:

You might also like