ASN.1 module description

tried to use ASN.1 for a client server communication, but i'm not sure that i understand it correctly . The Client have to send an integer to the server, the server read it and send back a number and a String, the Client read them and send back a String the server read it and send back a String. It's something like this.
Client ---> server 1 integer

Server --> Client 1 integer
Server --> Client 1 String

Client ---> server 1 String
Server --> Client 1 String

Open in new window


I wrote the following ASN.1 module, I succeeded to generate the classes with jac asn1 but I am not sure that the description of the module are correct because the two sequence are exactly the same. I m not sure if i have to put only the variable that i will send or recive or both.



 
   Demo-module DEFINITIONS ::=       -- Module-name DEFINITIONS ::= BEGIN

    BEGIN

    Client ::= SEQUENCE {

    clientInt INTEGER ,

    serverInt INTEGER ,

    serverString BIT STRING ,

    clientString BIT STRING , serverResponse BIT STRING

    }

    Server ::= SEQUENCE {

    clientInt     INTEGER ,                 

    serverInt INTEGER ,

    serverString BIT STRING ,

    clientString BIT STRING , serverResponse BIT STRING

    }

    END -- end of module, END required

Open in new window

alexgagaAsked:
Who is Participating?
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You need to think of this in terms of what data is interchanged at each communication point. From what you are saying, you have FOUR separate communications that happen, eg. "Client 1st Request", "Server 1st Response", "Client 2nd Request", "Server 2nd Response". Hence you should have FOUR separate objects in your ASN.1 definitions, rather than the two you have above. That should be enough of a hint to get you going, but if you still have questions let us know.

PS. You probably don't want to use BIT STRING, what your after is probably more likely an IA5String.
0
 
alexgagaAuthor Commented:
Thank you for the answer


Demo-module DEFINITIONS ::=       -- Module-name DEFINITIONS ::= BEGIN

BEGIN
 
ClientFirstRequest ::= SEQUENCE {                   

   clientInt     INTEGER 

   }        

ServerFirstResponse ::= SEQUENCE {                

   serverInt    INTEGER ,        

   serverString     BIT STRING   
 
 }   

ClientSecondRequest ::= SEQUENCE {                   

   clientString     IA5String 

   }     

ServerSecondResponse ::= SEQUENCE {                

   serverResponse     IA5String 
 }   

 

END                                      -- end of module, END required

Open in new window


I still have one question :) before i was using sockets in my programm. Now it will be just the same thing ? I just code the object with ASN.1 send it via Spckets and decode it in the other side ?
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Looks alright, although you still have one more bit string left in there.

Yeah, a socket would be fine. And I assume you are talking stream sockets, ie. TCP rather than UDP, which is also fine because the ASN.1 encodings include "length" information, so you can stick in the encoded bytes and the receiving end can still decode the object out of the stream.

Are you making a new connection from the client to server between the 1st and 2nd requests that we detailed above? If not, if you are just using the one socket connection, the only thing you may need to consider, is how both ends handle their "state" correctly, ie. if something gets screwed up along the way and one end thinks that they are up to the 2nd request/response and the other end the 1st, they will be sending a different object to what is expected. You may just have to think how to handle that case properly!
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
alexgagaAuthor Commented:
Ok thank you, but my code was like this

Random rand = new Random();
int  n = rand.nextInt(50) + 1;
DataOutputStream dos = new DataOutputStream(_socket.getOutputStream());
 dos.writeInt(n);

Open in new window


I tried to make it like this
 
	int  n = rand.nextInt(50) + 1;
        ClientFirstRequest h = new ClientFirstRequest();
        h.setValue(4);
        DataOutputStream dos = new DataOutputStream(_socket.getOutputStream());
         dos.writeInt(h);

Open in new window


I tried to use setValue because in the  generated class ClientFirstRequest it say that i have to use it

package sequenceOfExample;

/*
 * Created by JAC (Java Asn1 Compiler)
 */

import com.turkcelltech.jac.*;
import com.chaosinmotion.asn1.Tag;

public class ClientFirstRequest extends Sequence
{
	/**
	 * if you want to set/fill an element below, just call the setValue(..) method over its instance.
	 *
	 * To encode/decode your object, just call encode(..) decode(..) methods.
	 * See 'TestProject.java' in the project to examine encoding/decoding examples
	 */
	public ASN1Integer clientInt = new ASN1Integer("clientInt");
	/* end of element declarations */
	
	/**
	* asn.1 SEQUENCE constructor
	*/
	public
	ClientFirstRequest()
	{
		super();
		setUpElements();
	}

	/**
	* asn.1 SEQUENCE constructor with its name
	*/
	public
	ClientFirstRequest(String name)
	{
		super(name);
		setUpElements();
	}
	

	protected void
	setUpElements()
	{
		super.addElement(clientInt);
	/* end of element setup */
	}
}

Open in new window


And one other question please, in the server, I have to create another abject FirstClientRequest to recive the object on it ?
thanx a lot
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You have accepted my other answers so I'm not sure if that means that you were able to work this out, but just case you haven't this is what I think...

You can't use a DataOutputStream as well as ASN.1 encodings. They do similar things but in very different ways and so are not related as such. You need to use the methods of the classes created by the ASN/1 compiler, JAC. (See the comments on lines 15,16 of what you posted above).

So, I think you would need to do something like this...
	int  n = rand.nextInt(50) + 1;
        ClientFirstRequest h = new ClientFirstRequest();
        h.setValue(4);
        BerOutputStream bos = new BerOutputStream(_socket.getOutputStream());
         h.encode(bos);

Open in new window

Note: you will then need to import that BerOutputStream at the top of your java file...  import com.chaosinmotion.asn1.BerOutputStream;



As for the server side, it should be fairly similar except you would be using a BerInputStream along with the "decode" method of the FirstClientRequest object.
0
 
alexgagaAuthor Commented:
Thank you for the answer. No I still not able but I think that it will be better if I ask the question in new subject because it have nothing to do with the module description :D I will trie it and tell you if it work
0
 
alexgagaAuthor Commented:
Thank you for the answer. No I still not able but I think that it will be better if I ask the question in new subject because it have nothing to do with the module description :D I will trie it and tell you if it work
0
 
alexgagaAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.