Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

J2ME input/output stream with C# NetworkStream

843790Sep 20 2007 — edited Sep 20 2007
I've been stuck on this problem for several days already. I hope there's someone who knows C# as well and can help me. From the code below, the server works when i first try to read some bytes from the client and then perform a reply send. But when i added some more code to try to receive bytes from the client again, the whole server application goes haywire and does not even send the first reply.

The problem probably lies in the server code but maybe its something i've done wrongly in the J2ME client code. It may also be because there's output and input stream in J2ME while there is only a network stream in C#. Thanks so much for your help!

Server Code in C#:*

TcpListener myList = new TcpListener(ipAd,13000);
myList.Start();
Socket s = myList.AcceptSocket();
NetworkStream ns = new NetworkStream(s);

byte[] ba = new byte[40];
ns.Read(ba, 0, ba.Length);
//..some operations with ba

byte[] myWriteBuffer = null;
myWriteBuffer = Encoding.Unicode.GetBytes("1");

ns.Write(myWriteBuffer,0,myWriteBuffer.Length);
ns.flush();

byte[] bb = new byte[40]
ns.Read(bb,0,bb.Length);
//.. some operations with bb

s.Close();
myList.Stop();


Client Code in J2ME:*_

String name = "socket://" + IPAddress + ":" + 13000;
sc = (StreamConnection)Connector.open(name,Connector.READ_WRITE);

try{
os = sc.openOutputStream();

StringBuffer sb = new StringBuffer();
byte[] data = textbox1.getBytes();
os.write(data);
os.close;

is = sc.openInputStream();
byte[] incoming = new byte[5];

while (is.read(incoming)!= -1) {
//do something to the byte
}

if(is !=null){ // This statement may be the problem
is.close(); // because when i remove the if statement
is = null; // and just close the stream, an exception occurs
} // but i do not understand why

os = sc.openOutputStream();
byte[] data2 = textbox2.getBytes();
os.write(data2);

os.close();
}

Anyone know if its because i'm using the wrong type of streams? Thx once more for the help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 18 2007
Added on Sep 20 2007
1 comment
816 views