Skip to main content

Posts

Socket (TCP & UDP) communication in Java

Socket communication in Java enables communication between two endpoints over a network. There are two main types of sockets: TCP sockets and UDP sockets. Let's explain both types with examples: TCP Socket Communication: 1. **Server Side**:    - The server creates a `ServerSocket` object to listen for incoming connections on a specific port.    - When a client connects, the server accepts the connection and creates a `Socket` object to communicate with the client.    - The server reads from and writes to the socket's input and output streams to communicate with the client. import java.io.*; import java.net.*; public class TCPServer {     public static void main(String[] args) throws IOException {         ServerSocket serverSocket = new ServerSocket(12345);         System.out.println("Server started. Waiting for client...");         Socket clientSocket = serverSocket.accept();         System.out.println("Client connected.");         BufferedReader in = new Bu

Scanning and Formatting

 Here are some notes on scanning and formatting in Java I/O with examples: 1. Scanning (Input):    - Scanning involves reading input from different sources like the keyboard, files, or network connections.    - Java provides the `Scanner` class in the `java.util` package to facilitate scanning.    Example:    import java.util.Scanner;    public class ScannerExample {        public static void main(String[] args) {            Scanner scanner = new Scanner(System.in);            System.out.print("Enter your name: ");            String name = scanner.nextLine();            System.out.println("Hello, " + name + "!");            scanner.close();        }    } 2. Formatting (Output):    - Formatting is the process of presenting data in a specific way, such as aligning text, setting precision for floating-point numbers, etc.    - The `System.out.printf()` method is commonly used for formatted output in Java I/O.    Example:    public class FormattingExample {    

Buffered Streams

Buffered Streams in Java: Buffered streams are used to improve the performance of input/output operations by reducing the number of system calls. They achieve this by using an internal buffer to read from or write to the underlying input/output stream in larger chunks, rather than one byte or character at a time. Buffered Input Stream Classes: - `BufferedInputStream`: Provides buffering for input bytes, allowing the reading of data from an underlying input stream. - `BufferedReader`: Reads text from a character-input stream, buffering characters to provide efficient reading of characters, arrays, and lines. Buffered Output Stream Classes: - `BufferedOutputStream`: Provides buffering for output bytes, allowing the writing of data to an underlying output stream. - `BufferedWriter`: Writes text to a character-output stream, buffering characters to provide efficient writing of characters, arrays, and lines. Example: Reading from a File using BufferedReader: import java.io.BufferedReader; i

Character Streams

Character streams in Java are used for handling input and output of character data, making them suitable for text-based operations where character encoding matters. Unlike byte streams, which deal with raw binary data, character streams handle characters and automatically handle character encoding and decoding. Let's explore character streams with an example: Example: Reading and Writing Text Files Using Character Streams In this example, we'll create a program that reads the contents of a text file using a `FileReader` and writes them into another text file using a `FileWriter`. import java.io.*; public class CharacterStreamExample {     public static void main(String[] args) {         String sourceFile = "source.txt";         String destinationFile = "destination.txt";         try (FileReader reader = new FileReader(sourceFile);              FileWriter writer = new FileWriter(destinationFile)) {             int character;             while ((character = re

Byte Streams

 Java Byte Streams Byte streams in Java are used to handle input and output of raw binary data. They are suitable for handling low-level data such as images, audio, and binary files. Let's dive into an example to illustrate how byte streams work: Example: Copying a File Using Byte Streams In this example, we'll create a program that reads the contents of a source file and writes them into a destination file using byte streams. import java.io.*; public class ByteStreamExample {     public static void main(String[] args) {         String sourceFile = "source.txt";         String destinationFile = "destination.txt";         try (FileInputStream fis = new FileInputStream(sourceFile);              FileOutputStream fos = new FileOutputStream(destinationFile)) {             int byteRead;             while ((byteRead = fis.read()) != -1) {                 fos.write(byteRead);             }             System.out.println("File copied successfully!");      

I/O Streams

 Input/Output Streams in Java In Java, streams represent a sequence of data. Input streams are used for reading data from a source, while output streams are used for writing data to a destination. Types of Streams: 1. Byte Streams:    - Operate on bytes.    - Suitable for binary data.    - `InputStream` and `OutputStream` are the abstract classes for byte streams. 2. Character Streams:    - Operate on characters, internally converting them to bytes.    - Suitable for text data.    - `Reader` and `Writer` are the abstract classes for character streams. Commonly Used Byte Streams: - `FileInputStream` and `FileOutputStream`: For reading/writing from/to files. - `ByteArrayInputStream` and `ByteArrayOutputStream`: For reading/writing to byte arrays. - `DataInputStream` and `DataOutputStream`: For reading/writing primitive data types. - `ObjectInputStream` and `ObjectOutputStream`: For reading/writing Java objects. Commonly Used Character Streams: - `FileReader` and `FileWriter`: For reading

Experiment No: 1 - Study of Java Runtime Environment (JRE).

  Experiment No: 1   Aim: Study   of Java run time environment (JRE) Theory: Java is a programming language and a platform. Platform Any hardware or software environment in which a program runs, known as a platform. Since Java has its own Runtime Environment (JRE) and API, it is called platform. The history of Java is a fascinating journey that began in the early 1990s and continues to shape the modern software development landscape. Here's an overview of the key milestones and events in the history of Java: Origins (Early 1990s): Java's story begins with a team of engineers at Sun Microsystems, led by James Gosling, Mike Sheridan, and Patrick Naughton. Their goal was to create a programming language that could be used to develop software for consumer electronic devices, such as set-top boxes and interactive television. Green Project (1991): The project, initially known as the "Green Project," aimed to develop a language that could addr

JUnit Testing Framework

 JUnit is a popular testing framework for Java programming language. It provides a simple and efficient way to write unit tests for your Java code, allowing you to ensure that your code functions as expected and continues to work correctly as you make changes. Key Features of JUnit: 1. Annotations:  JUnit uses annotations to identify methods that specify test cases and setup/teardown operations. Annotations include `@Test`, `@Before`, `@After`, `@BeforeClass`, and `@AfterClass`. 2. Assertions:  JUnit provides a set of assertion methods such as `assertEquals`, `assertTrue`, `assertFalse`, `assertNotNull`, etc., to validate expected outcomes of tests. 3. Test Runners:  JUnit includes various test runners that execute test cases and report results. The default runner is `BlockJUnit4ClassRunner`, but JUnit also supports parameterized tests and suites. 4. Parameterized Tests:  JUnit allows you to write parameterized tests using the `@ParameterizedTest` annotation, which enables you to run t