<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Eshan Kavinda Perera on Medium]]></title>
        <description><![CDATA[Stories by Eshan Kavinda Perera on Medium]]></description>
        <link>https://medium.com/@eshankavinda?source=rss-4b8a1f5e2cb7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nIIiKVzBRtKYB4gIrfsIog.jpeg</url>
            <title>Stories by Eshan Kavinda Perera on Medium</title>
            <link>https://medium.com/@eshankavinda?source=rss-4b8a1f5e2cb7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 18:50:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@eshankavinda/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Headfirst Java (Ch: 16) *Data Structures (collections and generics)]]></title>
            <link>https://medium.com/@eshankavinda/headfirst-java-ch-16-data-structures-collections-and-generics-18da7b39dfaa?source=rss-4b8a1f5e2cb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/18da7b39dfaa</guid>
            <category><![CDATA[generics]]></category>
            <dc:creator><![CDATA[Eshan Kavinda Perera]]></dc:creator>
            <pubDate>Tue, 19 Jul 2022 20:20:58 GMT</pubDate>
            <atom:updated>2022-07-19T20:20:58.162Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/854/0*GTk9ddsLysec7jol.png" /></figure><p>The Java Collections Framework includes a data structure that should be suitable for almost every task you may face such as adding data, find something, view duplicates etc…</p><blockquote><strong>ArrayList API</strong></blockquote><p>Consider, you have to add 10 different songs names to arrayList. You can do it easily using add method. Now you want to sort these songs names to alphabetical order. But when you look in ArrayList, there doesn’t seem to be any method related to sorting. In other words there is no sort() method. ArrayList is not the only collection and there are more key collection classes.</p><ul><li>TreeSet — keeps the elements sorted and prevents duplicates.</li><li>HashMap — store and access elements as name/value pairs.</li><li>LinkedList — makes it easy to create structures like stacks or queues.</li><li>HashSet — prevents duplicates in the collection, and given an element, can find that element in the collection.</li><li>LinkedHashMap — remember the order of elements (name/value pairs) were inserted</li></ul><p>To sort an array we could use a TreeSet or Collections.sort() method.</p><blockquote><strong>Generics</strong></blockquote><p>Generics enable the creation of type-safe collections, where more errors are discovered during compile time instead of runtime.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*B6xlUktphz1vucoDvCMtNw.png" /></figure><ol><li>Creating instances of generified classes (like ArrayList)</li></ol><pre>new ArrayList&lt;Song&gt;();</pre><p>2. Declaring and assigning variables of generic types</p><pre>List&lt;Song&gt; songList = new ArrayList&lt;Song&gt;();</pre><p>3. Declaring (and invoking) methods that take generic types</p><pre>void foo(List&lt;Song&gt; list){<br> x.foo(songList)<br>}</pre><blockquote><strong>Generic Classes</strong></blockquote><p>The two key areas to look at in a generified class are:</p><ol><li>Class declaration</li><li>Method declarations</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*o95gaysuYHXQ-8ZnhB6RoA.png" /><figcaption>“E” as a stand-in for “the type of element you want this collection to hold and return.”</figcaption></figure><blockquote><strong>Generic Methods</strong></blockquote><p>A generic class means that the class declaration includes a type parameter. A generic method means that the method declaration uses a type parameter in its signature.</p><p>We can use type parameters in a method in two different ways,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*wfdOpbvSTXz2d8eJBF1WZQ.png" /></figure><blockquote><strong><em>Reference</em></strong></blockquote><p><a href="https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf">https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18da7b39dfaa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Headfirst Java (Ch: 15) *Make a Connection]]></title>
            <link>https://medium.com/@eshankavinda/headfirst-java-ch-15-make-a-connection-49f9d3421adc?source=rss-4b8a1f5e2cb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/49f9d3421adc</guid>
            <category><![CDATA[sockets]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Eshan Kavinda Perera]]></dc:creator>
            <pubDate>Tue, 19 Jul 2022 18:13:11 GMT</pubDate>
            <atom:updated>2022-07-19T18:14:25.193Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article we will mainly discuss how to connect our java program to another machine java program. It’s very easy. Classes in the java.net library are in charge of handling all the low-level networking aspects.</p><p>As an example consider real-time chat app, according to the chat application,</p><ul><li>The Client has to know about the Server.</li><li>The Server has to know about ALL the Clients.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*XXPDcenzMP9mldfRT2rxTQ.png" /><figcaption>Overview of chat application</figcaption></figure><p>Chat application works as follows,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*XD279KpHUvNK6vUuYFrJ_Q.png" /></figure><p>We have to plan below 3 things to get the client working are,<br>1) How to establish the initial connection between the client and server <br>2) How to send messages to the server <br>3) How to receive messages from the server</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*EyEefikAKnmhFbIYv9Kh8Q.png" /></figure><p><strong>Let’s look at how to make a network Socket connection</strong></p><p>A Socket connection means the two machines have information about each other, including network location (IP address) and TCP port. To make a Socket connection, you need to know two things about the server. there are,</p><ol><li>Who it is (IP Address)</li><li>Running port (TCP port no)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*VZRHAbNoLUcjcKkMC2cfaQ.png" /></figure><p>A TCP port is a 16-bit number that identifies a specific program on the server. The TCP port range of 0 to 1023 is reserved for well known services. But we don’t use them for our own server programs. Above chat server that uses port 5000. We just choose a value that falls between 1024 and 65535.</p><blockquote><strong>Steps for read data from a Socket</strong></blockquote><p>To read data from a Socket, we have to use a BufferedReader. Let’s look at the steps,</p><ol><li>Make a Socket connection to the server</li></ol><pre>Socket chatSocket = new Socket(“127.0.0.1”, 5000);</pre><p>2. Make an InputStreamReader chained to the Socket’s low-level (connection) input stream</p><pre>InputStreamReader stream = new InputStreamReader(chatSocket.getInputStream());</pre><p>3. Make a BufferedReader and read</p><pre>BufferedReader reader = new BufferedReader(stream); String message = reader.readLine();</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/668/1*KmvLNIiW0Jncdd623c8jyA.png" /><figcaption>read data from a Socket</figcaption></figure><blockquote><strong>Steps for write data to a Socket</strong></blockquote><p>To write data to a Socket, we have to use a PrintWriter. Let’s look at the steps,</p><ol><li>Make a Socket connection to the server</li></ol><pre>Socket chatSocket = new Socket(“127.0.0.1”, 5000);</pre><p>2. Make a PrintWriter chained to the Socket’s low-level (connection) output stream</p><pre>PrintWriter writer = new PrintWriter(chatSocket.getOutputStream());</pre><p>3. Write (print) something</p><pre>writer.println(“message to send”);<br>writer.print(“another message”);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*HF-gT9hcizfJ4iYdN4QoQg.png" /><figcaption>write data to a Socket</figcaption></figure><p><strong>How to launch a new thread</strong></p><ol><li>Make a Runnable object (the thread’s job)</li></ol><pre>Runnable threadJob = new MyRunnable();</pre><p>2. Make a Thread object (the worker) and give it a Runnable (the job)</p><pre>Thread myThread = new Thread(threadJob);</pre><p>3. Start the Thread</p><pre>myThread.start();</pre><p>Runnable is to a Thread what a job is to a worker. A Runnable is the job a thread is supposed to run. A Runnable holds the method that goes on the bottom of the new thread’s stack: run().</p><blockquote><strong>References</strong></blockquote><p><a href="https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf">https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49f9d3421adc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Headfirst Java (Ch: 14) Saving Objects]]></title>
            <link>https://medium.com/@eshankavinda/headfirst-java-ch-14-saving-objects-ba669ac62415?source=rss-4b8a1f5e2cb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba669ac62415</guid>
            <category><![CDATA[state]]></category>
            <category><![CDATA[save]]></category>
            <category><![CDATA[restore]]></category>
            <category><![CDATA[objects]]></category>
            <dc:creator><![CDATA[Eshan Kavinda Perera]]></dc:creator>
            <pubDate>Sun, 17 Jul 2022 20:20:13 GMT</pubDate>
            <atom:updated>2022-07-17T20:20:13.621Z</atom:updated>
            <content:encoded><![CDATA[<p>We already know objects have state and behavior. Behavior lives in the class, but state lives within each individual object. In this article we mainly discuss how to save and restore the state of an object.</p><p>There are lots of options to save the state of Java program. In this article we will be looking at following two options.</p><ol><li><strong>Serialization- </strong>(if our data will be used by <em>only the Java program</em>.)</li></ol><p>Write a file that holds flattened (serialized) objects. Then serialized objects get from the file and re-inflate them into living, breathing, heap-inhabiting objects using our application.</p><p><strong>2. Write a plain text file- </strong>(If our data will be used by other programs)</p><p>Create a file using delimiters that other software can understand. If we write a text file with program state values by separating the pieces of state with commas, the other programs can split the string by comma and restore the state to their program.</p><p><strong>How to Write a serialized object to a file</strong></p><p>There are mainly four steps for serializing (saving) an object. Let’s look at the steps,</p><ol><li>Make a FileOutputStream</li></ol><pre>FileOutputStream fileStream = new FileOutputStream(“MyGame.ser”);</pre><p>2. Make an ObjectOutputStream</p><pre>ObjectOutputStream os = new ObjectOutputStream(fileStream);</pre><p>ObjectOutputStream lets you write objects, but it can’t directly connect to a file. It needs to be fed a ‘helper’. This is actually called ‘chaining’ one stream to another.</p><p>3. Write the object</p><pre>os.writeObject(characterOne); <br>os.writeObject(characterTwo); <br>os.writeObject(characterThree);</pre><p>4. Close the ObjectOutputStream</p><pre>os.close();</pre><p><strong>How data moves in streams from one place to another?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*xzUbiKQ1G6H2zPHN8EwWSA.png" /></figure><p>Connection streams in the Java I/O API represent connections to sources and destinations like files or network sockets. Chain streams must be chained to a connection stream because they are unable to connect on their own.</p><p><strong>What really happens to an object when it’s serialized?</strong></p><ul><li><strong>Object on the heap<br></strong>On the heap, objects have state, which is the value of their instance variables. These values make one instance of a class different from another instance of the same class.</li><li><strong>Object serialized<br></strong>Values of instance variables are stored in serialized objects. So that an identical instance (object) can be brought back to life on the heap.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*LfU7n39GuWCRIOlVlS0-eg.png" /></figure><blockquote>Serialization saves the entire object graph. All objects referenced by instance variables, starting with the object being serialized.</blockquote><p>You must implement Serializable interface with class if you want class to be serializable. The Serializable interface is known as a marker or tag interface, because the interface doesn’t have any methods to implement. If any superclass of a class is serializable, the subclass is automatically serializable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*u2Qwcubd3RWcb8Rpy0jvcg.png" /></figure><p>Consider following example,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/617/1*pyPy0LNDOxLmLkm_gLEPPA.png" /></figure><p>You can’t serialize a Pond object if its Duck instance variable refuses to be serialized (by not implementing Serializable).</p><ul><li>Mark the variable with the transient keyword, if you want an instance variable to be skipped by the serialization process.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/444/1*y6ZHRecPgzkg3zKjES03UA.png" /></figure><p><strong>How to restoring an object (Deserialization)</strong></p><p>There are few steps for deserialization process (restore an object to its’ original state). Let’s look at the steps,</p><ol><li>Make a FileInputStream</li></ol><pre>FileInputStream fileStream = new FileInputStream(“MyGame.ser”);</pre><pre>//If the file “MyGame.ser” doesn’t exist, you’ll get an exception.</pre><p>2.<strong> </strong>Make an ObjectInputStream</p><pre>ObjectInputStream os = new ObjectInputStream(fileStream);</pre><p>Although it can’t directly access a file, ObjectInputStream allows you to read objects. It must be chained to a connection.</p><p>3. Read the objects</p><pre>GameCharacter elf = (GameCharacter) one;<br>GameCharacter troll = (GameCharacter) two;<br>GameCharacter magician = (GameCharacter) three;</pre><p>4. Close the ObjectInputStream</p><pre>os.close();</pre><p>When we close the ObjectInputStream, the FileInputStream (and the file) will close automatically.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/1*SuMXognu3Esok2W6c0nnow.png" /><figcaption>Deserialization process</figcaption></figure><p>When an object is deserialized, the JVM attempts to bring the object back to life by making a new object on the heap that has the same state the serialized object had at the time it was serialized. Also except for the transient variables, which come back either null (for object references) or as default primitive values.</p><blockquote><strong>References</strong></blockquote><p><a href="https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf">https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba669ac62415" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Headfirst Java (Ch: 13) Work on Your Swing]]></title>
            <link>https://medium.com/@eshankavinda/headfirst-java-ch-13-work-on-your-swing-3d2120bac949?source=rss-4b8a1f5e2cb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d2120bac949</guid>
            <category><![CDATA[java-swing]]></category>
            <dc:creator><![CDATA[Eshan Kavinda Perera]]></dc:creator>
            <pubDate>Sat, 16 Jul 2022 10:56:53 GMT</pubDate>
            <atom:updated>2022-07-16T10:56:53.205Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we’ll mainly discuss about Java swing and how to work with layout managers. Also we can learn more about widgets with how to make and display them.</p><p><strong>Swing components</strong></p><p>Technically speaking, a widget is a <strong>Swing Component</strong>. Almost all GUI components can be extended from javax. swing. JComponent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/0*i6egCIrUzDUAB8Kg.png" /><figcaption>widgets of JComponent</figcaption></figure><p>In swing, you can stick just about anything into anything else. As an example, you can add user interactive components like buttons and lists into background elements like frames and panels. A <strong>JPanel </strong>is usually used as a background for grouping other components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*2oUJH_lFISgt2pvHleUPgA.png" /><figcaption>Interactive components vs background components</figcaption></figure><p><strong>Layout Managers</strong></p><p>A layout manager is a Java object associated with a particular component, almost always a background component. The layout manager is responsible for managing the components that make up the component it is connected to. That means, If a frame contains a panel and the panel contains a button, the layout manager of the panel determines the button’s size and location, while the panel’s size and location are managed by the frame’s layout manager. Below example shows how to create panels and add buttons on it.</p><pre>JPanel panelA = new JPanel(); <br>JPanel panelB = new JPanel(); <br>panelB.add(new JButton(“button 1”)); <br>panelB.add(new JButton(“button 2”)); <br>panelB.add(new JButton(“button 3”)); <br>panelA.add(panelB);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*DZfajtqdd_y2oQ7wqd8T_g.png" /></figure><p><strong>The Big Three layout managers</strong></p><ol><li><strong>BorderLayout</strong></li></ol><p>It is the default layout manager for a <strong>frame</strong>. It divides a background component into five regions(North, West, Center, East, South) and BorderLayout manager-controlled background can only have one component added per region.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*8kSrBxDfGU1PS8OmwJHUrQ.png" /></figure><p>2. <strong>FlowLayout</strong></p><p>FlowLayout is the default layout manager for a <strong>panel</strong>. It is mainly used for arrange the components in a line, one after another (in a flow).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*md1r3hrXnMPZ9plUH5cchA.png" /></figure><p>3. <strong>BoxLayout</strong></p><p>BoxLayout is used to arrange the components either vertically or horizontally. BoxLayout.X_AXIS and BoxLayout.Y_AXIS constant can be used to change the alignment of the components are horizontal from left to right and vertical from top to bottom.</p><pre>(new BoxLayout(panel, BoxLayout.Y_AXIS)) //Alignment of the components are vertical from top to bottom</pre><blockquote><strong>References</strong></blockquote><p><a href="https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf">https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d2120bac949" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Headfirst Java (Ch: 12)* A Very Graphic Story]]></title>
            <link>https://medium.com/@eshankavinda/headfirst-java-ch-12-a-very-graphic-story-daa8857950bd?source=rss-4b8a1f5e2cb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/daa8857950bd</guid>
            <category><![CDATA[graphics]]></category>
            <dc:creator><![CDATA[Eshan Kavinda Perera]]></dc:creator>
            <pubDate>Fri, 15 Jul 2022 20:52:34 GMT</pubDate>
            <atom:updated>2022-07-15T20:57:34.496Z</atom:updated>
            <content:encoded><![CDATA[<p>Think, If you’re creating programs that other people will use. You need to use graphical user interface (GUI) instead of command line applications (CLI). CLI applications are not good way for common people usage because they’re weak, inflexible, and unfriendly. In this article we will discuss how to create a simple GUI using Java language.</p><p>A Java has a <strong>JFrame </strong>to develop GUI applications. A JFrame is the object that represents a window on the screen. We can put all the interface things like buttons, checkboxes, text fields, dropdown menus on it. And also it can have an menu bar with menu items and the little windowing icons for minimizing, maximizing, and closing the window. The appearance of the JFrame has depends on the platform you’re using.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/805/1*7KBE8v05KbT0vtxtzPi6bA.png" /><figcaption>JFrame appearance of windows 7, windows 10 and mac os</figcaption></figure><p><strong>Let’s making a GUI</strong></p><ol><li>Make a frame (a JFrame)</li></ol><pre>JFrame frame = new JFrame();</pre><p>2. Make a widget (button, text field, etc.)</p><pre>JButton button = new JButton(“click me”);</pre><p>3. Add the widget to the frame</p><pre>frame.getContentPane().add(button);</pre><p>You don’t add things to the frame directly. Think of the frame as the trim around the window, and you add things to the window pane.</p><p>4. Display it (give it a size and make it visible)</p><pre>frame.setSize(300,300);<br>frame.setVisible(true);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/284/1*x4ODk4FQEd1DQDLRjLKblQ.png" /><figcaption>Output of after execute above 4 steps</figcaption></figure><p><strong>How do we get the button to do something specific when the user clicks it?</strong></p><p>We need to following two things</p><ol><li>A <strong>method </strong>to be called when the user clicks (the thing you want to happen as a result of the button click).</li><li>A way to <strong>know </strong>when to trigger that method. In other words, a way to know when the user clicks the button!</li></ol><p><strong>How will we know when the button is clicked?</strong></p><p>In Java, the process of getting and handling a user event is called event-handling. There are many different event types in Java like action event, key event, mouse event etc. When the user clicks a button, the system trigger it’s action event and the event do the action of this button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/291/1*DH_qAza9sjkvBVn2kAknCQ.png" /></figure><p><strong>Listener Interface</strong></p><p>A listener interface is the bridge between the listener (you) and event source (the button). We can implement a listener interface for listen their events. When you implement a listener interface, you give the button a way to call you back. The interface is where the call-back method is declared.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*tpalFWBWe2l-APArmJo8DQ.png" /></figure><p><strong>Let’s Setup ActionEvent for a Button</strong></p><ol><li>Implement the ActionListener interface</li><li>Register with the button (tell it you want to listen for events)</li><li>Define the event-handling method (implement the actionPerformed() method from the ActionListener interface)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*avrO34GxbGGFLVpNbOk1zQ.png" /><figcaption>steps for add actionEvent to a button</figcaption></figure><blockquote><strong>References</strong></blockquote><p><a href="https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf">https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=daa8857950bd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Headfirst Java (Ch: 11) Exception Handling]]></title>
            <link>https://medium.com/@eshankavinda/headfirst-java-ch-11-exception-handling-32886e977659?source=rss-4b8a1f5e2cb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/32886e977659</guid>
            <category><![CDATA[exception-handling]]></category>
            <dc:creator><![CDATA[Eshan Kavinda Perera]]></dc:creator>
            <pubDate>Thu, 14 Jul 2022 20:29:20 GMT</pubDate>
            <atom:updated>2022-07-14T20:29:41.841Z</atom:updated>
            <content:encoded><![CDATA[<p>Java Exception Handling is a mechanism to handle runtime errors. An <strong>exception </strong>is an unwanted or unexpected event that takes place while a program is being run. There are some reasons for an exception occurs,</p><p>1.Invalid user input<br>2. Device failure<br>3. Loss of network connection<br>4. Physical limitations (out of disk memory)<br>5. Code errors<br>6. Opening an unavailable file</p><p>What is the different between Exception and Error?</p><p>Error and Exception are not same. Main difference between error and exception are, An Error indicates a serious problem that a reasonable application should not try to catch. But an Exception indicates conditions that a reasonable application might try to catch.</p><p>In Java, the class Throwable includes all exception and error types as subclasses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/0*7gBJKiCweQzl6Rrr.png" /><figcaption>Exception Hierarchy</figcaption></figure><p>In order to handle an Exception the compiler needs to know that you’re calling a risky method. To handle that you should wrap the risky code something called in <strong>try/catch.</strong></p><p>A try/catch block informs the compiler that you are aware that the method you are calling may experience an exceptional occurrence and that you’re prepared to handle it. That compiler doesn’t care how you handle and exception, it cares only that you say you’re taking care of it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*kp6ejk54vZNsByB3A1uo3Q.png" /><figcaption>Usage of try/catch block</figcaption></figure><p><strong>An exception is an object of type Exception.</strong></p><p>Exception is an object. In the below example, the <strong><em>catch</em></strong> argument is declared as type Exception, and the reference variable for the parameter is <strong><em>ex</em></strong>. What you write in a catch block depends on the exception that was thrown. For an example, if a text file is missing when you write something on the text file you might use the catch block to inform user to ‘file is not found’ and ask from user to create a new file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/1*fqABecACMYY1n-Klk4elUg.png" /><figcaption>Exception is an object</figcaption></figure><p><strong>Types of Exceptions</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/0*hi0OnGNWofZ118hY.jpg" /></figure><p>Java defines a number of exception types that are connected to its different class libraries. Java also gives users the option to create custom exceptions. Exceptions can be divided into two categories, there are,</p><ol><li>Built-in Exceptions</li></ol><ul><li><strong>Checked Exception</strong> — Checked exceptions are called compile-time exceptions because these exceptions are checked at compile-time by the compiler. In other words Exceptions that are NOT subclasses of RuntimeException are checked for by the compiler. They’re called “checked exceptions”</li><li><strong>Unchecked Exception</strong> — The compiler will not check these exceptions at compile time. In other words, if a program throws an unchecked exception, and even if we didn’t handle or declare it, the program would not give a compilation error.</li></ul><p>2. User-Defined Exceptions</p><p>Java’s built-in exceptions may sometimes be unable to explain clearly a particular situation. In such cases, users can also create custom exceptions, which are called ‘user-defined Exceptions’.</p><p><strong>Flow control in try/catch blocks</strong></p><p>When you call a risky method, one of two things can happen.</p><ol><li>The try block completes when the risky method succeeds</li><li>Throws an exception back to the caller function when the risky method fails.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*BSzOHpY1i98fMaQ9VSiaaA.png" /></figure><p><strong>Usage of finally block</strong></p><p>A finally block is where you put code that must run regardless of an exception.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/251/1*u1NemP4AIl51bd8zUPB9DA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*zl0R6QM7_XhF4ojfeFPH2Q.png" /><figcaption>Working flow, with exception and without exception</figcaption></figure><p><strong>Catching multiple exceptions</strong></p><ol><li>Using throws</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*K0mPTfrpmLc4JdPLKbqrpA.png" /></figure><p>2. Using multiple catch blocks</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*slVHsfdFgaO8rr8Vd5cTdA.png" /></figure><p>Consider following hierarchy,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/291/1*Y3iUBMSbZQ9bj-WrkNY_zA.png" /></figure><p>Exceptions are polymorphic, so that,</p><ol><li>You can DECLARE exceptions using a supertype of the exceptions you throw.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*MQNbXfUPC8ByZXUcPDcuOA.png" /></figure><p>2. You can CATCH exceptions using a supertype of the exception thrown.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/665/1*TeXMuQU_voI4VWV9r_ppJg.png" /></figure><p>There are some exception rules,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*6cAijnKL_mnWq0-PEOfRIw.png" /></figure><blockquote><strong>References</strong></blockquote><p><a href="https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf">https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32886e977659" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Headfirst Java (Ch: 10)* Numbers and Statics]]></title>
            <link>https://medium.com/@eshankavinda/headfirst-java-ch-10-numbers-and-statics-b7bf8c18c8a4?source=rss-4b8a1f5e2cb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7bf8c18c8a4</guid>
            <category><![CDATA[static]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Eshan Kavinda Perera]]></dc:creator>
            <pubDate>Tue, 12 Jul 2022 19:34:13 GMT</pubDate>
            <atom:updated>2022-07-12T19:34:13.830Z</atom:updated>
            <content:encoded><![CDATA[<p>Think, you have to get the absolute value of a number, round a number, find the maximum/minimum of two numbers, print the number with two decimal places, put commas into your large numbers in order to read easier, parsing a String into a number, turning a number into a String. How do you do that?. In the Java API, numerous useful methods for changing numbers are available and simple to use. But most of the methods are <strong>static. </strong>In this article, we can learn about what it means for a variable or method to be static, including constants in Java static final variables.</p><p><strong>Math methods</strong></p><p>Methods in the Math class don’t use any instance variable values. And because the methods are ‘static’, you don’t need to have an instance of Math. All you need is the Math class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/227/1*Oa5mcQann8ag54xw9OXpig.png" /><figcaption>Usage of the methods of Math class</figcaption></figure><p>The functionality of these methods doesn’t require knowledge of a particular object because they never use instance variables. If you try to make an instance of class Math we will get the error message.</p><p><strong>Difference between regular (non-static) and static methods</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*268_Z8-izni5XMXnuxN8JA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*1NuEUQeDCPcvRRMZxOQyiw.png" /><figcaption>regular method vs static methods</figcaption></figure><blockquote><strong>A static method means “behavior not dependent on an instance variable, so no instance/object is required. Just the class.”</strong></blockquote><ul><li>We can call a <strong>static</strong> method using a class name and call a <strong>non-static (regular) </strong>method using a reference variable name.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*Dd0HgWYBnepaX00lEigTLg.png" /><figcaption>Example for usage of static method and regular method</figcaption></figure><ul><li>Static methods can’t use non-static (instance) variables.</li></ul><p>If you try to use an instance variable from inside a static method, the compiler thinks, “I don’t know which object’s instance variable you’re talking about!” If you have ten Duck objects on the heap, a static method doesn’t know about any of them. As an example If you try to compile below code,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*haddOPkIObNy9x9SFieEfQ.png" /></figure><p>You will get this error message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/285/1*7PGlY6rJzSqDJ_VsDRWI4w.png" /></figure><p>And also, we can’t compile below code,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/386/1*m22DWZUAfFDHJxHG22O9Sg.png" /></figure><p>because, they usually use instance variable state to affect the behavior of the method. A <strong>getSize()</strong> method returns the value of the size variable, but the compiler doesn’t know whose size should be returned. The object used to invoke the getSize() method. In that case we will get this error message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/265/1*E-UWVahOGCZS7R6NSA6X3w.png" /></figure><h4><strong>Static variables</strong></h4><p>When a variable is specified as <strong>static</strong>, a single copy of the variable is made and shared with all objects at the class level. The same static variable value is shared by all class instances. It means, one value per class, instead of one value per instance.</p><p><strong><em>Initializing a static variable<br></em></strong>There are two guarantees about static initialization.</p><ul><li>Static variables in a class are initialized before any object of that class can be created.</li><li>Static variables in a class are initialized before any static method of the class runs.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*phNonUq3jPwlHk2l1bDITQ.png" /></figure><h4>Static final variables</h4><p>Static final variables are constants. When a variable is declared as final, it cannot be changed after initialization. In other words, the static final variable’s value won’t change as long as the class is loaded.</p><p>We can initialize a final static variable using two ways.</p><ol><li>At the time we declare it.</li><li>In a static initializer.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*Ym7XRl3iO7_zZfH3Ynri_w.png" /><figcaption>Initializing Static final variables</figcaption></figure><p>Additionally, <strong>final</strong> isn’t just for static variables, we can use final keyword with non-static variables, methods and classes.</p><p>A <strong>final variable</strong> means you can’t change its value, a <strong>final method</strong> means you can’t override the method and a <strong>final class</strong> means you can’t extend the class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*AezeVfyjC72WsfbMm7ah3A.png" /></figure><blockquote><strong>References</strong></blockquote><p><a href="https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf">https://www.rcsdk12.org/cms/lib/NY01001156/Centricity/Domain/4951/Head_First_Java_Second_Edition.pdf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7bf8c18c8a4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Analyze TCP Dump for Slow Backends *.]]></title>
            <link>https://medium.com/@eshankavinda/how-to-analyze-tcp-dump-for-slow-backends-8d6d9488b03d?source=rss-4b8a1f5e2cb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d6d9488b03d</guid>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Eshan Kavinda Perera]]></dc:creator>
            <pubDate>Thu, 07 Jul 2022 01:19:30 GMT</pubDate>
            <atom:updated>2022-07-07T01:19:30.281Z</atom:updated>
            <content:encoded><![CDATA[<p>The most of software developers says, the system is slow and the system is sometimes slow, which is a painful topic for them. In this article, we’ll explain how to diagnose this kind of slowness and what things we should check to speed up the system or service.</p><p>Let’s consider the below simple express app and execute the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*slvSVm40kjbx6-zwQ5qoYA.png" /></figure><p>Then, you should send the post request to the server with the simple text as mentioned below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/929/1*mKz8Bf1VKn9s7vCHcowpSA.png" /></figure><p>Let’s go to analyze the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ItPiV3gvJ7PTk2FbwKZ39A.png" /></figure><p>Look at the above figure. There is a special column called Calculated Window Size. Window Size means, the available size of the receiver buffer. If it’s a client, the client receive buffer size and if it is server, the server receive buffer size. According to initial request, the window size is 65535 (line no 4). Let’s click the line no 7 and check the it’s particular transaction details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*DpDnCxwxLeA7TdQHfvl0Fw.png" /></figure><p>We can see which is going from client to server. It says Window is 10233 bytes. According to initial request it shows Window is 65535 bytes. This 65,535 bytes is a huge amount.</p><p>As an example, if you’re going to upload/download 1GB or 2GB file you should send 65535bytes and then get ACK and you send again 65535bytes and then get ACK again and again. You have to repeat the process multiple times. It is spent much time to complete the download or upload because it is very slow. What is the solution of that? Now, Let’s look at that.</p><p>When we tried to the transaction with huge files, we wanted more space. To achieve that, we have to use a flag called Window Scale.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*ath1zS6S40v00P4Ky5-Npg.png" /><figcaption>window scale option during TCP-3way hand shake</figcaption></figure><p>Window scale means 8, this is 2⁸ equals 256. That means though our initial negotiation is 65 535 and accepting 256 times as this value is fine with me. The window scale option is used only during the TCP three-way handshake. To analyzing slowness of the system or service we need to make sure we capture SYN packets during three-way-handshaking because we must ensure that the bandwidth is being utilized fully.</p><blockquote><strong>References</strong></blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FCa9ZRyzeauo%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DCa9ZRyzeauo&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FCa9ZRyzeauo%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9f4ef67621cdc4fba178f39c884d229/href">https://medium.com/media/a9f4ef67621cdc4fba178f39c884d229/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d6d9488b03d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HTTP Keep-Alive and Handshake]]></title>
            <link>https://medium.com/@eshankavinda/http-keep-alive-and-handshake-a0d3da833a8e?source=rss-4b8a1f5e2cb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0d3da833a8e</guid>
            <category><![CDATA[http-keep-alive]]></category>
            <dc:creator><![CDATA[Eshan Kavinda Perera]]></dc:creator>
            <pubDate>Wed, 06 Jul 2022 12:39:50 GMT</pubDate>
            <atom:updated>2022-07-06T12:39:50.107Z</atom:updated>
            <content:encoded><![CDATA[<p>As a backend development software engineer, what is the biggest complaint you are getting from clients?. It is your system/service is slow. So sometimes you say, it worked fine, but sometimes it’s slow or not works well. In this article, we can figure out why it is slow and why it is sometimes slow.</p><p><strong>Two Branches of HTTP<br></strong>1. Persistence HTTP<br>2. Non-Persistence HTTP</p><p>Non-Persistence HTTP is mainly HTTP 1.0. We mainly don’t use that but sometimes if you’re working with legacy systems, you might use it. However that works, we open the connection and send the message. After, we get the response and close connection. In other words, In HTTP/1.0, after every request, the connection between the client and server is lost.</p><p>We have something called RTT (round trip time). Round trip time is the amount of time it takes for a packet to travel from the client to the server. In order to send a message we need to open a connection, send the message, and get the response and close the connection.</p><p>Suppose, If an HTML page contains 10 JavaScript files that need to be opened. To receive the file right now, you must send ten different requests to the server. It means you have to open one request for each file because it opens a connection, takes the file, takes the response, and closes the connection. So this process is difficult and slow. Thus, they introduce <strong>HTTP 1.1</strong> which is a <strong>persistent HTTP.</strong></p><p>In Persistence HTTP there are two parts.<br>1. Tunneling<br>2. Without Tunneling</p><p>Mainly by default HTTP 1.1 is persistence HTTP and it goes with tunneling. Let’s consider how that works. You make the connection, send the messages, get the responses, and close the connection at the end of the transaction. That is the same exactly TCP. However there’s a small trick here.</p><p>Consider, you send the request to backend server. But the server is busy due to doing some task. In that case HTTP might close the connection due to server is not responding. In order to prevent such, we use a special technique called <strong>Keep-Alive</strong>. It means, we just send the ping to the server and saying keep this connection alive.</p><p><strong>Let’s consider Keep-Alive technique with practically</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*o1FSj_oKdH3Kju4nMQjJ9Q.png" /></figure><p>Let’s execute the simple express app using node JS and use browser to call the it’s single endpoint. Let’s check the Wireshark network capture with different conditions.</p><ol><li><strong>Send the request and keep the browser(connection) idle.</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q1CbsnurW8R6WhckFF9kIg.png" /></figure><p>After you send the request, you can see, the standard three-way handshake has been completed (line no 4:6). The server sends the FIN packet to a client (line no. 15), the client just acknowledges it (line no:16) and does not send a FIN packet back to the server. After sometimes, in order to keep the connection alive, the client sends a Keep-Alive message to the server (line no: 19). By doing this, the connection is prevented from lost.</p><p>2. <strong>Send the request three times after getting a response each time.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k24yiw-vFEe-0Y0SKPIrkg.png" /></figure><p>We can see the standard TCP handshake has been completed in line no 18 to 20. Let’s consider line no 22 to 32). We can see the continuous ACK sent between the client and server after getting the each response. So you can see this goes as a cycle. But you can see there is no FIN packets sent from the server because the server is not trying to close the connection.</p><blockquote><strong>References</strong></blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FYf06faA5-JY%3Fstart%3D625%26feature%3Doembed%26start%3D625&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DYf06faA5-JY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FYf06faA5-JY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/4187c6e26cd8a23716c73bc1ae2d12fa/href">https://medium.com/media/4187c6e26cd8a23716c73bc1ae2d12fa/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0d3da833a8e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TCP 3 Way Handshake]]></title>
            <link>https://medium.com/@eshankavinda/tcp-3-way-handshake-a699c2c5e3b8?source=rss-4b8a1f5e2cb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/a699c2c5e3b8</guid>
            <category><![CDATA[tcp-ip]]></category>
            <dc:creator><![CDATA[Eshan Kavinda Perera]]></dc:creator>
            <pubDate>Sun, 03 Jul 2022 21:56:49 GMT</pubDate>
            <atom:updated>2022-07-03T21:56:49.496Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*jGdou9vfPCQ2eQtCZlmhUg.png" /><figcaption>Layers of OSI model</figcaption></figure><p>There is a layer in the Open Systems Interconnection (OSI) model that handles protocols like TCP, UDP, etc. When we writing HTTP applications, it is running on seventh(Application) layer. But the http is build on top of the (Transmission Control Protocol)TCP and (Internet Protocol) IP. Let see how this exactly works.</p><p><strong>What is TCP/IP?</strong></p><p>TCP/IP is a suite of communication protocols used to interconnect network devices on the internet. TCP/IP is also used as a communications protocol in a private computer network (an intranet or extranet). Let’s follow the TCP header format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*NS9sqLroZIpcCkyM.png" /><figcaption>TCP header format</figcaption></figure><p>There is a few important things.</p><ol><li>Source Port and Destination Port</li><li>Sequence Number</li><li>Acknowledge Number</li><li>Flags</li></ol><ul><li>URG — Urgent</li><li>ACK — Acknowledge</li><li>PSH — Push</li><li>RST — Reset</li><li>SYN — Sync</li><li>FIN — Finish</li></ul><p>5. Window Size</p><p>Let’s figure out when the two networks want to connect, how they connect and talk to each other over the TCP.</p><ol><li>Client send a flag the SYN flag is enabled. SYN is set as 1</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*owoVIJfbhqCauZyLLfrr2A.png" /><figcaption>SYN flag is enabled and set as 1</figcaption></figure><p>2. Server respond with the ACK and SYN. ACK and SYN is set to 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*xjeCbMI0lpGFvzVN0Leeeg.png" /><figcaption>Server respond with the ACK and SYN</figcaption></figure><p>3. Client sending an ACK as reply. ACK is set as 1</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*Q0LNaEWQ_WO5gwtvgVHEFQ.png" /><figcaption>communication established</figcaption></figure><p>Now the connection established between client and the server.</p><p>There is an optional call called windows update. Either client side or server side when they communicate, they are communicating window size. Window Size means, how many bytes can send before my buffer get filled. If buffer is filled, it send the buffer field flag to other side in order to avoid received messages from them. Once the application consumed the buffer. Then buffer gets some space to get messages from the other side.</p><p><strong>Now, Let’s consider message sending part</strong></p><p>Following diagram shows how Client and Server establish the connection and send “Hellow Client” message from server to client.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*2dePJOBxgF657qSZCdTHng.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*-ib0VClknEuYWom-oaKLMg.png" /><figcaption>Wireshark Capture</figcaption></figure><p>After successful message transaction, connection has to terminate. Below diagram shows how Client and Server terminate the connection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*ctxyXDI7fqSFYiYsyBTWfQ.png" /><figcaption>Terminate the connection</figcaption></figure><p><a href="https://github.com/EshanKavinda/learn-with-krish/tree/main/TCP_DEMO_WIRESHARK"><strong>Click to view Practical Source Code</strong></a></p><blockquote><strong>References</strong></blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FUpUd5zEUUgI%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DUpUd5zEUUgI&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FUpUd5zEUUgI%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd4f20228c4954b36bd61109e0f7ab25/href">https://medium.com/media/dd4f20228c4954b36bd61109e0f7ab25/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a699c2c5e3b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>