Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

InputStreams .. ObjectInputStreams ...

0 views
Skip to first unread message

ash

unread,
Jul 13, 1998, 3:00:00 AM7/13/98
to
Hi all,
I am trying to write a client/server program. The server can
be accessed by two kinds of clients. One of the clients sends a Java
object and the other s ends a regular stream of characters. Now, these
regular stream of characters have a particular grammar to it. Thus, I
can write lex/yacc specification for the JavaCC tool. It generates a
whole bunch of classes for Exceptions/Errors/Tokens ...etc. And I have a
Decode function in this class which parses the incoming stream. And it
throws a ParseException, TokenMgrError if it is unable to parse it.

Thus, in my program I listen on a particular port for a connection.
And, after accepting a connection I get the input and output
streams. Now, control goes to the read function, where I instantiate an
object of BufferedInputStream and I mark the number of available number
of bytes. Then I try reading the HTTPRequest object (generated by the
JavaCC tool) and if an exception is thrown, then I reset the
BufferedInputStream and instantiate an object of type ObjectInputStream
from the InputStream. At this point I get a

java.io.StreamCorruptedException: Input does not contain a serialized
object
at
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java)
at ObjServ.ReadObject(ObjServ.java:71)
at ObjServ.main(ObjServ.java:92).

Does anyone have any ideas on this or why this is happenning ? I feel
that I should have my own buffered input stream which contains the
funtions for reading objects as well as other kinds of object.

Any input/ideas will be greatly appreciated.

Thankx a lot in advance....I really appreciate this ..
- Ash

here is the code for the Server ...


/* ObjServ.java
Server for testing out whether Objects can be transferred
b/w client and server of different types
*/

import java.io.*;
import java.net.*;
import java.lang.*;
import HTTPRequest.*;

class ObjServ {

public InputStream m_is;
public OutputStream m_os;
ServerSocket serv_sock = null;
Socket sock = null;

public Object m_obj;

public ObjServ () {

try {
serv_sock =new ServerSocket (12345);
sock = serv_sock.accept ();

m_is = sock.getInputStream ();
m_os = sock.getOutputStream ();
m_obj = new Object ();
}
catch (IOException e) {
e.printStackTrace ();
}
}

// destructor
public void finalize () {
try {
m_is.close ();
m_os.close ();
}
catch (IOException e) {
e.printStackTrace ();
}
}
// reads an object and prints out the class of the object printed
void ReadObject () {
BufferedInputStream bis = new BufferedInputStream (m_is);

try {
System.out.println ("Before mark available " + bis.available ());
bis.mark (bis.available ());
HTTPRequest http_req = new HTTPRequest (bis);
System.out.println ("Before HTTPRequest.Decode () " +
bis.available ());
http_req.Decode ();
bis.reset ();
bis.skip (bis.available());
System.out.println ("HTTPRequest read");
}
catch (IOException e1) {
e1.printStackTrace ();
}
catch (ParseException e) {
e.printStackTrace ();
}
catch (TokenMgrError e) {

try {
System.out.println ("Before reset available " + bis.available
());
bis.reset ();
ObjectInputStream obj_is = new ObjectInputStream (m_is);

m_obj = obj_is.readObject ();
System.out.println ("Read an object of class " + m_obj.getClass
());
obj_is = null;
}
catch (ClassNotFoundException e2) {
e2.printStackTrace ();
}
catch (IOException e1) {
e1.printStackTrace ();
}

}
}

public static void main (String args[]) {

ObjServ serv = new ObjServ ();

serv.ReadObject ();

}
}


Here is the Client portion of the code :

/* ObjClient.java
Client for the ObjServ class
*/

import java.io.*;
import java.net.*;
import java.lang.*;

class ObjClient {

public Socket sock;
public InputStream m_is;
public OutputStream m_os;
public Object obj;

public ObjClient () {
String host = "pollux.cs.uga.edu";

try {
sock = new Socket (host, 12345);

m_os = sock.getOutputStream ();
m_is = sock.getInputStream ();
obj = new Object ();
}
catch (IOException e) {
e.printStackTrace ();
}
}

public void finalize () {
try {
m_os.close ();
m_is.close ();
}
catch (IOException e) {
e.printStackTrace ();
}
}

public void WriteObject () {
try {
ObjectOutputStream obj_os = new ObjectOutputStream (m_os);
obj_os.writeObject (obj);
obj_os = null;
}
catch (IOException e) {
e.printStackTrace ();
}
}


public static void main (String args[]) {
ObjClient client = new ObjClient ();

client.obj = new Integer (10);
client.WriteObject ();
// client.obj = new String ("String Obj");
// client.WriteObject ();
}
}

The code for the HTTPRequest package can be obtained at the URL
http://www.cs.uga.edu/~swarup/HTTPRequest/

This directory contains all the required files.

Thankx a lot in advance for ur help.....

Alan Blackwell

unread,
Jul 13, 1998, 3:00:00 AM7/13/98
to ash
Hi Ash,

I did not review your code thoroughly but I do have advice on fixing
your problem. In your code you attempt to use the ObjectInputStream
and ObjectOutputStream instances with an instance of Object. This is
not possible because the Object class does not implement the Serialization
interface. Thus, the solution is to create a subclass of Object that implements
serialization.

Alan Blackwell

--------------------------------------

vcard.vcf
0 new messages