{"id":841,"date":"2010-03-31T09:11:22","date_gmt":"2010-03-31T08:11:22","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=841"},"modified":"2010-03-31T09:11:22","modified_gmt":"2010-03-31T08:11:22","slug":"udp-client","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/31\/udp-client\/","title":{"rendered":"UDP Client"},"content":{"rendered":"<p>A <a href=\"http:\/\/en.wikipedia.org\/wiki\/User_Datagram_Protocol\">UDP<\/a> Client is very similar to a <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/03\/30\/tcp-client\/\">TCP Client<\/a> in that it communicates with a server, but the main difference between the two is that UDP is more of a &#8220;fire and forget&#8221; setup than lets make sure that the data is sent across.  A good example of this would be for a UDP server\/client to be something like a radio station, when you are listening to the radio and if you miss part of the song then it does not really matter (apart from if you was singing along!!), but with TCP server\/client you want to have all of the data associated with that file.<\/p>\n<pre lang=\"java\">\r\n\/\/ create a socket to connect to the server with\r\nDatagramSocket theSocket = new DatagramSocket();\r\nint serverPort = 9999;\r\n\/\/ the remote server, in this case I am using the local host but you could use any remote server as long as the UDP server is running on it\r\nInetAddress theServer = InetAddress.getLocalHost();\r\n\/\/ and connect the socket to the remote server and port number\r\ntheSocket.connect(theServer,serverPort);\r\n<\/pre>\n<p>and now we build up a message to send to the server, we are using bytes because that is what is sent over with the DatagramPacket&#8217;s (these are the packets of data within the wrapped up &#8220;letter&#8221; that is being sent from the client to the server, the &#8220;letter&#8221; in this case is the details included within the packet e.g. address to, address from more details are <a href=\"http:\/\/en.wikipedia.org\/wiki\/User_Datagram_Protocol#Packet_structure\">here<\/a>)<\/p>\n<pre lang=\"java\">\r\nbyte[] outBuffer = new byte[50];\r\nString message = \"genux\";\r\noutBuffer = message.getBytes();\r\n<\/pre>\n<p>and then to send the information to the server we, since theSocket is already connected to the server then do not need to tell the DatagramPacket where to send.<\/p>\n<pre lang=\"java\">\r\n\/\/ build up a packet to send to the server\r\ntheSendPacket = new DatagramPacket(outBuffer, outBuffer.length);\r\n\/\/ send the data\r\ntheSocket.send(theSendPacket);\r\n<\/pre>\n<p>and now to get a response (if the server gets, and the client does receive one)<\/p>\n<pre lang=\"java\">\r\n\/\/ get the servers response within this packet\r\ntheReceivedPacket = new DatagramPacket(inBuffer, inBuffer.length);\r\ntheSocket.receive(theReceivedPacket);\r\n\t\t\t\r\n\/\/ the server response is...\r\nSystem.out.println(\"Client - server response : \"+new String(theReceivedPacket.getData(), 0, theReceivedPacket.getLength()));\r\n<\/pre>\n<p>here is the full code<\/p>\n<pre lang=\"java\">\r\nimport java.io.*;\r\nimport java.net.*;\r\n\r\npublic class UDPClient {\r\n\r\n\tDatagramSocket theSocket = null;\r\n\tint serverPort = 9999;\r\n\t\r\n\tpublic UDPClient()\r\n\t{\r\n\t\ttry {\r\n\t\t\ttheSocket = new DatagramSocket();\r\n\t\t\t\r\n\t\t\t\/\/ but if you want to connect to your remote server, then alter the theServer address below\r\n\t\t\tInetAddress theServer = InetAddress.getLocalHost();\r\n\t\t\ttheSocket.connect(theServer,serverPort);\r\n\t\t\t\r\n\t\t\tSystem.out.println(\"Client socket created\");\r\n\t\t}catch (SocketException ExceSocket)\r\n\t\t{\r\n\t\t\tSystem.out.println(\"Socket creation error  : \"+ExceSocket.getMessage());\r\n\t\t} \r\n\t\tcatch (UnknownHostException ExceHost)\r\n\t\t{\r\n\t\t\tSystem.out.println(\"Socket host unknown : \"+ExceHost.getMessage());\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic void connectToServer()\r\n\t{\r\n\t\tDatagramPacket theSendPacket;\r\n\t\tDatagramPacket theReceivedPacket;\r\n\t\tInetAddress theServerAddress;\r\n\t\tbyte[] outBuffer;\r\n\t\tbyte[] inBuffer;\r\n\t\t\r\n\t\t\/\/ the place to store the sending and receiving data\r\n\t\tinBuffer = new byte[500];\r\n\t\toutBuffer = new byte[50];\r\n\t\ttry {\r\n\t\t\tString message = \"genux\";\r\n\t\t\toutBuffer = message.getBytes();\r\n\t\t\t\r\n\t\t\tSystem.out.println(\"Message sending is : \" + message);\r\n\r\n\t\t\t\/\/ the server details\r\n\t\t\ttheServerAddress = theSocket.getLocalAddress();\r\n\t\t\t\r\n\t\t\t\/\/ build up a packet to send to the server\r\n\t\t\ttheSendPacket = new DatagramPacket(outBuffer, outBuffer.length, theServerAddress, serverPort);\r\n\t\t\t\/\/ send the data\r\n\t\t\ttheSocket.send(theSendPacket);\r\n\t\t\t\r\n\t\t\t\/\/ get the servers response within this packet\r\n\t\t\ttheReceivedPacket = new DatagramPacket(inBuffer, inBuffer.length);\r\n\t\t\ttheSocket.receive(theReceivedPacket);\r\n\t\t\t\r\n\t\t\t\/\/ the server response is...\r\n\t\t\tSystem.out.println(\"Client - server response : \"+new String(theReceivedPacket.getData(), 0, theReceivedPacket.getLength()));\r\n\t\t\ttheSocket.close();\r\n\t\t} catch (IOException ExceIO)\r\n\t\t{\r\n\t\t\tSystem.out.println(\"Client getting data error : \"+ExceIO.getMessage());\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args)\r\n\t{\r\n\t\tUDPClient theClient = new UDPClient();\r\n\t\ttheClient.connectToServer();\r\n\t}\r\n}\r\n<\/pre>\n<p>if you save that as UDPClient.java and then to compile<\/p>\n<pre lang=\"java\">\r\njavac UDPClient.java\r\n<\/pre>\n<p>the server is the next part of the puzzle, which is coming next.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A UDP Client is very similar to a TCP Client in that it communicates with a server, but the main difference between the two is that UDP is more of a &#8220;fire and forget&#8221; setup than lets make sure that the data is sent across. A good example of this would be for a UDP &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/31\/udp-client\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">UDP Client<\/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":[166,160,167],"class_list":["post-841","post","type-post","status-publish","format-standard","hentry","category-java","tag-datagrampacket","tag-socket","tag-udp"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/841","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=841"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/841\/revisions"}],"predecessor-version":[{"id":842,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/841\/revisions\/842"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}