| Author |
Question about transfering data over sockets
|
Stole Perov
Greenhorn
Joined: Feb 08, 2010
Posts: 12
|
|
Hello all.
I would really appreciate some help on the following matter:
First of all, here is the code:
So, i create a scoket, and then i fill the byte array (data) and then send it over to the other socket. So, my questions is, how to create the Socket receiver to know that he will be receiving byte array which has 100 elements?
Tnx in advance.
(wonderfull forum btw)
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
You may need to create a ServerSocket which will be the server-side of your communication.
Have a look at the Sun Tutorial.
|
Author of ExamLab (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
... and welcome to JavaRanch Stole
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Is that data text or binary? If it is binary, don't use a PrintWriter, use a PrintStream instead. Although using the output stream directly will also work; use the write methods, combined with flush():
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Stole Perov
Greenhorn
Joined: Feb 08, 2010
Posts: 12
|
|
Fist, tnx for the answers.
The data is binary code, a file of any kind. Also, my question was:
How to create the other part, the other client who receives this data as it is, and put it in a data array possibly.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Have you checked out the link Devaka has posted?
|
 |
Stole Perov
Greenhorn
Joined: Feb 08, 2010
Posts: 12
|
|
|
Yes i did. And, unfortunately, i was not able to find the answer that i was looking for. Would you mind pointing me in the right direction. Tnx.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Did you find ServerSocket? Did you find its accept() method, that returns another Socket?
As for the receiving end knowing what it will receive, you should tell it first. You can send one int first that indicates the number of bytes that will then follow. Then you send the array. The receiving end first reads the int, and then reads as many bytes as the int indicates.
|
 |
Stole Perov
Greenhorn
Joined: Feb 08, 2010
Posts: 12
|
|
Thanks for the prompt replay.
Now, just to clarify things a bit.
I understand how to make a Server socket, i understand how to make it wait for connections, i understand how to accept a connection and bind it to a socket. However, if you dont mind looking at my first initial post [the code part], i would really appreciate a code example of a socket that receives the data which has been sent by the first one.
If something is confusing any 1 in my request, please point it out.
Thanks again.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Well, you start by calling socket.getInputStream(). That gives you an InputStream and you can read its data using its read methods. You can use DataInputStream and DataOutputStream to make life easier for you:
|
 |
Stole Perov
Greenhorn
Joined: Feb 08, 2010
Posts: 12
|
|
So, just to make it clear that i understood.
dos.writeInt(data.length); == write to the "buffer" the value of the data array.[do not send the buffered data]
dos.write(data); == add to the "buffer" the whole data array.[do not send the buffered data]
dos.flush(); == send to the output socket the following buffered data [data.length, data[]]
so, for example, if data.length = 2 and data[0] = 1 and data[1] = 3 then the output to the outgoing buffer would be 2,1,3
right?
and, what does "int length = dos.readInt();" means. Measure the length of the received stream or read the first integer from the stream.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Stole Perov wrote:So, just to make it clear that i understood.
dos.writeInt(data.length); == write to the "buffer" the value of the data array.[do not send the buffered data]
dos.write(data); == add to the "buffer" the whole data array.[do not send the buffered data]
dos.flush(); == send to the output socket the following buffered data [data.length, data[]]
Right.
so, for example, if data.length = 2 and data[0] = 1 and data[1] = 3 then the output to the outgoing buffer would be 2,1,3
right?
Almost. An int takes up 4 bytes in size, so what is actually sent is <2 as 4 bytes>,1,3.
and, what does "int length = dos.readInt();" means. Measure the length of the received stream or read the first integer from the stream.
It reads the first 4 bytes and then converts them into an int. The reason I used writeInt and readInt is because you can't assume you need any less than 4 bytes to store the complete array size; your array can in theory have a length of Integer.MAX_VALUE and you need 4 bytes to store that value. DataOutputStream.writeInt and DataInputStream.readInt will convert a single int into 4 bytes and vice versa.
|
 |
 |
|
|
subject: Question about transfering data over sockets
|
|
|