qwertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfgh
jklzxcvbnmqwertyuiopasdfghjklzxcvb
nmqwertyuiopasdfghjklzxcvbnmqwer
Java Socket Programming
tyuiopasdfghjklzxcvbnmqwertyuiopas
dfghjklzxcvbnmqwertyuiopasdfghjklzx
cvbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyuio
pasdfghjklzxcvbnmqwertyuiopasdfghj
klzxcvbnmqwertyuiopasdfghjklzxcvbn
mqwertyuiopasdfghjklzxcvbnmqwerty
uiopasdfghjklzxcvbnmqwertyuiopasdf
ghjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmrty
uiopasdfghjklzxcvbnmqwertyuiopasdf
ghjklzxcvbnmqwertyuiopasdfghjklzxc
Table of Contents
1. Java Socket Programming................................................................................................................2
1.1. ServerSocket Class Methods:...................................................................................................3
1.2. Socket Class Methods:.............................................................................................................5
1.3. InetAddress Class Methods:.....................................................................................................8
1.4. Example of Java Socket Programming.....................................................................................8
1.5. Example of Java Socket Programming (Read-Write both side)................................................9
2. Java DatagramSocket and DatagramPacket...................................................................................11
2.1. Java DatagramSocket class....................................................................................................11
2.2. Java DatagramPacket class....................................................................................................11
2.3. Example of Sending DatagramPacket by DatagramSocket....................................................11
2.4. Example of Receiving DatagramPacket by DatagramSocket..................................................12
1. Java Socket Programming
Java Socket programming is used for communication between the applications running on different JRE.
Java Socket programming can be connection-oriented or connection-less. Socket and ServerSocket
classes are used for connection-oriented socket programming and DatagramSocket and DatagramPacket
classes are used for connection-less socket programming.
The client in socket programming must know two information:
1. IP Address of Server, and
2. Port number.
Sockets provide the communication mechanism between two computers using TCP. A client program
creates a socket on its end of the communication and attempts to connect that socket to a server. When the
connection is made, the server creates a socket object on its end of the communication. The client and
server can now communicate by writing to and reading from the socket. The [Link] class
represents a socket, and the [Link] class provides a mechanism for the server program to
listen for clients and establish connections with them.
The following steps occur when establishing a TCP connection between two computers using sockets:
The server instantiates a ServerSocket object, denoting which port number communication is to
occur on.
The server invokes the accept() method of the ServerSocket class. This method waits until a
client connects to the server on the given port.
After the server is waiting, a client instantiates a Socket object, specifying the server name and
port number to connect to.
The constructor of the Socket class attempts to connect the client to the specified server and port
number. If communication is established, the client now has a Socket object capable of
communicating with the server.
On the server side, the accept() method returns a reference to a new socket on the server that is
connected to the client's socket.
After the connections are established, communication can occur using I/O streams. Each socket has both
an OutputStream and an InputStream. The client's OutputStream is connected to the server's InputStream,
and the client's InputStream is connected to the server's OutputStream.
TCP is a two-way communication protocol, so data can be sent across both streams at the same time.
There are following useful classes providing complete set of methods to implement sockets.
1.1. ServerSocket Class Methods:
The [Link] class is used by server applications to obtain a port and listen for client
requests
The ServerSocket class has four constructors:
If the ServerSocket constructor does not throw an exception, it means that your application has
successfully bound to the specified port and is ready for client requests.
Here are some of the common methods of the ServerSocket class:
When the ServerSocket invokes accept(), the method does not return until a client connects. After a client
does connect, the ServerSocket creates a new Socket on an unspecified port and returns a reference to this
new Socket. A TCP connection now exists between the client and server, and communication can begin.
1.2. Socket Class Methods
The [Link] class represents the socket that both the client and server use to communicate with
each other. The client obtains a Socket object by instantiating one, whereas the server obtains a Socket
object from the return value of the accept() method.
The Socket class has five constructors that a client uses to connect to a server
When the Socket constructor returns, it does not simply instantiate a Socket object but it actually
attempts to connect to the specified server and port.
Some methods of interest in the Socket class are listed here. Notice that both the client and server have a
Socket object, so these methods can be invoked by both the client and server.
1.3. InetAddress Class Methods:
This class represents an Internet Protocol (IP) address. Here are following useful methods which you
would need while doing socket programming:
1.4. Example of Java Socket Programming
Let's see a simple of java socket programming in which client sends a text and server receives it.
File: [Link]
File: [Link]
1.5. Example of Java Socket Programming (Read-Write both side)
In this example, client will write first to the server then server will receive and print the text. Then server
will write to the client and client will receive and print the text. The step goes on.
File: [Link]
File: [Link]
2. Java DatagramSocket and DatagramPacket
Java DatagramSocket and DatagramPacket classes are used for connection-less socket programming.
2.1. Java DatagramSocket class
Java DatagramSocket class represents a connection-less socket for sending and receiving datagram
packets. A datagram is basically information but there is no guarantee of its content, arrival or arrival
time.
2.1.1. Commonly used Constructors of DatagramSocket class
DatagramSocket() throws SocketException: it creates a datagram socket and binds it with the
available Port Number on the localhost machine.
DatagramSocket(int port) throws SocketException: it creates a datagram socket and binds it
with the given Port Number.
DatagramSocket(int port, InetAddress address) throws SocketException: it creates a
datagram socket and binds it with the specified port number and host address.
2.2. Java DatagramPacket class
Java DatagramPacket is a message that can be sent or received. If you send multiple packets, it may
arrive in any order. Additionally, packet delivery is not guaranteed.
2.2.1. Commonly used Constructors of DatagramPacket class
DatagramPacket (byte[] barr, int length): it creates a datagram packet. This constructor is used
to receive the packets.
DatagramPacket (byte[] barr, int length, InetAddress address, int port): it creates a datagram
packet. This constructor is used to send the packets.
2.3. Example of Sending DatagramPacket by DatagramSocket
2.4. Example of Receiving DatagramPacket by DatagramSocket
3. Lab Practice:
1. File Transfer through Socket
2.