Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about transfering data over sockets

 
Stole Perov
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
ExamLab Creator
Marshal
Pie
Posts: 4314
233
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may need to create a ServerSocket which will be the server-side of your communication.
Have a look at the Sun Tutorial.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4314
233
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to JavaRanch Stole
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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():
 
Stole Perov
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked out the link Devaka has posted?
 
Stole Perov
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic