Department of Computational Science
BSIT | BSCS | BSMath
IPT1
Java Socket
Programming
Jomarie S. Isorena
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.
• The Socket class is used to communicate client and server. Through this class, we can read and write
message.
• The ServerSocket class is used at server-side. The accept() method of ServerSocket class blocks the
console until the client is connected.
After the successful connection of client, it returns the instance of Socket at server-side.
Java Socket Programming
Client Side
1. Open Client
• The client initiates by creating a socket using the socket function. This socket will
serve as the endpoint for communication.
2. Connect
• The client then attempts to connect to the server using the connect function. This
sends a connection request to the server's listening socket.
3. Client/Server Session
• Write - The client writes data to the socket using the write function, sending
information to the server.
• Read - The client then reads data from the socket using the read function,
receiving a response from the server.
Server Side
1. Open Client
• The server begins by creating a socket using the socket function.
• It then binds the socket to an address and port using the bind function, specifying
where it will listen for incoming requests.
• Finally, it enters the listening state using the listen function, which allows it to
accept incoming connection requests.
2. Connection Request
• When a connection request is received from a client, the server accepts it using
Key Points the accept function. This establishes a connection and allows for communication
• Session Loop - During the session, the client and server between the client and server.
alternate between reading and writing, facilitating a two- 3. Client/Server Session
way communication. • Write - The server reads incoming data from the client.
• Connection Lifecycle - The sequence demonstrates a • Read - The server responds by writing data back to the client.
typical lifecycle of a connection, starting from the creation 4. End of File (EOF):
and binding of sockets to the final closure after data • Once communication is complete, the server closes the connection using the close
exchange. function.
Socket & ServerSocket
Class
Socket & ServerSocket
A socket is simply an endpoint for communications between the machines. The Socket class can be
used to create a socket.
Important methods
Method Description
1) public InputStream getInputStream() returns the InputStream attached with this socket.
returns the OutputStream attached with this
2) public OutputStream getOutputStream()
socket.
3) public synchronized void close() closes this socket
ServerSocket class
The ServerSocket class can be used to create a server socket. This object is used to establish
communication with the clients.
Important methods
Method Description
returns the socket and establish a connection
1) public Socket accept()
between server and client.
2) public synchronized void close() closes the server socket.
Example of Java
Socket Programming
Example of Java Socket Programming
Creating Server
• To create the server application, we need to create the instance of ServerSocket class. Here, we
are using 6666 port number for the communication between the client and server. You may also
choose any other port number. The accept() method waits for the client. If clients connects with
the given port number, it returns an instance of Socket.
Creating Client
• To create the client application, we need to create the instance of Socket class. Here, we need to
pass the IP address or hostname of the Server and a port number. Here, we are using "localhost"
because our server is running on same system.
Example of Java Socket Programming
File: MyServer.java File: MyClient.java
To execute this program open two command prompts and execute each program at each command
prompt as displayed in the below figure.
Example of Java Socket Programming
After running the client application, a message will be displayed on the server console.
File: MyServer.java File: MyClient.java
Example of Java
Socket Programming
(Read-Write both side)
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: File: MyClientReadWrite.java
MyServerReadWrite.java
Example of Java Socket Programming (Read-
Write both side)
File: File: MyClientReadWrite.java
MyServerReadWrite.java
Example of Java Socket Programming (Read-
Write both side)
File: File: MyClientReadWrite.java
MyServerReadWrite.java
Department of Computational Science
BSIT | BSCS | BSMath
Thank you!