Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-29-2007, 06:14 PM
Member
 
Join Date: Oct 2007
Posts: 4
heyhey is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-01-2007, 07:11 PM
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; } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to run Spring Client in a Servlet Java Tip Spring Framework 0 04-02-2008 12:33 PM
Identify Client in Socket Client Server Application masadjie Networking 1 12-20-2007 11:18 AM
servlet client help for newbie heyhey Java Servlet 0 10-29-2007 06:25 PM
How to run Spring Client in a Servlet JavaBean Java Tips 0 10-04-2007 11:19 PM
javax.servlet.ServletException: Wrapper cannot find servlet class util.t2 osval Advanced Java 1 08-07-2007 05:47 PM


All times are GMT +3. The time now is 09:49 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org