Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Network Connection between J2SE and J2ME Rate Topic: -----

#1 chris.tkd  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 26-September 06


Dream Kudos: 25

Share |

Network Connection between J2SE and J2ME

Posted 17 December 2008 - 09:37 AM

Hi,

Im writing an application to send data to/from mobile to pc.

Im a little stuck when trying to actually send data from the mobile(J2ME) to the pc(J2ME).

Im not sure what im doing wrong, but im getting the error

java.io.StreamCorruptedException: invalid stream header

on the server(J2SE) side every time.



The client (J2ME) code is


//create connection to server
StreamConnection connection = (StreamConnection) Connector.open("socket://localhost:2000", Connector.READ_WRITE);
			
//build streams
 DataOutputStream os = connection.openDataOutputStream();
			
//write data
int t = 100;
os.writeInt(t.toString());
os.flush();

...



The server (J2SE) is

ServerSocket serverSocket = new ServerSocket(2000);

Socket client = serverSocket.accept();

ObjectInputStream serverInputStream = new ObjectInputStream(client.getInputStream()); 

int height = serverInputStream.readInt();



Can anyone see what im doing wrong?

Thanks,
Chris.
Was This Post Helpful? 0
  • +
  • -


#2 bcklzz  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-December 08


Dream Kudos: 0

Re: Network Connection between J2SE and J2ME

Posted 17 December 2008 - 02:32 PM

Hi Chris

Im not Java expert but lookin at your code you have in the J2ME side a DataOutputStream
whereas in the J2SE you have a ObjectInputStream
the Exception StreamCorruptedException is thrown if the stream header is incorrect
Acording to tha Java documentation in the ObjectInputStream constructor "a serialization stream header is read from the stream and verified" also "This constructor will block until the corresponding ObjectOutputStream has written and flushed the header" so if you are using a DataOutputStream you dont have that header.

you should use a DataInputStream in the J2SE side as a "data input stream lets an application read primitive Java data types from an underlying input stream[...]. An application uses a data output stream to write data that can later be read by a data input stream"

i hope this can Help :D

Cheers
Was This Post Helpful? 0
  • +
  • -

#3 chris.tkd  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 26-September 06


Dream Kudos: 25

Re: Network Connection between J2SE and J2ME

Posted 17 December 2008 - 02:44 PM

Yep I managed to figure that out eventually!

To get it to work i used a

DataOutputStream os = connection.openDataOutputStream(); // J2ME client

and a

DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream())); //J2SE Server

I can now send pictures from my mobile to my PC :)
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 1997
  • View blog
  • Posts: 13,284
  • Joined: 06-March 08


Dream Kudos: 550

Re: Network Connection between J2SE and J2ME

Posted 17 December 2008 - 07:12 PM

View Postbcklzz, on 17 Dec, 2008 - 02:32 PM, said:

Hi Chris

Im not Java expert but lookin at your code you have in the J2ME side a DataOutputStream
whereas in the J2SE you have a ObjectInputStream
the Exception StreamCorruptedException is thrown if the stream header is incorrect
Acording to tha Java documentation in the ObjectInputStream constructor "a serialization stream header is read from the stream and verified" also "This constructor will block until the corresponding ObjectOutputStream has written and flushed the header" so if you are using a DataOutputStream you dont have that header.

you should use a DataInputStream in the J2SE side as a "data input stream lets an application read primitive Java data types from an underlying input stream[...]. An application uses a data output stream to write data that can later be read by a data input stream"

i hope this can Help :D

Cheers


And you are not a Java expert :D
Welcome at DIC Bcklzz :^:
:wub:
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users