0

in server, I have send a byte array to client through java socket

byte[] message = ... ;

DataOutputStream dout = new DataOutputStream(client.getOutputStream());
dout.write(message);

How can I receive this byte array from client? anyone give me some code example to do this thanks in advance

flag

3 Answers

1

First, do not use DataOutputStream unless it’s really necessary. Second:

Socket socket = new Socket("host", port);
OutputStream socketOutputStream = socket.getOutputStream();
socketOutputStream.write(message);

Of course this lacks any error checking but this should get you going. The JDK API Javadoc is your friend and can help you a lot.

link|flag
0

When i was dealing with sockets my guide was Sun itself. I wrote a chat application with java. You may see my source code here (sorry but it's Turkish) and download the netbeans project here.

link|flag
0

There is a JDK socket tutorial here, which covers both the server and client end. That looks exactly like what you want.

(from that tutorial) This sets up to read from an echo server:

    echoSocket = new Socket("taranis", 7);
    out = new PrintWriter(echoSocket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(
                                echoSocket.getInputStream()));

taking a stream of bytes and converts to strings via the reader and using a default encoding (not advisable, normally).

Error handling and closing sockets/streams omitted from the above, but check the tutorial.

link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.