View Single Post
  #2 (permalink)  
Old 11-01-2007, 07:11 PM
Turtle Turtle is offline
Member
 
Join Date: Nov 2007
Posts: 4
Turtle is on a distinguished road
Read the comments in this code.
And refer to the online documentation: Java Platform SE 6
Also, check out the networking tutorials: Trail: Custom Networking (The Java™ Tutorials)

Code:
// Imports must be preceded with the keyword import. // Each import must be terminated with a semi-colin (as with all statements). // Note, you can import all classes from a package by using an astriks... // import java.io.*; // This is not recursive though, classes in sub-packages with need to be imported too. // import java.io.ObjectOutputStream; // import java.io.ByteArrayOutputStream; import java.io.*; // Required for DatagramSocket, InetAddress & DatagramPacket // As these classes reside in this package... import java.net.*; class Test { // The getBytes method throws an UnsupportedEncodingException exception, you may want to handle this if it is dynamically set. public static void main(String args[]) throws UnsupportedEncodingException { //creates a string for name called Help // Java is case sensitive, I've changed this to a capital S. // Also, each statement must be terminated with a semi-colin. String S = "Help"; //Converts name into byte sequence byte[] B = S.getBytes("US-ASCII"); // getBytes is a instance method and must be used on an instance of an object. // Converting int to String... // The value range an integer can hold is -2^31 to 2^31-1 // That's from -2,147,483,648 to 2,147,483,647 // The max & min numbers can be found in a primative classes wrapper class... // http://java.sun.com/javase/6/docs/api/java/lang/Integer.html // Your number is within the range of a double, but for even larger numbers check out the BigInteger class. int intNumber = 50; String stringNumber = String.valueOf(intNumber); //Convert number into byte sequence B = stringNumber.getBytes("US-ASCII"); // getBytes is a instance method and must be used on an instance of an object. //create datagram socket DatagramSocket aSocket; try { aSocket = new DatagramSocket(); // Needed to declare the variable type } catch (SocketException se) { System.out.println("DataSocket can and has thrown a SocketException. Exiting application"); return; } //Host name InetAddress address; try { address = InetAddress.getByName("localhost"); // Missed a semi-colin } catch (UnknownHostException uhe) { System.out.println("InetAddress can and has thrown a UnknownHostException. Exiting application"); return; } //set port number //public function set ServerPort (3670 : int) int serverPort = 3670; //create datagram packet // m is the packet data, and should be assigned - I have switched it with B // The packet length should get the length from the data (B.length) it is sending. // I am assuming your host is the "address". DatagramPacket request = new DatagramPacket(B, B.length, address, serverPort); // Send it try { aSocket.send(request); } catch(IOException ioe) { System.out.println("The send method can and has thrown an IOException. Exiting application"); return; } } }
Reply With Quote