views:

4182

answers:

3

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

+1  A: 

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.

Bombe
A: 

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.

Ahmet Kakıcı
A: 

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.

Brian Agnew