The document discusses Java UDP sockets and networking. It explains how to use the InetAddress class to represent IP addresses, and the DatagramSocket and DatagramPacket classes to send and receive UDP datagrams. It provides code examples for both client and server implementations of UDP networking in Java using these classes.
Java- Datagram Socket class & Datagram Packet classRuchi Maurya
Java DatagramSocket and DatagramPacket
Java DatagramSocket and DatagramPacket classes are used for connection-less socket programming.
________________________________________
Java DatagramSocket class
Java DatagramSocket class represents a connection-less socket for sending and receiving datagram packets.
A datagram is basically an information but there is no guarantee of its content, arrival or arrival time.
Commonly used Constructors of DatagramSocket class
o DatagramSocket() throws SocketEeption: it creates a datagram socket and binds it with the available Port Number on the localhost machine.
o DatagramSocket(int port) throws SocketEeption: it creates a datagram socket and binds it with the given Port Number.
o DatagramSocket(int port, InetAddress address) throws SocketEeption: it creates a datagram socket and binds it with the specified port number and host address.
________________________________________
Java DatagramPacket class
Java DatagramPacket is a message that can be sent or received. If you send multiple packet, it may arrive in any order. Additionally, packet delivery is not guaranteed.
Commonly used Constructors of DatagramPacket class
o DatagramPacket(byte[] barr, int length): it creates a datagram packet. This constructor is used to receive the packets.
o DatagramPacket(byte[] barr, int length, InetAddress address, int port): it creates a datagram packet. This constructor is used to send the packets.
Java- Datagram Socket class & Datagram Packet classRuchi Maurya
Java DatagramSocket and DatagramPacket
Java DatagramSocket and DatagramPacket classes are used for connection-less socket programming.
________________________________________
Java DatagramSocket class
Java DatagramSocket class represents a connection-less socket for sending and receiving datagram packets.
A datagram is basically an information but there is no guarantee of its content, arrival or arrival time.
Commonly used Constructors of DatagramSocket class
o DatagramSocket() throws SocketEeption: it creates a datagram socket and binds it with the available Port Number on the localhost machine.
o DatagramSocket(int port) throws SocketEeption: it creates a datagram socket and binds it with the given Port Number.
o DatagramSocket(int port, InetAddress address) throws SocketEeption: it creates a datagram socket and binds it with the specified port number and host address.
________________________________________
Java DatagramPacket class
Java DatagramPacket is a message that can be sent or received. If you send multiple packet, it may arrive in any order. Additionally, packet delivery is not guaranteed.
Commonly used Constructors of DatagramPacket class
o DatagramPacket(byte[] barr, int length): it creates a datagram packet. This constructor is used to receive the packets.
o DatagramPacket(byte[] barr, int length, InetAddress address, int port): it creates a datagram packet. This constructor is used to send the packets.
Poche chiacchiere e tanto codice per cercare rendere la nostra vita di
sviluppatori più divertente.
Parleremo di JAX-RS, le annotazioni, l'MVC che mette a disposizione e
l'integrazione di Jersey con Guice.
Useremo AOP per gestire log, transazioni e con l'aiuto di Infinispan
limiteremo le chamate concorrenti sul nostro cluster.
A fast Message-Queue base on zookeeper.
Jafka mq is a distributed publish-subscribe messaging system cloning from Apache Kafka.
So it has following features:
(1)Persistent messaging with O(1) disk structures that provide constant time performance even with many TB of stored messages.
(2)High-throughput: even with very modest hardware single broker can support hundreds of thousands of messages per second.
(3)Explicit support for partitioning messages over broker servers and distributing consumption over a cluster of consumer machines while maintaining per-partition ordering semantics.
(4)Simple message format for many language clients.
Writing Domain-Specific Languages for BeepBeepSylvain Hallé
This paper describes a plug-in extension of the BeepBeep 3 event stream processing engine. The extension allows one to write a custom grammar defining a particular specification language on event traces. A built-in interpreter can then convert expressions of the language into chains of BeepBeep processors through just a few lines of code, making it easy for users to create their own domain-specific languages.
In-depth caching in Varnish - GOG Varnish Meetup, march 2019GOG.com dev team
You think Varnish can cache responses by URL only? Not even close. Learn all different caching strategies available in Varnish, their benefits and consequences of usage. Learn how to and when to queue requests for the same endpoint, how to handle requests with conditional caching headers and how to have two levels of cache by tagging the responses.
Poche chiacchiere e tanto codice per cercare rendere la nostra vita di
sviluppatori più divertente.
Parleremo di JAX-RS, le annotazioni, l'MVC che mette a disposizione e
l'integrazione di Jersey con Guice.
Useremo AOP per gestire log, transazioni e con l'aiuto di Infinispan
limiteremo le chamate concorrenti sul nostro cluster.
A fast Message-Queue base on zookeeper.
Jafka mq is a distributed publish-subscribe messaging system cloning from Apache Kafka.
So it has following features:
(1)Persistent messaging with O(1) disk structures that provide constant time performance even with many TB of stored messages.
(2)High-throughput: even with very modest hardware single broker can support hundreds of thousands of messages per second.
(3)Explicit support for partitioning messages over broker servers and distributing consumption over a cluster of consumer machines while maintaining per-partition ordering semantics.
(4)Simple message format for many language clients.
Writing Domain-Specific Languages for BeepBeepSylvain Hallé
This paper describes a plug-in extension of the BeepBeep 3 event stream processing engine. The extension allows one to write a custom grammar defining a particular specification language on event traces. A built-in interpreter can then convert expressions of the language into chains of BeepBeep processors through just a few lines of code, making it easy for users to create their own domain-specific languages.
In-depth caching in Varnish - GOG Varnish Meetup, march 2019GOG.com dev team
You think Varnish can cache responses by URL only? Not even close. Learn all different caching strategies available in Varnish, their benefits and consequences of usage. Learn how to and when to queue requests for the same endpoint, how to handle requests with conditional caching headers and how to have two levels of cache by tagging the responses.
Optimal health products are supplements for health fitness . These products are integrative medicine to improve a dieter’s physical and emotional health. Call us 415-328-8356
Corporate Insight's 2013 Monitor Awards are out! The awards honor financial services firms for excellence in the online user experience they offer prospects, clients and advisors. Throughout the month of February, we will be releasing a series of slide decks highlighting the firms that received Gold Monitor Awards. Each of the nine slide decks will focus on a specific industry within financial services.
The Mutual Fund Monitor Awards recognize leading mutual fund issuers for excellence in the online user experience they offer their prospects and clients.
As their exam approaches, many students start feeling very nervous and tense. This presentation provides many practical tips to students about how they should prepare for their exams.
Annuity and Life Insurance Product Update - Q1 2015Corporate Insight
Our quarterly Annuity and Life Insurance Product Update reviews new products released by annuity carriers and life insurers over the last quarter. This slide deck reviews the new annuity and life insurance products introduced in the first quarter of 2015.
This is all about socket programming in java using TCP and UDP socket and an example of simple Echo Server.
Also includes concepts of the socket, Socket class and methods and use of those.
- Practical exercises on the use of UDP protocol using Java language
- References
YouTube link :
https://www.youtube.com/playlist?list=PLtDIUAtyP4lhV7CsYfLuIx26UeG4J-ujZ
GitHub :
https://github.com/Ghadeerof
Distributed systems
1.Write a program for implementing Client Server communication model.
2.Write a program to show the object communication using RMI.
3.Show the implementation of Remote Procedure Call.
4.Show the implementation of web services.
5.Write a program to execute any one mutual exclusion algorithm.
6.Write a program to implement any one election algorithm
7.Show the implementation of any one clock synchronization algorithm.
8.Write a program to implement two phase commit protocol
2. JAVA - Internet Addresses
java.net.InetAddress class
You get an address by using static methods:
Create InetAddress object representing the local machine
InetAddress myAddress = InetAddress.getLocalHost();
Create InetAddress object representing some remote machine
InetAddress ad = InetAddress.getByName(hostname);
3. JAVA - Printing Internet Addresses
You get information from an InetAddress
by using methods:
ad.getHostName();
ad.getHostAddress();
4. JAVA - The InetAddress Class
Handles Internet addresses both as host names and as IP addresses
Static Method getByName returns the IP address of a specified host name as
an InetAddress object
Methods for address/name conversion:
public static InetAddress getByName(String host) throws
UnknownHostException
public static InetAddress[] getAllByName(String host) throws
UnknownHostException
public static InetAddress getLocalHost() throws UnknownHostException
public boolean isMulticastAddress()
public String getHostName()
public byte[] getAddress()
public String getHostAddress()
public int hashCode()
public boolean equals(Object obj)
public String toString()
5. import java.net.*;
import java.io.*;
public class IPFinder
{
public static void main(String[] args) throws IOException
{
String host;
BufferedReader input =
new BufferedReader(
new InputStreamReader(System.in));
System.out.print("nnEnter host name: ");
host = input.readLine();
try
{
InetAddress address = InetAddress.getByName(host);
System.out.println("IP address: " + address.toString());
}
catch (UnknownHostException e)
{
System.out.println("Could not find " + host);
}
}
}
6. Retrieving the address of the local machine
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!");
}
}
}
7. The UDP classes
2 classes:
java.net.DatagramSocket class
is a connection to a port that does the sending and receiving. A
DatagramSocket can send to multiple, different addresses.The address
to which data goes is stored in the packet, not in the socket.
public DatagramSocket() throws SocketException
public DatagramSocket(int port) throws SocketException
public DatagramSocket(int port, InetAddress laddr) throws
SocketException
java.net.DatagramPacket class
is a wrapper for an array of bytes from which data will be sent or into
which data will be received. It also contains the address and port to
which the packet will be sent.
public DatagramPacket(byte[] data, int length)
public DatagramPacket(byte[] data, int length, InetAddress host, int
port)
8. Datagram Sockets
SERVER:
1. Create a DatagramSocket object
DatagramSocket dgramSocket =
new DatagramSocket(1234);
2. Create a buffer for incoming datagrams
byte[] buffer = new byte[256];
3. Create a DatagramPacket object for the incoming datagram
DatagramPacket inPacket =
new DatagramPacket(buffer, buffer.length);
4. Accept an incoming datagram
dgramSocket.receive(inPacket)
9. Datagram Sockets
SERVER:
5. Accept the sender’s address and port from the packet
InetAddress clientAddress = inPacket.getAddress();
int clientPort = inPacket.getPort();
6. Retrieve the data from the buffer
string message =
new String(inPacket.getData(), 0, inPacket.getLength());
7. Create the response datagram
DatagramPacket outPacket =
new DatagramPacket(
response.getBytes(), response.length(),
clientAddress, clientPort);
8. Send the response datagram
dgramSocket.send(outPacket)
9. Close the DatagramSocket: dgram.close();
10. Datagram Sockets
CLIENT:
1. Create a DatagramSocket object
DatagramSocket dgramSocket = new DatagramSocket;
2. Create the outgoing datagram
DatagramPacket outPacket = new DatagramPacket(
message.getBytes(),
message.length(),
host, port);
3. Send the datagram message
dgramSocket.send(outPacket)
4. Create a buffer for incoming datagrams
byte[] buffer = new byte[256];
11. Datagram Sockets
CLIENT:
5. Create a DatagramPacket object for the incoming datagram
DatagramPacket inPacket =
new DatagramPacket(buffer, buffer.length);
6. Accept an incoming datagram
dgramSocket.receive(inPacket)
7. Retrieve the data from the buffer
string response = new String(inPacket.getData(), 0,
inPacket.getLength());
8. Close the DatagramSocket:
dgram.close();
12. Sending UDP packets
When you receive a packet, the IP and
port number of the sender are set in the
DatagramPacket.
You can use the same packet to reply, by
overwriting the data, using the method:
packet.setData(newbuffer);
13. Non-blocking I/O receiving UDP
packets
You can set a time-out in milliseconds to
determine how long a read operation
blocks, before throwing an exception.
socket.setSoTimeout(duration);
If the duration given in milliseconds is
exceeded, an exception is thrown:
java.io.InterruptedException
14. References
Cisco Networking Academy Program (CCNA), Cisco Press.
CSCI-5273 : Computer Networks, Dirk Grunwald, University of Colorado-Boulder
CSCI-4220: Network Programming, Dave Hollinger, Rensselaer Polytechnic Institute.
TCP/IP Illustrated, Volume 1, Stevens.
Java Network Programming and Distributed Computing, Reilly & Reilly.
Computer Networks: A Systems Approach, Peterson & Davie.
http://www.firewall.cx
http://www.javasoft.com