Tuesday, November 7, 2017

SOCKET PROGRAMMING - UDP

DATAGRAM SOCKET AND PACKET
  • Java DatagramSocket and DatagramPacket classes are used for connection-less socket programming. 
Java Datagramsocket Class :
  • It is 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.
Constructors of DatagramSocket
Here are the all the constructors that are there in DatagramSocket class:
  •     DatagramSocket()
  •     DatagramSocket(DatagramSocket impl)
  •     DatagramSocket(int port)
  •     DatagramSocket(InetAddress laddr)
  •     DatagramSocket(SocketAddress bindAddr)
NOTE
  • DatagramSocket() : Creates a datagramSocket and binds it to any available port on local machine. If this constructor is used, the OS would assign any port to this socket. 
  • DatagramSocket(DatagramSocketImpl impl) : Creates an unbound datagram socket with given datagramImpl.  
  • DatagramSocket(int port) : Creates and binds the datagram socket to the specified port. The socket will be bound to the a wildcard address chosen by kernel. 
  • DatagramSocket(int port, InetAddress laddr) : Constructs a datagram socket and binds it to specified port and inetaddress.  
  • DatagramSocket(SocketAddress bindaddr) : Constructs a new socket object and binds it the specified socket address(IP address+port number). 
Methods
  •  bind() : Binds this socket to specified address and port number. 
  • connect() : Connects to the specified address and port.
  • disconnect() : Disconnects the socket. If the socket is not connected, then this method has no effect.  
  • isBound() : Returns a boolean value indicating whether this socket is bound or not. 
  • isConnected() : Returns boolean value representing connection state of the socket.  
  • getInetAddress() : Returns the address to which this socket is connected.
  • getPort() : Returns the port on the machine to which this socket is connected.  
  • getRemoteSocketAddress() : Returns the socket address (IP address+port number) to which this socket is connected. 
  • getLocalSocketAddress() : Returns the address of the machine this socket is bound to, i.e. local machine socket address.  
  • send() : Sends a datagram packet from this socket. 
  • receive() : It is used to receive the packet from a sender.
  • getLocalAddress() : Returns the local address to which this socket is bound.  
  • getLocalPort() : Returns the port on local machine to which this socket is bound. 
  • setSOTimeout() : This is used to set the waiting time for receiving a datagram packet.  
  • getSoTimeout() : Returns the timeout parameter if specified, or 0 which indicates infinite time. 
  • setSendBufferSize() : Used to set a limit to maximum size of the packet that can be sent from this socket.
  • getSendBufferSize() : Returns the value of SO_SNDBUF option of this socket.  
  • setReceiveBufferSize() : Used to set a limit to maximum size of the packet that is received on this socket.
  • getReceiveBufferSize() : Returns the value of SO_RCVBUF option of this socket.  
  • setReuseAddress() : Sometimes it may be necessary to bind multiple sockets to the same address.
  • getReuseAddress() : Returns boolean value indicating the setting of SO_REUSEADDR socket option.  
  • setBroadcast() : Sets the value of SO_BROADCAST socket option. 
  • getBroadcast() : Returns true if broadcast is enabled, false otherwise.  
  • setTrafficClass() : used to set the type-of-service octet in the IP datagram header for datagrams sent from this DatagramSocket. 
  • getTrafficClass() : Gets traffic class or type of service from the IP header of packets sent from this socket.  
  • close() : Closes this datagram socket. Any pending receive call will throw SocketException
  • isClosed() : Returns the boolean value indicating if the socket is closed or not.
  • getChannel() : Returns a data channel if any associated with this socket.  
  • setDatagramSocketImplFactory() : Sets the datagram socket implementation factory for the application.
Java DatagramPacket
  • It 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.
Constructors of DatagramPacket
The following are the constructors of DatagramPacket 
  • DatagramPacket(byte[] buf, int length)
  • DatagramPacket(byte[] buf, int length, InetAddress address, int port)
  • DatagramPacket(byte[] buf, int offset, int length)
  • DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)
  • DatagramPacket(byte[] buf, int offset, int length, SocektAddress address)
  • DatagramPacket(byte[] buf, int length, SocektAddress address)
NOTE
  • buf : This is the actual message that is to be delivered or received. Datagram packets receive or send data in stuffed in a byte array. If this is used in constructor for sending the message, than this represents the message to be delivered, when used for receiving purpose, it represents the buffer where the received message will be stored.
  • offset : It represents the offset into the buffer array.
  • length : It is the actual size of packet to receive. This must be less than or equal to the size of buffer array or else there will be overflow as received message wont fit into the array.
  • InetAddress address : This is the destination to which the message is to be delivered.
  • port : This is the port to which the message will be delivered.
  • SocketAddress address : The information about the address and port can be represented wih the help of socket address. Same function as the above two combined.
Methods
  • getAddress() : Returns the IP address to which this packet is sent to or from which it was received. 
  • getPort() : Returns the port to which this packet is sent to or from which it was received. This method is specifically used on the server from getting the port of the client who sent the request.   
  • getData() : Returns the data contained in this packet as a byte array. The data starts from the offset specified and is of length specified. 
  • getOffset() : Returns the offset specified.  
  • getLength() : Returns the length of the data to send or receive 
  • setData() : Used to set the data of this packet.  
  • setAddress() : Used to set the address to which this packet is sent. 
  • setPort() : Set the port on which destination will receive this packet.  
  • setSocketAddress() : Used to set the socket address of the destination(IP address+ port number). 
  • getSocketAddress() : Returns the socket address of this packet. In case it was received, return the socket address of the host machine.  
  • setLength() : Used to set the length for this packet. 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.