{"id":19977,"date":"2023-01-20T10:54:03","date_gmt":"2023-01-20T09:54:03","guid":{"rendered":"https:\/\/www.codemotion.com\/magazine\/?p=19977"},"modified":"2023-02-03T12:49:29","modified_gmt":"2023-02-03T11:49:29","slug":"socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol","status":"publish","type":"post","link":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/","title":{"rendered":"Socket Programming in Java: How to Establish a Solid Computer Networking Protocol"},"content":{"rendered":"\n<p>Looking to get up to speed on Java socket programming?&nbsp;<\/p>\n\n\n\n<p>You\u2019re not alone.<\/p>\n\n\n\n<p>Whether you\u2019re planning to write game console applications or focusing on the <a href=\"https:\/\/www.codemotion.com\/magazine\/devops\/what-is-devsecops-methodology-and-why-is-it-so-important\/\" target=\"_blank\" aria-label=\"benefits of implementing DevSecOps in your processes (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">benefits of implementing DevSecOps in your processes<\/a>, knowing more about how sockets work in Java will give you a better understanding of how machines communicate over networks.<\/p>\n\n\n\n<p>In this article, we\u2019ll take a closer look at how to use the <strong>Socket class in Java<\/strong> to enable effective and efficient network communication.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-are-sockets-in-java\">What are sockets in Java?<\/h2>\n\n\n\n<p>Whether you\u2019re working with <a href=\"https:\/\/www.dialpad.com\/glossary\/cloud-pbx\/\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">cloud PBX<\/a> platforms or any kind of general network, you\u2019ll need to have an understanding of how sockets work in Java.<\/p>\n\n\n\n<p>Essentially, socket programming enables applications <strong>running in separate Java runtime environments <\/strong>to communicate with one another. It\u2019s how a client and a server talk to each other.<\/p>\n\n\n\n<p>If you want a precise definition of a socket, it\u2019s this: one end of a two-way communication link established between two nodes in a network.&nbsp;<\/p>\n\n\n\n<p>In the Java platform, the Socket class is used to create a socket on the network. Achieving the desired network communication will mean setting up <strong>both a client socket and a server socket<\/strong>. Each socket is assigned an IP address and a port number. This makes sure the network protocol layer is able to identify the application that data should be sent to.<\/p>\n\n\n\n<p>Using the class instead of native code means that your Java programs will be able to communicate over the network on a platform-independent basis. In terms of <a href=\"https:\/\/www.openlegacy.com\/blog\/microservices-java\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">microservices Java<\/a> resources are available that will be of assistance in simplifying the task.<\/p>\n\n\n\n<p>The basic approach is fairly straightforward, however. As long as you understand the way network communication works, you shouldn\u2019t run into too many problems.<\/p>\n\n\n\n<p>If you are using virtual private server hosting such as Ultahost (a <a aria-label=\" (opens in a new tab)\" href=\"https:\/\/ultahost.com\/canada-toronto-vps\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"ek-link\">VPS in Canada<\/a>), then you will need to make sure it is compatible with your coding languages.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-tcp-vs-udp\">TCP vs. UDP<\/h2>\n\n\n\n<p>There are two methods for achieving network communication: <strong>Transport Control Protocol (TCP) and User Datagram Protocol (UDP). <\/strong>They each have their advantages and disadvantages, so the choice of which to use will depend on the nature of the project. One of the many <a href=\"https:\/\/www.codemotion.com\/magazine\/backend\/reasons-to-learn-java\/\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">reasons to learn Java<\/a> is that it will give you plenty of experience in making this kind of decision.<\/p>\n\n\n\n<p>TCP is a reliable protocol that ensures delivery of all information packets. With TCP, both nodes know that the communication payloads have been received. UDP, on the other hand, is connectionless. It\u2019s useful for instances where you don\u2019t need every single packet to be received.&nbsp;<\/p>\n\n\n\n<p>An example of this would be video streaming: UDP is usually used for this. That\u2019s because in this case, ensuring delivery of all packets is not the optimal choice if it means that the client slows down delivery of your video in order to receive every single packet. Far better that a few packets are missing but that speed is maintained. That way, it won\u2019t spoil your enjoyment as a viewer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-client-side-programming\">Client-side programming<\/h2>\n\n\n\n<p>So let\u2019s first consider the programming you\u2019ll need to do for the client-side socket.<\/p>\n\n\n\n<p>How client\u2013server communication works is that the client first waits for the server to start and then sends it a request via the connection created between corresponding sockets. The underlying principle is the same in any network, be it for sending print jobs to an office printer or facilitating communication in <a href=\"https:\/\/www.dialpad.com\/products\/cloud-contact-center\/\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">cloud contact centres<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-establish-your-connection\">1. Establish your connection<\/h3>\n\n\n\n<p>Step one is to establish a socket connection. This means that the two nodes need to be able to recognise each other\u2019s network location and TCP port.<\/p>\n\n\n\n<p>To create a socket, you start like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Socket socket = <span class=\"hljs-keyword\">new<\/span> Socket(\u201c<span class=\"hljs-number\">127.0<\/span><span class=\"hljs-number\">.0<\/span><span class=\"hljs-number\">.1<\/span>\u201d, <span class=\"hljs-number\">5000<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this line of code, the first argument represents the client-side socket and the second argument both the IP address of the machine running the server and the TCP port.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-implement-communication\">2. Implement communication<\/h3>\n\n\n\n<p>To actually send information via the connection, you\u2019ll need to obtain input and output streams. This is fairly self-explanatory. Input streams are for reading data from the server and output streams are for writing data to the server.<\/p>\n\n\n\n<p>You can do this by using the input and output stream functions:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">InputStream in = socket.getInputStream();<\/code><\/span><\/pre>\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">OutputStream out = socket.getOutputStream();<\/code><\/span><\/pre>\n\n\n<p>Bear in mind that these are the same types of streams you would use to read from and write to any file. So they can easily be modified to suit your purposes. For instance, you could wrap the InputStream with a BufferedReader via an InputStreamReader, making it a simple matter to read text with, say, readLine().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-closing-your-connection\">3. Closing your connection<\/h3>\n\n\n\n<p>Once you\u2019ve established your connection and used it to send requests, you\u2019ll need to close the connection.<\/p>\n\n\n\n<p>When you put it all together, you\u2019ll end up with something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"692\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/image-7.png\" alt=\"\" class=\"wp-image-19980\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/image-7.png 940w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/image-7-300x221.png 300w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/image-7-768x565.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"783\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/image-6.png\" alt=\"\" class=\"wp-image-19979\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/image-6.png 940w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/image-6-300x250.png 300w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/image-6-768x640.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-server-side-programming\">Server-side programming<\/h2>\n\n\n\n<p>Server-side programming is similar to client-side programming in that you firstly need to establish the connection, then you implement the requests and finish by closing the connection. In the spirit of <a href=\"https:\/\/www.codemotion.com\/magazine\/soft-skills\/it-careers\/developers-continuous-learning-upskilling-cross-training\/\">continuous learning<\/a>, let\u2019s have a look at the details of this too. Here are the steps you\u2019ll need to take:<\/p>\n\n\n\n<p>1) Firstly, establish a server-side socket using the Socket class:<\/p>\n\n\n\n<p>ServerSocket serverSocket = new ServerSocket(4000);<\/p>\n\n\n\n<p>2) Use the ServerSocket&#8217;s accept() method to listen for a connection from the client on the designated port.<\/p>\n\n\n\n<p>3) When a client connects to the server, the accept() method returns a Socket for the server to use to communicate with the client. The process is the same as before: <strong>obtain an InputStream to read from the client and an OutputStream to write to it<\/strong>.<\/p>\n\n\n\n<p>4) Again, the argument 4000 here is the port number. The server waits until it receives a connection request at its socket from the client side. Once it does, the following line of code is executed:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Socket clientSocket = serverSocket.accept();<\/code><\/span><\/pre>\n\n\n<p>If you\u2019ve done everything correctly, at this point the request is accepted by the server.<\/p>\n\n\n\n<p>In other words, once you\u2019ve completed the configuration of the connection on both the client and server endpoints, you execute the server-side program first. Only after doing that do you run the client-side program in order to send the request. Once the request is sent from the client, the server will respond.<\/p>\n\n\n\n<p>Don\u2019t forget: when writing your Java code, never be afraid of <a href=\"https:\/\/www.computer.org\/publications\/tech-news\/build-your-career\/javascript-code-reuse-and-maintainability\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">code reuse<\/a>. It\u2019s a popular habit for a reason. Reusing code that works in new applications is standard practice and it helps speed up the process of new development.<\/p>\n\n\n\n<figure class=\"wp-block-image alignfull size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"652\" height=\"434\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/image-5.png\" alt=\"\" class=\"wp-image-19978\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/image-5.png 652w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/image-5-300x200.png 300w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/image-5-600x400.png 600w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><figcaption class=\"wp-element-caption\">Don&#8217;t be afraid to reuse your code.<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-in-practice-running-the-code-on-a-terminal-or-command-prompt\">In practice \u2013 running the code on a terminal or command prompt<\/h3>\n\n\n\n<p>Both the client-side and the server-side Java code will be saved as a .Java file. In UNIX-based systems, these will run on a terminal, while in Windows-based ones, you can run them from the command prompt. When implementing the code this way, follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Step 1: Open your terminal or command prompt.&nbsp;<\/li>\n\n\n\n<li>Step 2: Make sure to change your path to the directory that contains the Java files for both client and server.<\/li>\n\n\n\n<li>Step 3: Server first, remember! Use the command $ java server to run the server program.<\/li>\n\n\n\n<li>Step 4: Run the client program using the command $ java client.<\/li>\n<\/ul>\n\n\n\n<p>All going well, you should see some text indicating that the client sent its request to the server and that the connection was successfully accepted by the server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-a-few-words-on-nio-nio-2-and-non-blocking-apis\">A few words on NIO\/NIO.2 and non-blocking APIs<\/h2>\n\n\n\n<p>Java is one of the go-to <a href=\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/cloud-professionals-top-programming-languages\/\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">languages for cloud professionals<\/a> and as such, it is regularly being updated. For basic network communication, the steps we\u2019ve explained so far will be sufficient in most cases. However, there will be some occasions where it\u2019s better to use NIO instead.<\/p>\n\n\n\n<p>The java.nio package was introduced in Java 1.4 before being updated in Java 1.7 (NIO.2). It has enhanced file operation capability and offers the ASynchronousSocketChannel option.<\/p>\n\n\n\n<p>Why would you need to use this? Well, it\u2019s very handy for those situations where you\u2019re dealing with asynchronous input\/output.<\/p>\n\n\n\n<p>With NIO, what you can do is open a channel to your destination, then use a buffer. The process involves writing information to a buffer, then reading the data from a buffer, and sending the resulting data to the destination.&nbsp;<\/p>\n\n\n\n<p>In order to use a buffer, you\u2019ll follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Step 1: Write data into a buffer<\/li>\n\n\n\n<li>Step 2: Call the buffer&#8217;s flip() method&nbsp;<\/li>\n\n\n\n<li>Step 3: Read data from the buffer<\/li>\n\n\n\n<li>Step 4: Call the buffer&#8217;s clear() or compact() method in preparation for it receiving further data<\/li>\n<\/ul>\n\n\n\n<p>There\u2019s a lot to say about NIO that is outside of the scope of this article, but it\u2019s worth being aware of. It might not be entirely necessary if you\u2019re working on a project like implementing a <a href=\"https:\/\/www.dialpad.com\/products\/voip-phone-system\/\">voice over IP (VoIP) phone system<\/a>, but it\u2019s part and parcel of modern Java practice.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-making-communication-work\">Making communication work<\/h2>\n\n\n\n<p>Whether you\u2019re working on the finer points of <a href=\"https:\/\/www.make.com\/en\/integrations\/hubspotcrm\/jira\">Jira HubSpot integration<\/a> or developing network applications, you may encounter the need to use Java in all sorts of situations.<\/p>\n\n\n\n<p>Hopefully, this introduction to socket <a href=\"https:\/\/www.codemotion.com\/magazine\/backend\/reasons-to-learn-java\/\" class=\"ek-link\">programming <\/a>in Java has whetted your appetite to learn more. If so, we wish you the best of luck on your Java journey!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looking to get up to speed on Java socket programming?&nbsp; You\u2019re not alone. Whether you\u2019re planning to write game console applications or focusing on the benefits of implementing DevSecOps in your processes, knowing more about how sockets work in Java will give you a better understanding of how machines communicate over networks. In this article,&#8230; <a class=\"more-link\" href=\"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/\">Read more<\/a><\/p>\n","protected":false},"author":125,"featured_media":19983,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":6,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_uag_custom_page_level_css":"","_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[20],"tags":[52],"collections":[],"class_list":{"0":"post-19977","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-languages","8":"tag-java","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Getting Started With Socket Programming in Java - Codemotion<\/title>\n<meta name=\"description\" content=\"Don&#039;t miss this guide with all you need to know about socket programming in Java and how to establish a solid Computer Networking Protocol.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Socket Programming in Java: How to Establish a Solid Computer Networking Protocol\" \/>\n<meta property=\"og:description\" content=\"Don&#039;t miss this guide with all you need to know about socket programming in Java and how to establish a solid Computer Networking Protocol.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/\" \/>\n<meta property=\"og:site_name\" content=\"Codemotion Magazine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Codemotion.Italy\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-20T09:54:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-03T11:49:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1397\" \/>\n\t<meta property=\"og:image:height\" content=\"751\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jenna Bunnell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:site\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jenna Bunnell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\\\/\"},\"author\":{\"name\":\"Jenna Bunnell\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#\\\/schema\\\/person\\\/42e32581e4cd58c07562013d2435da54\"},\"headline\":\"Socket Programming in Java: How to Establish a Solid Computer Networking Protocol\",\"datePublished\":\"2023-01-20T09:54:03+00:00\",\"dateModified\":\"2023-02-03T11:49:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\\\/\"},\"wordCount\":1537,\"publisher\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/iStock-1335247101.jpg\",\"keywords\":[\"Java\"],\"articleSection\":[\"Languages and frameworks\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\\\/\",\"url\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\\\/\",\"name\":\"Getting Started With Socket Programming in Java - Codemotion\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/iStock-1335247101.jpg\",\"datePublished\":\"2023-01-20T09:54:03+00:00\",\"dateModified\":\"2023-02-03T11:49:29+00:00\",\"description\":\"Don't miss this guide with all you need to know about socket programming in Java and how to establish a solid Computer Networking Protocol.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/iStock-1335247101.jpg\",\"contentUrl\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/iStock-1335247101.jpg\",\"width\":1397,\"height\":751,\"caption\":\"Computer with elements of program code on the screen and the inscription Java and a keyboard on a purple background 3d\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Languages and frameworks\",\"item\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Socket Programming in Java: How to Establish a Solid Computer Networking Protocol\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#website\",\"url\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/\",\"name\":\"Codemotion Magazine\",\"description\":\"We code the future. Together\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#organization\",\"name\":\"Codemotion\",\"url\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/wp-content\\\/uploads\\\/2019\\\/11\\\/codemotionlogo.png\",\"contentUrl\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/wp-content\\\/uploads\\\/2019\\\/11\\\/codemotionlogo.png\",\"width\":225,\"height\":225,\"caption\":\"Codemotion\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Codemotion.Italy\\\/\",\"https:\\\/\\\/x.com\\\/CodemotionIT\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#\\\/schema\\\/person\\\/42e32581e4cd58c07562013d2435da54\",\"name\":\"Jenna Bunnell\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e3e68825799a74211aa2cfb5a1722059647dc0b3494da04f357594df469d9a9f?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e3e68825799a74211aa2cfb5a1722059647dc0b3494da04f357594df469d9a9f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e3e68825799a74211aa2cfb5a1722059647dc0b3494da04f357594df469d9a9f?s=96&d=mm&r=g\",\"caption\":\"Jenna Bunnell\"},\"description\":\"Jenna Bunnell is the Senior Manager for Content Marketing at Dialpad, an AI-incorporated cloud-hosted stand up meetings system that provides call details for business owners and sales representatives. She is driven and passionate about communicating a brand\u2019s design sensibility and visualizing how content can be presented in creative and comprehensive ways.\",\"url\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/author\\\/jenna-bunnell\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Getting Started With Socket Programming in Java - Codemotion","description":"Don't miss this guide with all you need to know about socket programming in Java and how to establish a solid Computer Networking Protocol.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/","og_locale":"en_US","og_type":"article","og_title":"Socket Programming in Java: How to Establish a Solid Computer Networking Protocol","og_description":"Don't miss this guide with all you need to know about socket programming in Java and how to establish a solid Computer Networking Protocol.","og_url":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/","og_site_name":"Codemotion Magazine","article_publisher":"https:\/\/www.facebook.com\/Codemotion.Italy\/","article_published_time":"2023-01-20T09:54:03+00:00","article_modified_time":"2023-02-03T11:49:29+00:00","og_image":[{"width":1397,"height":751,"url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101.jpg","type":"image\/jpeg"}],"author":"Jenna Bunnell","twitter_card":"summary_large_image","twitter_creator":"@CodemotionIT","twitter_site":"@CodemotionIT","twitter_misc":{"Written by":"Jenna Bunnell","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/#article","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/"},"author":{"name":"Jenna Bunnell","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/42e32581e4cd58c07562013d2435da54"},"headline":"Socket Programming in Java: How to Establish a Solid Computer Networking Protocol","datePublished":"2023-01-20T09:54:03+00:00","dateModified":"2023-02-03T11:49:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/"},"wordCount":1537,"publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101.jpg","keywords":["Java"],"articleSection":["Languages and frameworks"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/","url":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/","name":"Getting Started With Socket Programming in Java - Codemotion","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/#primaryimage"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101.jpg","datePublished":"2023-01-20T09:54:03+00:00","dateModified":"2023-02-03T11:49:29+00:00","description":"Don't miss this guide with all you need to know about socket programming in Java and how to establish a solid Computer Networking Protocol.","breadcrumb":{"@id":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/#primaryimage","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101.jpg","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101.jpg","width":1397,"height":751,"caption":"Computer with elements of program code on the screen and the inscription Java and a keyboard on a purple background 3d"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codemotion.com\/magazine\/languages\/socket-programming-in-java-how-to-establish-a-solid-computer-networking-protocol\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codemotion.com\/magazine\/"},{"@type":"ListItem","position":2,"name":"Languages and frameworks","item":"https:\/\/www.codemotion.com\/magazine\/languages\/"},{"@type":"ListItem","position":3,"name":"Socket Programming in Java: How to Establish a Solid Computer Networking Protocol"}]},{"@type":"WebSite","@id":"https:\/\/www.codemotion.com\/magazine\/#website","url":"https:\/\/www.codemotion.com\/magazine\/","name":"Codemotion Magazine","description":"We code the future. Together","publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codemotion.com\/magazine\/#organization","name":"Codemotion","url":"https:\/\/www.codemotion.com\/magazine\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","width":225,"height":225,"caption":"Codemotion"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Codemotion.Italy\/","https:\/\/x.com\/CodemotionIT"]},{"@type":"Person","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/42e32581e4cd58c07562013d2435da54","name":"Jenna Bunnell","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/e3e68825799a74211aa2cfb5a1722059647dc0b3494da04f357594df469d9a9f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/e3e68825799a74211aa2cfb5a1722059647dc0b3494da04f357594df469d9a9f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e3e68825799a74211aa2cfb5a1722059647dc0b3494da04f357594df469d9a9f?s=96&d=mm&r=g","caption":"Jenna Bunnell"},"description":"Jenna Bunnell is the Senior Manager for Content Marketing at Dialpad, an AI-incorporated cloud-hosted stand up meetings system that provides call details for business owners and sales representatives. She is driven and passionate about communicating a brand\u2019s design sensibility and visualizing how content can be presented in creative and comprehensive ways.","url":"https:\/\/www.codemotion.com\/magazine\/author\/jenna-bunnell\/"}]}},"featured_image_src":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101-600x400.jpg","featured_image_src_square":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101-600x600.jpg","author_info":{"display_name":"Jenna Bunnell","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/jenna-bunnell\/"},"uagb_featured_image_src":{"full":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101.jpg",1397,751,false],"thumbnail":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101-150x150.jpg",150,150,true],"medium":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101-300x161.jpg",300,161,true],"medium_large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101-768x413.jpg",768,413,true],"large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101-1024x550.jpg",1024,550,true],"1536x1536":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101.jpg",1397,751,false],"2048x2048":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101.jpg",1397,751,false],"small-home-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101.jpg",100,54,false],"sidebar-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101-180x128.jpg",180,128,true],"genesis-singular-images":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101-896x504.jpg",896,504,true],"archive-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101-400x225.jpg",400,225,true],"gb-block-post-grid-landscape":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101-600x400.jpg",600,400,true],"gb-block-post-grid-square":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2023\/01\/iStock-1335247101-600x600.jpg",600,600,true]},"uagb_author_info":{"display_name":"Jenna Bunnell","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/jenna-bunnell\/"},"uagb_comment_info":0,"uagb_excerpt":"Looking to get up to speed on Java socket programming?&nbsp; You\u2019re not alone. Whether you\u2019re planning to write game console applications or focusing on the benefits of implementing DevSecOps in your processes, knowing more about how sockets work in Java will give you a better understanding of how machines communicate over networks. In this article,&#8230;&hellip;","lang":"en","_links":{"self":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/19977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/users\/125"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/comments?post=19977"}],"version-history":[{"count":6,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/19977\/revisions"}],"predecessor-version":[{"id":20076,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/19977\/revisions\/20076"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media\/19983"}],"wp:attachment":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media?parent=19977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/categories?post=19977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/tags?post=19977"},{"taxonomy":"collections","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/collections?post=19977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}