?
Solved

Create a packet in java Socket programming

Posted on 2003-03-22
6
Medium Priority
?
372 Views
Last Modified: 2010-03-31
Thanks for ur help
please help me again.I want to make a packet in java.The first four bytes header of packet must contain the size of the packet and the rest is data.How can i make it.
thanks
0
Comment
Question by:BijuAbraham
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8187891
If you are using a TCP socket, you don't control the packets.  They are handled at a lower layer.  If you are using UDP, you want to create a DatagramPacket.  

public class MyProtocolPacket
{
  MyProtocolPacket(byte data[])
  {
    //create new array with size = data.length + 4;
    //populate first four bytes
    packet = new DatagramPacket(newArray);
  }
 
  public getPacket() { return packet; }
  private DatagramPacket packet;
}

cheers,
brian
0
 

Author Comment

by:BijuAbraham
ID: 8192898
in first four bytes .I want to store the size of packet four using my own custom protocol.Means first four bytes cotains the size of packet
ex.

     e.g. 0500HELLO
i have to make packet in this form.
can you help me to make code in this form
thanks for your interest brian

0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8193212
Assuming the 0500 are ascii characters,

DecimalFormat formatter = new DecimalFormat("0000");
String text = formatter.format(500);
for (int i=0; i<4; i++)
{
  dataBuffer[i] = (byte)text.charAt(i);
}

brian
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Expert Comment

by:Mayank S
ID: 8193378
If you make a DatagramPacket, then you don't need to store the size in the first 4 bytes. The size can be retrieved directly using the getLength () method. A DatagramPacket can be constructed using one of its 4 constructors:

DatagramPacket ( byte data[], int size )
DatagramPacket ( byte data[], int offset, int size )
DatagramPacket ( byte data[], int size, InetAddress ipAddress, int port )
DatagramPacket ( byte data[], int offset, int size, InetAddress ipAddress, int port )

Mayank.
0
 

Author Comment

by:BijuAbraham
ID: 8194017
https://lists.xcf.berkeley.edu/lists/advanced-java/2001-December/018043.html
i have got one similar to what i want.It converts Intiger to byte array.So ican convert size of data(intiger) to byte.
So soory for the inconveinance.
thanks for all who find time to commenting
thank agian for ur help


0
 
LVL 2

Accepted Solution

by:
bkrahmer earned 500 total points
ID: 8200046
No inconvenience.  Just please close the post by awarding somebody with the answer.  (me?)  Also, if you would have asked the question better, (i.e. the problem at hand has nothing to do with packets.), you could have gotten a better and quicker answer.

cheers,
brian
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question