

{"id":12763,"date":"2018-04-08T11:05:06","date_gmt":"2018-04-08T11:05:06","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=12763"},"modified":"2026-05-30T15:01:47","modified_gmt":"2026-05-30T09:31:47","slug":"socket-programming-in-java","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/","title":{"rendered":"Socket Programming in Java &#8211; Established Java Socket Connection"},"content":{"rendered":"<p>This topic may seem intimidating at first but once you understand the nitty-gritty details about it, it is all going to be very easy. Very soon you will be able to run your own servers from the comfort of your laptop! So what are sockets in the first place?<\/p>\n<p>A very abstract definition of it could be that a socket is an interface between the application and the network. When you combine an IP address and a port you get a socket.<\/p>\n<p>Let us dive in!<\/p>\n<h3>What is Socket Programming in Java?<\/h3>\n<p>Java being a high-level language can be useful for designing server-client connections. You can achieve this by socket programming. Socket Programming is a mechanism of connecting the machines with each other thus forming a network.<\/p>\n<p><strong>The socket programming consist of three core parts:<\/strong><\/p>\n<ul>\n<li><strong>The Socket:<\/strong> a socket acts like an ending node that carries the IP address and Port no.<\/li>\n<li><strong>A communication structure:<\/strong> It is a model that explains request response architecture. When the client sends the request, how the server processes it and gives back the response to the client is explained.<\/li>\n<li><strong>Protocols:<\/strong> These are the set of rules that to be followed during the socket programming.<\/li>\n<\/ul>\n<p>There are some classes that we need to learn about before we progress into programming.<\/p>\n<h3>Java Socket Class<\/h3>\n<p>The socket class is useful for creating a socket. These sockets act as endpoints for communication between two machines.<\/p>\n<p>There are few ways by which we can initialize the Socket object:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/ways-to-initialize-the-socket-object.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84900\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/ways-to-initialize-the-socket-object.jpg\" alt=\"ways to initialize the socket object\" width=\"1100\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/ways-to-initialize-the-socket-object.jpg 1100w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/ways-to-initialize-the-socket-object-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/ways-to-initialize-the-socket-object-1024x585.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/ways-to-initialize-the-socket-object-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/ways-to-initialize-the-socket-object-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/ways-to-initialize-the-socket-object-720x411.jpg 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/ways-to-initialize-the-socket-object-520x297.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/ways-to-initialize-the-socket-object-320x183.jpg 320w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/a><\/p>\n<ul>\n<li><strong>Socket() &#8211;<\/strong> This does not connect a socket. It just creates one.<\/li>\n<li><strong>Socket(InetAddress address, int port, InetAddress LocalAddress, int localport) &#8211;<\/strong> This connects the local address and the port to the remote address and the remote port. However, do remember that they have to be on the same network.<\/li>\n<li><strong>Socket(InetAddress address, in port) &#8211;<\/strong> This simply connects the IP address and the port to the local socket.<\/li>\n<li><strong>Socket(String host, int port, InetAddress localAddr, int localPort) &#8211;<\/strong> This connects the socket to the host mentioned in the constructor.<\/li>\n<\/ul>\n<h4>Important Methods in Socket Class in Java<\/h4>\n<p><strong>1. public InputStream getInputStream() &#8211;<\/strong> Serves the purpose of returning the InputStream bound with the socket.<\/p>\n<p><strong>2. public OutputStream getOutPutStream() &#8211;<\/strong> This serves the purpose of returning the Output Steam attached to the socket.<\/p>\n<p><strong>3. public synchronized void close() &#8211;<\/strong> As the name suggests, this closes the connection.<\/p>\n<p><strong>4. public InputStream getInputStream() &#8211;<\/strong> This method returns the InputStream of the socket in action. This stream is in connection with the OutputStream of the other remote socket.<\/p>\n<p><strong>5. publicOutputStream getOutputStream() &#8211;<\/strong> This method returns the OutputStream of the socket. As you might think, the OutputStream is connected with the InputStream of the remote socket.<\/p>\n<p>There are plenty of other methods too in the Socket class. Make sure to check out the documentation for that.<\/p>\n<p>Although we all know no developer has ever done that.<\/p>\n<h3>ServerSocket Class in Java<\/h3>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Serversocket-class-method.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84899\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Serversocket-class-method.jpg\" alt=\"Serversocket class method in Java\" width=\"1000\" height=\"590\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Serversocket-class-method.jpg 1000w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Serversocket-class-method-300x177.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Serversocket-class-method-150x89.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Serversocket-class-method-768x453.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Serversocket-class-method-720x425.jpg 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Serversocket-class-method-520x307.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Serversocket-class-method-320x189.jpg 320w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/a><\/p>\n<p>This class proves to be useful when you create a socket. It has several methods for doing that. Each socket waits for some requests to come over the network.<\/p>\n<p>As soon as it receives a request, it performs the required operations and sends back the response.<\/p>\n<p>However, do note that the actual weight lifting of the sockets is performed by the \u201cSocketImpl\u201d class.<\/p>\n<h4>Important Methods of the ServerSocket Class<\/h4>\n<p><strong>1. public socket accept( ) &#8211;<\/strong> This returns the socket object and establishes a connection between the client and the server.<\/p>\n<p><strong>2. public synchronized void close() &#8211;<\/strong> As the name suggests, this closes the connection.<\/p>\n<p><strong>3. public int getLocalPort() &#8211;<\/strong> When you ask the server to choose any port, then this method is useful for returning the port on which the server is running.<\/p>\n<h3>InetAddress Class Methods in Java<\/h3>\n<p>This class is solely dedicated to IP Addresses. There are a few methods we need to learn about.<\/p>\n<p><strong>1. static InetAddress getByAddress(byte[]addr) &#8211;<\/strong> This returns the InetAddress of the raw IP address given.<\/p>\n<p><strong>2. static InetAddress getByAddress(String host, byte[]addr) &#8211;<\/strong> This returns the InetAddress when the hostname and the IP address is given.<\/p>\n<p><strong>3. String getHostAddress() &#8211;<\/strong> This returns the IP address in a String format.<\/p>\n<p>Let us look at a simple client-server connection through a Java Socket Program.<\/p>\n<h4>Java Program to Illustrate the use of Socket Programming in Java<\/h4>\n<p>At first, we will design the server-side logic.<\/p>\n<p><strong>Server.java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.socketinjava;\r\n\r\nimport java.io.*;\r\nimport java.net.*;\r\n\r\npublic class Server {\r\n   \r\n    public static void main(String[] args) {\r\n        System.out.println(\"Starting the Server!! Any errors will be reported on the screen.... \");\r\n        try {\r\n            ServerSocket s = new ServerSocket(8080);\r\n            \/\/This creates a new socket at port 8080. \r\n            Socket sock=s.accept();\r\n            DataInputStream dis = new DataInputStream(sock.getInputStream());\r\n            String sockmsg =  (String)dis.readUTF();\r\n            System.out.println(\"The message received is \"+sockmsg);\r\n            \/\/This simply prints whatever message the server receives from the client.\r\n            \/\/Now we close the socket\r\n            s.close();\r\n            \/\/\r\n        } catch (Exception e) {\r\n            \/\/TODO: handle exception\r\n            System.out.println(\"There was an error with the file.\");\r\n    \r\n        }\r\n    }\r\n    \r\n\r\n    \r\n}\r\n<\/pre>\n<p><strong>The Client Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.socketinjava;\r\nimport java.net.*;\r\nimport java.io.*;\r\n\r\n\r\npublic class Client \r\n{\r\n\r\n\r\n    public static void main(String[] args) {\r\n        try {\r\n            \/\/At first we create a socket\r\n            Socket clientsock=new Socket(\"localhost\",8080);\r\n            DataOutputStream out = new DataOutputStream(clientsock.getOutputStream());\r\n            out.writeUTF(\"Hey this is a client message!\");\r\n            out.flush();\r\n            out.close();\r\n            clientsock.close();\r\n\r\n\r\n        } catch (Exception e) {\r\n            \/\/TODO: handle exception\r\n            System.out.println(\"There is an error in the system!\");\r\n            System.out.println(e.getMessage());\r\n            \r\n        }      \r\n    }\r\n}\r\n<\/pre>\n<p><strong>OUTPUT for both the programs:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image1-16.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84903\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image1-16.png\" alt=\"Socket Programming in Java\" width=\"1294\" height=\"142\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image1-16.png 1294w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image1-16-300x33.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image1-16-1024x112.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image1-16-150x16.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image1-16-768x84.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image1-16-720x79.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image1-16-520x57.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image1-16-320x35.png 320w\" sizes=\"auto, (max-width: 1294px) 100vw, 1294px\" \/><\/a><\/p>\n<p>Now you must be thinking that it is not very efficient of an approach if I can only send a single message at a time. A duplex communication is essential.<\/p>\n<p>Let us create one!<\/p>\n<p>First, we will create the Server program! This will be similar to the previous program.<\/p>\n<p><strong>Server.java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.socketinjava;\r\n\r\nimport java.io.*;\r\nimport java.net.*;\r\nimport java.util.*;\r\n\r\npublic class Server {\r\n   \r\n    public static void main(String[] args) {\r\n        try\r\n        {\r\n            System.out.println(\"Starting the Server!! Any errors will be reported on the screen.... \");\r\n            ServerSocket s = new ServerSocket(8080);\r\n            \/\/This creates a new socket at port 8080. \r\n            Socket sock=s.accept();\r\n            DataInputStream din = new DataInputStream(sock.getInputStream());\r\n            DataOutputStream dout = new DataOutputStream(sock.getOutputStream());\r\n            Scanner sc = new Scanner(System.in);\r\n            String mesgreceive=\"\", mesgsent=\"\";\r\n            \r\n            while(true)\r\n            {\r\n                \r\n                mesgreceive=din.readUTF();\r\n                System.out.println(\"Client Message:\"+mesgreceive);\r\n                System.out.println(\"Please enter your message\");\r\n                mesgsent=sc.nextLine();\r\n                dout.writeUTF(mesgsent);\r\n                dout.flush();\r\n                if(mesgsent.equals(\"kill\")||mesgreceive.equals(\"kill\"))\r\n                break;\r\n                \r\n                \r\n            }\r\n            din.close();\r\n            dout.close();\r\n\r\n            System.out.println(\"Closing the connection socket!\");\r\n            s.close();\r\n    }\r\n    catch(Exception e)\r\n    {\r\n        System.out.println(\"There was an error in the program! \" + e.getMessage());\r\n    }\r\n}\r\n}\r\n<\/pre>\n<p><strong>Client.java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.socketinjava;\r\nimport java.net.*;\r\nimport java.io.*;\r\nimport java.util.*;\r\n\r\n\r\npublic class Client \r\n{\r\n\r\n\r\n    public static void main(String[] args) {\r\n        try {\r\n            \/\/At first we create a socket\r\n            Socket clientsock=new Socket(\"localhost\",8080);\r\n            DataOutputStream out = new DataOutputStream(clientsock.getOutputStream());\r\n            DataInputStream in =new DataInputStream(clientsock.getInputStream());\r\n            String receivemsg=\"\",sendmesg=\"\";\r\n            Scanner sc = new Scanner(System.in);\r\n\r\n            out.writeUTF(\"Hey this is a client message!\");\r\n            out.flush();\r\n            while(true)\r\n            {\r\n                \/\/For this case, we will be sending a message from the client to the server first. \r\n                \/\/We don\u2019t want to be stuck with a deadlock now. \r\n                receivemsg=in.readUTF();\r\n                System.out.println(\"Server Says: \"+receivemsg);\r\n                System.out.println(\"Please enter your message\");\r\n                sendmesg=sc.nextLine();\r\n\r\n                out.writeUTF(sendmesg);\r\n                out.flush();\r\n                if(receivemsg.equals(\"kill\")||sendmesg.equals(\"kill\"))\r\n                {\r\n                    System.out.println(\"Someone broke the conversation!\");\r\n                    break;\r\n                }\r\n\r\n\r\n            }\r\n            sc.close();\r\n            out.close();\r\n            in.close();\r\n            \r\n            clientsock.close();\r\n\r\n\r\n        } catch (Exception e) {\r\n            \/\/TODO: handle exception\r\n            System.out.println(\"There is an error in the system!\");\r\n            System.out.println(e.getMessage());\r\n\r\n        }      \r\n    }\r\n}\r\n\r\n<\/pre>\n<p>Now when we start the program we see that the Client messages the server. This is to start a basic conversation.<\/p>\n<p>If both the client and the server waited for a hello message, it would have been difficult to proceed with the connection.<\/p>\n<p>Now let us look at the conversation we had in between the Client and the Server.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image2-15.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84902\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image2-15.png\" alt=\"Java socket programming\" width=\"1302\" height=\"597\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image2-15.png 1302w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image2-15-300x138.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image2-15-1024x470.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image2-15-150x69.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image2-15-768x352.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image2-15-980x450.png 980w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image2-15-720x330.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image2-15-520x238.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/image2-15-320x147.png 320w\" sizes=\"auto, (max-width: 1302px) 100vw, 1302px\" \/><\/a><\/p>\n<h3>Summary<\/h3>\n<p>Socket programming is just scratching the surface!<\/p>\n<p>We can do a plethora of things with sockets and Java.<\/p>\n<p>We learned about the Socket Class, Server Socket class, how to do one-way connections and duplex connections as well!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This topic may seem intimidating at first but once you understand the nitty-gritty details about it, it is all going to be very easy. Very soon you will be able to run your own&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":84898,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[4196,6366,7682,7683,12974,12975],"class_list":["post-12763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-establishing-java-socket-connection","tag-how-to-use-java-socket-programming","tag-java-socket-client-example","tag-java-socket-programming","tag-socket-programming-in-java-source-code","tag-socket-programming-in-java-with-example"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Socket Programming in Java - Established Java Socket Connection - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what is Socket Programming in Java, established Java Socket Connection, Java Socket Client Example, Java Client Side Programming\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/\" \/>\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 - Established Java Socket Connection - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what is Socket Programming in Java, established Java Socket Connection, Java Socket Client Example, Java Client Side Programming\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-08T11:05:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-30T09:31:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Java-Socket-Programming.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Socket Programming in Java - Established Java Socket Connection - DataFlair","description":"Learn what is Socket Programming in Java, established Java Socket Connection, Java Socket Client Example, Java Client Side Programming","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:\/\/data-flair.training\/blogs\/socket-programming-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Socket Programming in Java - Established Java Socket Connection - DataFlair","og_description":"Learn what is Socket Programming in Java, established Java Socket Connection, Java Socket Client Example, Java Client Side Programming","og_url":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-04-08T11:05:06+00:00","article_modified_time":"2026-05-30T09:31:47+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Java-Socket-Programming.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Socket Programming in Java &#8211; Established Java Socket Connection","datePublished":"2018-04-08T11:05:06+00:00","dateModified":"2026-05-30T09:31:47+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/"},"wordCount":880,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Java-Socket-Programming.jpg","keywords":["Establishing Java Socket connection","how to use Java Socket Programming","Java Socket Client Example","Java Socket Programming","Socket Programming in Java source code","Socket Programming in Java with example"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/","url":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/","name":"Socket Programming in Java - Established Java Socket Connection - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Java-Socket-Programming.jpg","datePublished":"2018-04-08T11:05:06+00:00","dateModified":"2026-05-30T09:31:47+00:00","description":"Learn what is Socket Programming in Java, established Java Socket Connection, Java Socket Client Example, Java Client Side Programming","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Java-Socket-Programming.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Java-Socket-Programming.jpg","width":1200,"height":628,"caption":"Socket Programming in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/socket-programming-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Socket Programming in Java &#8211; Established Java Socket Connection"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=12763"}],"version-history":[{"count":9,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12763\/revisions"}],"predecessor-version":[{"id":148497,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12763\/revisions\/148497"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/84898"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=12763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=12763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=12763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}