Results 1 to 2 of 2
Thread: Client servlet help please
- 10-29-2007, 04:14 PM #1
Member
- Join Date
- Oct 2007
- Posts
- 4
- Rep Power
- 0
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?
- 11-01-2007, 05:11 PM #2
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
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
-
How to run Spring Client in a Servlet
By Java Tip in forum Spring FrameworkReplies: 0Last Post: 04-02-2008, 10:33 AM -
Identify Client in Socket Client Server Application
By masadjie in forum NetworkingReplies: 1Last Post: 12-20-2007, 09:18 AM -
servlet client help for newbie
By heyhey in forum Java ServletReplies: 0Last Post: 10-29-2007, 04:25 PM -
How to run Spring Client in a Servlet
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:19 PM -
javax.servlet.ServletException: Wrapper cannot find servlet class util.t2
By osval in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 03:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks