Results 1 to 2 of 2
  1. #1
    heyhey is offline Member
    Join Date
    Oct 2007
    Posts
    4
    Rep Power
    0

    Default Client servlet help please

    new to java trying to create a java udp client servlet to encode data (ascii) and be recieved by server, i have tried to write some code but not sure if it is correct and if i need to declare anything at the begining:

    java.io.ObjectOutputStream
    java.io.ByteArrayOutputStream

    public static void main(String args[]){

    //creates a string for name called Help
    String s= "Help"

    //Converts name into byte sequence
    byte[] B = S.getBytes("US-ASCII");

    //Creates a string for a number
    String boot= 1234567811111111

    //Convert number into byte sequence
    byte[] B =CC;getbytes("US-ASCII");

    //create datagram socket
    aSocket = new DatagramSocket();

    //Host name
    InetAddress address = InetAddress.getByName("localhost")

    //set port number
    public function set ServerPort (3670 : int)

    //create datagram packet
    DatagramPacket request =
    new DatagramPacket(m, args[0].length(), aHost, serverPort);
    ;
    // Send it
    aSocket.send(request);

    does the code i have written convert to ascii? any problems with it?

  2. #2
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    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)

    Java 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;
    		}
    	}
    }

Similar Threads

  1. How to run Spring Client in a Servlet
    By Java Tip in forum Spring Framework
    Replies: 0
    Last Post: 04-02-2008, 10:33 AM
  2. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM
  3. servlet client help for newbie
    By heyhey in forum Java Servlet
    Replies: 0
    Last Post: 10-29-2007, 04:25 PM
  4. How to run Spring Client in a Servlet
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:19 PM
  5. Replies: 1
    Last Post: 08-07-2007, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •