Socket Programming and Java


Socket Programming in Java

Java Socket programming is used for communication between applications running on different machines. A socket is an endpoint for communications and is created using the IP Address of the machine and a port number. Communication over the socket takes place via a protocol.

1.  Internet Protocol (IP) is a low-level routing protocol that breaks data into small packets and sends them to an address across a network.
2.   Transmission Control Protocol (TCP) is a connection oriented higher-level protocol that establishes a connection between the client and server machines and reliably transmits the data. Socket and ServerSocket classes are used for connection-oriented socket programming
3.    User Datagram Protocol (UDP) is connectionless higher-level protocol that is used to support fast but unreliable transport of packets. DatagramSocket and DatagramPacket classes are used for connection-less socket programming

TCP/IP Sockets
TCP/IP sockets are used to implement reliable, bidirectional, persistent, point-to-point, stream-based connections between hosts on the Internet. A socket can be used to connect Java’s I/O system to other programs that may reside either on the local machine or on any other machine on the Internet. There are two kinds of TCP sockets in Java. – Client sockets and Server sockets.

Client Sockets
The Socket class can be used to create client sockets. It is designed to connect to server sockets and initiate protocol exchanges.

Creating Client sockets –
§  Socket(InetAddress ipAddress, int port) throws IOException
o   Creates a socket using a preexisting InetAddress object and a port

Important methods of Socket class –
§  public InputStream getInputStream()
o   returns the InputStream attached with this socket
§  public OutputStream getOutputStream()
o   returns the OutputStream attached with this socket.
§  public synchronized void close()
o   closes this socket

Server Sockets
The ServerSocket class can be used to create a server socket. This object is designed to be a “listener”. It waits for connection requests from client sockets and establish communication with the client machines.

Creating Server sockets –
§  ServerSocket(int port) throws IOException
o   Creates server socket on the specified port with a default queue length of 50.
§  ServerSocket(int port, int maxQueue) throws IOException
o   Creates a server socket on the specified port with a maximum queue length of maxQueue.

Important methods of ServerSocket class –
§  public Socket accept() 
o   returns the socket and establish a connection between server and client.
§  public synchronized void close()
o   closes the server socket.

Datagrams
Datagrams are bundles of information passed between machines. Java implements datagrams on top of the UDP protocol by using two classes:
§  DatagramPacket – data container
§  DatagramSocket – mechanism to send or receive the DatagramPackets.

Creating DatagramSocket
§  DatagramSocket( ) throws SocketException
§  DatagramSocket(int port) throws SocketException
§  DatagramSocket(int port, InetAddress ipAddress) throws SocketException
§  DatagramSocket(SocketAddress address) throws SocketException

Important Methods of DatagramSocket class
§  void send(DatagramPacket packet) throws IOException
§  void receive(DatagramPacket packet) throws IOException

Creating DatagramPacket
§  DatagramPacket(byte data[ ], int size)
§  DatagramPacket(byte data[ ], int offset, int size)
§  DatagramPacket(byte data[ ], int size, InetAddress ipAddress, int port)
§  DatagramPacket(byte data[ ], int offset, int size, InetAddress ipAddress, int port)

Important Methods of DatagramPacket class
§  InetAddress getAddress( )
o   Returns the address of the source or destination (for datagrams being received / sent).
§  void setAddress(InetAddress ipAddress)
o   Sets the address to which a packet will be sent.
§  byte[ ] getData( )
o   Returns the byte array of data contained in the datagram. Mostly used to retrieve data from the datagram after it has been received.
§  void setData(byte[ ] data)
§  void setData(byte[ ] data, int idx, int size)
o   Sets the data to data, the offset to idx, and the length to size. If not specified, default offset is zero, and the length is the number of bytes in data.
§  int getLength( )
o   Returns the length of the valid data contained in the byte array that would be returned from the
§  int getOffset( )
o   Returns the starting index of the data.
§  int getPort( )
o   Returns the port number.


Programming Examples

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu