{"id":843,"date":"2010-03-31T09:30:19","date_gmt":"2010-03-31T08:30:19","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=843"},"modified":"2010-03-31T09:30:19","modified_gmt":"2010-03-31T08:30:19","slug":"udp-server","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/31\/udp-server\/","title":{"rendered":"UDP Server"},"content":{"rendered":"<p>As from the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/03\/31\/udp-client\/\">UDP Client<\/a> here is the UDP Server, it is similar to the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/03\/30\/tcp-server\/\">TCP Server<\/a> in that it makes connections with a remote client and sends and receives data, but once again this data is not checked to make sure that it has been received (the &#8220;fire and forget&#8221; setup again)<\/p>\n<p>So to start with we need to create a server socket for the clients to connect to<\/p>\n<pre lang=\"java\">\r\nDatagramSocket theSocket = new DatagramSocket(9999);\r\n<\/pre>\n<p>since we now have a place for the client to connect to we now need to wait for a client connection request<\/p>\n<pre lang=\"java\">\r\n\/\/ create a place for the client to send data too\r\ntheRecievedPacket = new DatagramPacket(inBuffer, inBuffer.length);\r\n\/\/ wait for a client to request a connection\r\ntheSocket.receive(theRecievedPacket);\r\n<\/pre>\n<p>once a client has requested some data, we can start the process of setting up the response, the response starts of with getting there details, there IP address and also the port number that they wish to talk on ( image attached below of the <a href=\"http:\/\/www.wireshark.org\/\">wireshark<\/a> request and different port number)<\/p>\n<pre lang=\"java\">\r\n\/\/ get the client details\r\nclientAddress = theRecievedPacket.getAddress();\r\nclientPort = theRecievedPacket.getPort();\r\n<\/pre>\n<p>and then just build up the response to send back and send it<\/p>\n<pre lang=\"java\">\r\nString message = \"Server - client sent : \" + new String(theRecievedPacket.getData(),0, theRecievedPacket.getLength());\r\noutBuffer = message.getBytes();\r\n\t\t\t\r\n\/\/ send some data to the client\r\ntheSendPacket = new DatagramPacket(outBuffer, outBuffer.length, clientAddress, clientPort);\r\ntheSocket.send(theSendPacket);\r\n<\/pre>\n<p>here is the full java code <\/p>\n<pre lang=\"java\">\r\nimport java.io.*;\r\nimport java.net.*;\r\n\r\npublic class UDPServer {\r\n\t\r\n\tDatagramSocket theSocket = null;\r\n\tint serverPort = 9999;\r\n\t\r\n\tpublic UDPServer()\r\n\t{\r\n\t\ttry {\r\n\t\t\t\/\/ create the server UDP end point\r\n\t\t\ttheSocket = new DatagramSocket(serverPort);\r\n\r\n\t\t\tSystem.out.println(\"UDP Socket (end point) 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}\r\n\t\r\n\tpublic void clientRequest()\r\n\t{\r\n\t\tDatagramPacket theRecievedPacket;\r\n\t\tDatagramPacket theSendPacket;\r\n\t\tInetAddress clientAddress;\r\n\t\tint clientPort;\r\n\t\tbyte[] outBuffer;\r\n\t\tbyte[] inBuffer;\r\n\t\t\r\n\t\t\/\/ create some space for the text to send and recieve data \r\n\t\toutBuffer = new byte[500];\r\n\t\tinBuffer = new byte[50];\r\n\t\t\r\n\t\ttry {\r\n\t\t\t\/\/ create a place for the client to send data too\r\n\t\t\ttheRecievedPacket = new DatagramPacket(inBuffer, inBuffer.length);\r\n\t\t\t\/\/ wait for a client to request a connection\r\n\t\t\ttheSocket.receive(theRecievedPacket);\r\n\t\t\tSystem.out.println(\"Client connected\");\r\n\t\t\t\r\n\t\t\t\/\/ get the client details\r\n\t\t\tclientAddress = theRecievedPacket.getAddress();\r\n\t\t\tclientPort = theRecievedPacket.getPort();\r\n\t\t\t\r\n\t\t\tString message = \"Server - client sent : \" + new String(theRecievedPacket.getData(),0, theRecievedPacket.getLength());\r\n\t\t\toutBuffer = message.getBytes();\r\n\t\t\t\r\n\t\t\tSystem.out.println(\"Client data sent (\"+message+\")\");\r\n\t\t\t\/\/ send some data to the client\r\n\t\t\ttheSendPacket = new DatagramPacket(outBuffer, outBuffer.length, clientAddress, clientPort);\r\n\t\t\ttheSocket.send(theSendPacket);\r\n\t\t\t\r\n\t\t} catch (IOException ExceIO)\r\n\t\t{\r\n\t\t\tSystem.out.println(\"Error with client request : \"+ExceIO.getMessage());\r\n\t\t}\r\n\t\t\/\/ close the server socket\r\n\t\ttheSocket.close();\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args)\r\n\t{\r\n\t\tUDPServer theServer = new UDPServer();\r\n\t\ttheServer.clientRequest();\r\n\t}\r\n}\r\n<\/pre>\n<p>if you save that as UDPServer.java and then compile and run<\/p>\n<pre lang=\"bash\">\r\njavac UDPServer.java\r\n<\/pre>\n<p>and then run the java UDPserver program, I have inserted the &#8220;&#8212; waiting for the client to connect&#8221; line because that is where the server will stop waiting, once the client as connected the rest of the output will be outputted.<\/p>\n<pre lang=\"bash\">\r\njava UDPServer \r\nUDP Socket (end point) created\r\n---- waiting for the client to connect\r\nClient connected\r\nClient data sent (Server - client sent : genux)\r\n<\/pre>\n<p>and then run the UDPClient to connect to the server<\/p>\n<pre lang=\"bash\">\r\njava UDPClient \r\nClient socket created\r\nMessage sending is : genux\r\nClient - server response : Server - client sent : genux\r\n<\/pre>\n<p>Here is the image of a wireshark connection request over the UDP and with the clients port request number.<\/p>\n<figure id=\"attachment_844\" aria-describedby=\"caption-attachment-844\" style=\"width: 877px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/03\/wireshark.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/03\/wireshark.png\" alt=\"Request port number\" title=\"Wireshark - UDP Client\" width=\"877\" height=\"225\" class=\"size-full wp-image-844\" srcset=\"https:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/03\/wireshark.png 877w, https:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/03\/wireshark-300x76.png 300w\" sizes=\"auto, (max-width: 877px) 100vw, 877px\" \/><\/a><figcaption id=\"caption-attachment-844\" class=\"wp-caption-text\">Request port number<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>As from the UDP Client here is the UDP Server, it is similar to the TCP Server in that it makes connections with a remote client and sends and receives data, but once again this data is not checked to make sure that it has been received (the &#8220;fire and forget&#8221; setup again) So to &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/31\/udp-server\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">UDP 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":[166,168,163,167,169],"class_list":["post-843","post","type-post","status-publish","format-standard","hentry","category-java","tag-datagrampacket","tag-datagramsocket","tag-server","tag-udp","tag-wireshark"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/843","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=843"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/843\/revisions"}],"predecessor-version":[{"id":845,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/843\/revisions\/845"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}