Docsity

LAB UDP-NETWORK PROGRAMMING, Summaries of Network Programming

Partial preview of the text

Download LAB UDP-NETWORK PROGRAMMING and more Summaries Network Programming in PDF only on Docsity!

Send, recive on address . ] import java.net.*; import java.io.*; public class PacketSendDemo { the book 5 : public static void main (String args[]) { wnt, Vv as. (, // int arge = args.length; Os 7 p' 5 algs // Check for valid number of Parameters a ok icin (argew—asl)p{ System.out-println ("Syntax :"); \ inet System.out.println ("java PacketSendDemo hostname") ; return; System.out.println (x:"Binding to a local port"); // Create a datagram socket, bound to any available local port System.out.println ("Bound to local port "+ // Create a message to send using a UDP packet // Get the contents of our message asvan array of bytes 0) destinatien p is_ = the loop back addess.. -) soc li tion \—p Crandom port aum) @ +e prepare the pucket, i nould hone, byte alley Ot put:chaam ° . /, 4: “alls d toe att of by Yes _inctwe @) retuin tHe content of He allan Cusiny to byte attay) pyte[] barray = bout.toBytearray(); | datagram packet, containil byte arra | I ce a : W © ncgeanact pokes =o a aes Snenuery sae baray ent st SEE ee OED Looking up hostname “+ hostname ); 7/ Lookup the specified hostname, and get an InetAddress | 1 InetAddress remote_addr = InetAddress.getByName (hostname) 7 | Gystem-out.printin (*Hostnane resolved as " + remote_addr.getostaddress ()): ° 3 J // address packet to sender, | Y fp packet.setAddress (remote_addr); iH // Set port number to 2000 | I packet-setPort (2000); | / ve . Ket ember no guarantee of delivery ee sent: USING got | = S° cleet ) (onknownostException whe) ( Desi by name System.err-printin ("can't find host * +hostname)7 | catch (10Exception ice) { : CSL oo, Pusing input mtput = 2 Streams a —= = have, ipadacss = \27,0-0°] @ + a the address inside the packet LE Cy Ob9%) = Sender. 2) fe peat the host address as dotted decimal Pe ation. @) get tre Pott om te Soully sender // Display packet content @ 5 // Display only up to the length of the origina acke // for (int i=0; i < packet.getLength(); i++ // int data = bin.read()7 —_—_——___ /1 as Ad ataesena) // e L preak; // a} b Put ‘/ ve at t ( (char) data) ean”. a / socket.close(); an eng aS | catch (romxcepticn toe) } I pope oan error ya System.er +printin ("Error tiloe ms Stvea only — 2 ES Mee) @ to read) He data. , tne method retuas the bite allay ge we pet byle ators inpat stream © inpst stream reeler to ree) text 5 na @ p-fred ke pee) Lines er ez ak C C3! 02 $ UGS 1 AD!)