How can read and write byte in socket program

Hi,i am developing a chat application .Here i want to transfer the message in byte array format and received message in bytearray format .how is it possible.
[171 byte] By [Mani_csa] at [2007-10-3 3:20:55]
# 1

> Hi,

>

> i am developing a chat application .Here i want

> to transfer the message in byte array format and

> received message in bytearray format .how is it

> possible.

1 open sockets,

2 send byte arrays,

3 receive byte arrays.

.4.

5profit

ScarletPimpernela at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 2
I can get InputStream and output Stream object.how can i send through this things
Mani_csa at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 3
write(byte[] b) read(byte[] b)
ScarletPimpernela at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 4
you told like this.InputStream in=socket.getInputStream();in.read(byte[]);It's make null pointer exception.while i am starting the applicationMessage was edited by: Mani_cs
Mani_csa at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 5

> you told like this.

>

>InputStream in=socket.getInputStream();

> n.read(byte[]);

>

> It's make null pointer exception.while i am starting

> the application

>

> Message was edited by:

> Mani_cs

What is making a null pointer exception?

ScarletPimpernela at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 6

when client is connect to server the receive thread will be run.

public void run() {

byte[] message=null;

try {

while (true) {

din.read(message);

if(message.length > 0) {

StyledDocument receiveDoc=getMessage(message);

setMessage(receiveDoc);

message=null;

}

}

}catch( IOException ie ) { System.out.println( ie ); }

}

Here read methods makes null pointer exception

Mani_csa at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 7
whats din ?
ScarletPimpernela at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 8
din = socket.getInputStream();
Mani_csa at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 9
where does it come from? how does it get to your run method?
ScarletPimpernela at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 10
i am decalre as global variable.if i am usind readUTF it's working correct.But i want to transfer a style document.So i convert StyleDocument into ByteArray then transfer through stream.
Mani_csa at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 11
Jebus!it was staring straight at mebyte[] message=null; is nulling the message reference, not its contents.its message causing the NPE not the inputstream!
ScarletPimpernela at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 12
okey i will check
Mani_csa at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 13

Quote from read(byte[] b) method if InputStream in API

If b is null, a NullPointerException is thrown. If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

ScarletPimpernela at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 14
It's not working.
Mani_csa at 2007-7-14 21:13:08 > top of Java-index,Java Essentials,Java Programming...
# 15
> It's not working.define
ScarletPimpernela at 2007-7-21 10:09:06 > top of Java-index,Java Essentials,Java Programming...
# 16
After client connect to server.Thread will be run.So that time client cannot send message.surely it will be null.when they client send message it will not be nullMessage was edited by: Mani_cs
Mani_csa at 2007-7-21 10:09:06 > top of Java-index,Java Essentials,Java Programming...
# 17

> After client connect to server.Thread will be run.So

> that time client cannot send message.surely it will

> be null.when they client send message it will not be

> null

>

> Message was edited by:

> Mani_cs

what will and will not be null?

ScarletPimpernela at 2007-7-21 10:09:06 > top of Java-index,Java Essentials,Java Programming...
# 18
That previous code run in client machine.when client connect to server.That thread will be run.inputstream object read data from client socket.That time message should be null because now only the connect to server.
Mani_csa at 2007-7-21 10:09:06 > top of Java-index,Java Essentials,Java Programming...
# 19
I think you are confusing an empty byte array with null.An null array is like any null object, message has to point to a byte array in order for the read method to populate it.
ScarletPimpernela at 2007-7-21 10:09:06 > top of Java-index,Java Essentials,Java Programming...
# 20
Did you understand my pblm or not.When client connect to server client Thread is started to listen receive message.if client is send any message then read it and display otherwise it's listen receive message.
Mani_csa at 2007-7-21 10:09:06 > top of Java-index,Java Essentials,Java Programming...
# 21

> Did you understand my pblm or not.

Do you

>

>

> When client connect to server client Thread is

> started to listen receive message.if client is send

> any message then read it and display otherwise it's

> listen receive message.

So what part of this is not working, where is it not working and in what way is it not working

PLEASE learn how to define a problem with sufficient information.

saying "it is not working" is not good enough

ScarletPimpernela at 2007-7-21 10:09:06 > top of Java-index,Java Essentials,Java Programming...
# 22

Actually what is chatting program

1.server starts to listen client;

2.if any client starts application that is connected server.

3.Client side one Thread is continuously running to receive the message if it's come or not.

4.if any message you want sent you write client socket output stream.Then server readsmessage from the client socket inputstream.In server side one thread is running to listen any message to transfer.if any message it's come it will be send to other client otherwise no message in the client inputstream but here server thread is continuouslyrunning it's read the stream, that no message in the stream so it's throw null pointer exception.

Mani_csa at 2007-7-21 10:09:06 > top of Java-index,Java Essentials,Java Programming...
# 23

>it's read the stream, that no message in

> the stream so it's throw null pointer exception.

NO how many ******* times do I have to tell you. You are passing a null byte array which is causing the null pointer exception,

If you bothered to read the API you would have found that read() blocks until data is available.

ScarletPimpernela at 2007-7-21 10:09:06 > top of Java-index,Java Essentials,Java Programming...
# 24
i am not excepting this word.Don't be tension.I wil solve it. okey
Mani_csa at 2007-7-21 10:09:06 > top of Java-index,Java Essentials,Java Programming...