Tutorial Week03 Socket Programming 2025
Tutorial Week03 Socket Programming 2025
2 Client-Server Architectures
Socket programming establishes network connections, sends and receives data over a
network, and communicates between devices or processes. It is based on the concept
of sockets, which are endpoints for communication. Sockets can be either connection-
oriented or connection-less, depending on the protocol used.
out.println("message");
Close Socket
socket.close();
Overview: In this exercise, you will be creating two separate classes - Client and Server.
The Client class will connect to the Server class using a specific port. Once the
connection is established, the Client will send a message to the Server, which will then
respond back to the Client.
Key Concepts:
• Input/Output Streams: These are used for communication between the client
and server. You will use the InputStreamReader and BufferedReader classes for
reading data, and the PrintWriter class for writing data.
• Exception Handling: Network operations can fail for various reasons, so it’s
important to handle exceptions properly in your code. You will use a try-catch
block to catch any IOException that may occur.
Expected Outcome: By the end of this exercise, you should be able to create a simple
client-server application in . You will gain a basic understanding of network
programming, and be prepared to explore more complex topics like multi-threading and
non-blocking I/O.
STEPS:
o Locate the downloaded zip file on your computer. It’s usually in the
‘Downloads’ folder unless you chose a different location. You may also you
the ZIP file to import it directly to the NetBeans without requiring extracting
the ZIP file.
o In NetBeans, click on ‘File’ in the menu bar, then select ‘Open Project’.
o In the file chooser window, navigate to the location where you extracted the
project files.
o You should see a folder with the same name as the zip file. Click on it to
select it.
o The project should now be visible in the ‘Projects’ tab on the left side of the
IDE.
o Now, you can start implementing your code under each comment in the
provided classes.
1. Streams: In Java, a stream is a sequence of data. You can think of it like a flow of
information that you can read from or write to. Streams are a fundamental way to
handle input and output (I/O) operations.
2. byte[] (Byte Array): A byte array is simply an array that holds a sequence of bytes. In
Java, a byte is an 8-bit signed integer. Byte arrays are often used to store binary data
(like the raw content of a file, image data, etc.) that you read from or write to streams.
INPUTSTREAM
• Purpose: An abstract class representing an input stream of bytes. It's the superclass
of all classes representing an input stream. Think of it as a source from which you can
read a sequence of bytes.
• Key Methods:
o read(): Reads the next byte of data from the input stream.
o read(byte[] b): Reads a number of bytes from the input stream and stores them
into the buffer array b.
o read(byte[] b, int off, int len): Reads up to len bytes of data from the input stream
into an array of bytes, starting at offset off.
o close(): Closes the input stream and releases any system resources
associated with it.
OUTPUTSTREAM
• Key Methods:
• write(byte[] b): Writes b.length bytes from the specified byte array to this output
stream.
• write(byte[] b, int off, int len): Writes len bytes from the specified byte array starting
at offset off to this output stream.
• flush(): Flushes this output stream and forces any buffered output bytes to be
written out.
• close(): Closes this output stream and releases any system resources associated
with the stream.
• Purpose: An array to hold a sequence of bytes. It's a fundamental data structure for
representing raw binary data in Java.
• Declaration: byte[] myByteArray;
• Initialization:
• byte[] myByteArray = new byte[10]; // Creates an array of 10 bytes, initialized to 0.
• byte[] myByteArray = {10, 20, 30, 40, 50}; // Initializes with specific byte values.
Type Abstract class (base for byte Concrete class (for formatted text output)
output streams)
Data Bytes (byte, byte[]) Characters, formatted strings, various data types
(converted to text)
Key write() (bytes), flush(), close() print(), println(), printf(), format(), append(),
Methods flush(), close()
Typical Binary files, network streams, raw Text files, console output, formatted reports
Usage data output
EXERCISE 2: BUILDING A SIMPLE CHAT APPLICATION IN
Overview: In this exercise, you will be creating two separate classes - SimpleChatClient
and SimpleChatServer. The SimpleChatClient class will connect to the
SimpleChatServer class using a specific port. Once the connection is established, the
SimpleChatClient will send a message to the SimpleChatServer, which will then respond
back to the SimpleChatClient.
Key Concepts:
• Input/Output Streams: These are used for communication between the client
and server. You will use the InputStream and OutputStream classes for reading
data and writing data.
• Exception Handling: Network operations can fail for various reasons, so it’s
important to handle exceptions properly in your code. You will use a try-catch
block to catch any IOException that may occur.
Expected Outcome: By the end of this exercise, you should be able to create a simple
chat application in . You will gain a basic understanding of network programming, and be
prepared to explore more complex topics like multi-threading and non-blocking I/O.
STEPS:
a. Locate the downloaded zip file on your computer. It’s usually in the
‘Downloads’ folder unless you chose a different location. You may also you
the ZIP file to import it directly to the NetBeans without requiring extracting
the ZIP file.
a. In NetBeans, click on ‘File’ in the menu bar, then select ‘Open Project’.
b. In the file chooser window, navigate to the location where you extracted the
project files.
c. You should see a folder with the same name as the zip file. Click on it to
select it.
a. The project should now be visible in the ‘Projects’ tab on the left side of the
IDE.
a. Now, you can start implementing your code under each comment in the
provided classes.