0% found this document useful (0 votes)
38 views3 pages

Modul - Socket UDP

The document describes a UDP client-server application for sending and receiving messages. 1. The UDP server opens a port to listen for incoming datagram packets from clients. It receives messages, counts the messages, and sends the messages back to the client. 2. The UDP client connects to the server, allows the user to enter messages, sends those messages to the server as datagram packets, receives the responses from the server, and displays them to the user. The connection closes when the user enters "**CLOSE**". 3. Both the client and server create datagram sockets and packets to send and receive short messages to and from each other's ports.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Modul - Socket UDP

The document describes a UDP client-server application for sending and receiving messages. 1. The UDP server opens a port to listen for incoming datagram packets from clients. It receives messages, counts the messages, and sends the messages back to the client. 2. The UDP client connects to the server, allows the user to enter messages, sends those messages to the server as datagram packets, receives the responses from the server, and displays them to the user. The connection closes when the user enters "**CLOSE**". 3. Both the client and server create datagram sockets and packets to send and receive short messages to and from each other's ports.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Socket UDP
a) Server UDP
Server akan menerima pesan dari client, menghitung pesan yang masuk, kemudian
mengirimkan kembali pesan kembali ke client.

import java.net.*;
import java.io.*;

public class UDPEchoServer {


private static final int PORT = 1234;
private static DatagramSocket dtgramSocket;
private static DatagramPacket inPacket,outPacket;
private static byte[] buffer;

public static void main (String [] args){

System.out.println("Membuka Port...\n");
try {
dtgramSocket = new DatagramSocket(PORT); //Langkah 1

}catch(SocketException sockEx)
{
System.out.println("Tidak bisa terhubung ke port!");
System.exit(1);
}
handleClient();
}

private static void handleClient ()


{
try
{
String messageIn,messageOut;
int numMessages=0;
InetAddress clientAddress=null;
int clientPort;
do
{
buffer = new byte [256]; //Langkah 2
inPacket = new DatagramPacket(buffer, buffer.length);
//Langkah 3
dtgramSocket.receive(inPacket); //Langkah 4
clientAddress = inPacket.getAddress(); //Langkah 5
clientPort = inPacket.getPort(); //Langkah 5
messageIn = new
String(inPacket.getData(),0,inPacket.getLength()); //Langkah 6

System.out.println("Pesan diterima");
numMessages++;
messageOut= "Pesan " + numMessages + ": " + messageIn;
outPacket= new
DatagramPacket(messageOut.getBytes(),messageOut.length(),clientAddress,clientPort);
//Langkah 7
dtgramSocket.send(outPacket); //Langkah 8
}while (true);
}catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
System.out.println("\n* Tutup Koneksi...*");
dtgramSocket.close(); //Langkah 9
}

}
}

b) Client UDP

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class UDPEchoClient {

private static InetAddress host;


private static final int PORT = 1234;
private static DatagramSocket dtgramSocket;
private static DatagramPacket inPacket,outPacket;
private static byte[] buffer;

public static void main (String [] args)


{
try
{
host = InetAddress.getLocalHost();
}
catch (UnknownHostException uhEx)
{
System.out.println("ID host tidak ditemukan");
System.exit(1);
}
accessServer();
}

private static void accessServer()


{
try
{
//Langkah 1
dtgramSocket= new DatagramSocket();
//membuat stream untuk input dari keyboard
Scanner inputUser = new Scanner(System.in);
String pesan ="", respons = "";
do
{
System.out.print("Masukkan pesan :");
pesan = inputUser.nextLine();
if (!pesan.equals("**CLOSE**"))
{
outPacket = new
DatagramPacket(pesan.getBytes(),pesan.length(),host,PORT); //Langkah 2
dtgramSocket.send(outPacket); //Langkah 3
buffer = new byte[256];
//Langkah 4
inPacket = new
DatagramPacket(buffer,buffer.length); //Langkah 5
dtgramSocket.receive(inPacket);
//Langkah 6
respons = new
String(inPacket.getData(),0,inPacket.getLength()); //Langkah 7

System.out.println("\nServer> "+respons);
}
}while (!pesan.equals("**CLOSE**"));
}
catch (IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
System.out.println("\n* Tutup Koneksi..*");
dtgramSocket.close(); //Langkah 8
}
}
}

You might also like