{"id":837,"date":"2010-03-30T11:48:25","date_gmt":"2010-03-30T10:48:25","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=837"},"modified":"2010-03-30T11:48:25","modified_gmt":"2010-03-30T10:48:25","slug":"tcp-server","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/30\/tcp-server\/","title":{"rendered":"TCP Server"},"content":{"rendered":"<p>As a follow on from the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/03\/29\/internet-address-ip\/\">IP Address<\/a> in java, the next thing would be a server\/client over the internet.  This is a small demo on how to create <a href=\"http:\/\/en.wikipedia.org\/wiki\/Transmission_Control_Protocol\">TCP<\/a> connections over IP Address (TCP basically means that the data sent over the connection will fully be sent, e.g. a linux cd image), shall do one in a <a href=\"http:\/\/en.wikipedia.org\/wiki\/User_Datagram_Protocol\">UDP<\/a> which is more of a send data and do not really care if data is sent across the network like radio, if you do miss part of a song it does not really matter.<\/p>\n<p>So to start with, I am using the port number 9999, lets say that you are in a office with different phones running on different extension numbers but using the main telephone number to make calls on, so in this setup the IP address would be main office telephone number (one number to talk on) and the port number would be the different telephones extensions that you can talk on.<\/p>\n<p>To create a server socket you need to pass in the port number as well.<\/p>\n<pre lang=\"java\">\r\nServerSocket theServerSocket = new ServerSocket(9999);\r\n<\/pre>\n<p>then to just wait for a connection to try and connect you <\/p>\n<pre lang=\"java\">\r\nSocket sock = theServerSocket.accept();\r\n<\/pre>\n<p>which waits for a client connection (accept) , since the socket also has the clients IP address you can use that to make sure that you want to accept connections from that IP address if you wanted to, but to output the InetAddress you could use the &#8220;sock&#8221; from the above code<\/p>\n<pre lang=\"java\">\r\nSystem.out.println(\"The client IP address is \" + sock.getInetAddress());\r\n<\/pre>\n<p>the only other thing is that you can send data to the client which using a ObjectOutputStream with passing in the &#8220;sock&#8221; from the above code<\/p>\n<pre lang=\"java\">\r\nObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());\r\noos.writeChars(\"Hi from the server\");\r\noos.close();\r\n<\/pre>\n<p>Here is the full code <\/p>\n<pre lang=\"java\">\r\nimport java.io.IOException;\r\nimport java.io.ObjectOutputStream;\r\nimport java.net.InetAddress;\r\nimport java.net.ServerSocket;\r\nimport java.net.Socket;\r\n\r\npublic class TCPServer {\r\n\t\/\/ the IP class as such\r\n\tInetAddress theTCPServer;\r\n\t\/\/ port to talk over\r\n\tint portNumber = 9999;\r\n\t\/\/ the socket for the communication to happen on\r\n\tServerSocket theServerSocket;\r\n\t\r\n\t\/* \r\n\t * Setup the server socket communication \r\n\t *\/\r\n\tpublic TCPServer() {\r\n\t\ttry {\r\n\t\t\t\/\/ create the server socket on the port number\r\n\t\t\ttheServerSocket = new ServerSocket(portNumber);\r\n\t\t\tSystem.out.println(\"Server created on port : \"+portNumber);\r\n\t\t} catch (IOException ExecIO)\r\n\t\t{\r\n\t\t\tSystem.out.println(\"Error creating the server socket : \"+ExecIO.getMessage());\r\n\t\t}\r\n\t}\r\n\t\r\n\t\r\n\tpublic void connections()\r\n\t{\r\n\t\ttry {\r\n\t\t\t\/\/ accept a connection \r\n\t\t\tSocket sock = theServerSocket.accept();\r\n\t\t\tSystem.out.println(\"Server accepted connection, send to handler\");\r\n\t\t\t\r\n\t\t\t\/\/ print out the clients IP Address\r\n\t\t\tSystem.out.println(\"The client IP address is \" + sock.getInetAddress());\r\n\t\t\t\r\n\t\t\t\/\/ send the message to the client\r\n\t\t\tObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());\r\n\t\t\tSystem.out.println(\"Server socket opened\");\r\n\t\t\toos.writeChars(\"Hi from the server\");\r\n\t\t\toos.close();\r\n\t\t\t\r\n\t\t\t\/\/ close the socket\r\n\t\t\tsock.close();\r\n\t\t\t\r\n\t\t} catch  (IOException ExecIO)\r\n\t\t{\r\n\t\t\tSystem.out.println(\"Error creating connection : \"+ExecIO.getMessage());\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\tTCPServer theServer = new TCPServer();\r\n\t\ttheServer.connections();\r\n\t}\r\n}\r\n<\/pre>\n<p>save as TCPServer.java and then to compile, and run<\/p>\n<pre lang=\"bash\">\r\njavac TCPServer.java\r\njava TCPServer\r\n<\/pre>\n<p>and the output would be something like <\/p>\n<pre lang=\"bash\">\r\nServer created on port : 9999\r\nServer accepted connection, send to handler\r\nThe client address is \/127.0.1.1\r\nServer socket opened\r\n<\/pre>\n<p>you could use <a href=\"http:\/\/www.wireshark.org\/\">wireshark<\/a> to watch what is happening.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a follow on from the IP Address in java, the next thing would be a server\/client over the internet. This is a small demo on how to create TCP connections over IP Address (TCP basically means that the data sent over the connection will fully be sent, e.g. a linux cd image), shall do &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/30\/tcp-server\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">TCP Server<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[159,158,163,161,160,162],"class_list":["post-837","post","type-post","status-publish","format-standard","hentry","category-java","tag-getinetaddress","tag-objectoutputstream","tag-server","tag-serversocket","tag-socket","tag-tcp"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=837"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/837\/revisions"}],"predecessor-version":[{"id":838,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/837\/revisions\/838"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}