> hi, I just began with studying java and have a question about reading
> bytes from file(fileA).Every time I make it read into a byte array with
[quoted text clipped - 10 lines]
> Q1: what is the second array like if the file size is 270kb ?(first 14
> are bytes from file and rest are null ? or empty ? or zero ?)
First. Don't multi-post. As a newbie you should post to c.l.j.h.
I see no second array in your code. If you allocate a new array every
time you read your chunks, the remaining bytes will be set to 0. But if
you just continuosly overwrite the same array, then obviously the rest
will contain whatever values they happened to containg before. Null is a
reference and cannot be assigned to a byte.
> Q2 : if I copy the array byte after byte using a for loop like
>
[quoted text clipped - 7 lines]
> how can I write bytes to fileB and make fileA and fileB identical?
> (MD5 sum)
Uh? Just write the bytes like you would write any other bytes. See the
sun java tutorial for examples if you need them.
> because every time I found from 14th byte all the rest bytes
> are all zeros ,there is no -1 and I do not know where to stop if I
> want to write them to fileB.
Of course you have to keep track of how many bytes you are actually
using in an array. You could wrap it in a class:
class Chunk_O_Bytes
{
byte[] bytes;
int count;
}
// but don't give it such a silly name
> ---------------------------------------------------------------------------
> Question about socket:
[quoted text clipped - 3 lines]
> same amount of socket threads and then use them to send back the
> acknowledgments? or just one socket listening on all coming packets ?
There are a couple of different ways to do this.
First you must bind an adress to your socket. The adress in the internet
namespace is the machine's internet adress and the port number you wish
to use on the machine. You can't bind more than one socket per adress +
protocol.
Then you need to set your server socket to listen for incoming
connections, and accept them as the server sees fit. Accept returns new
sockets so that you can still listen on the same server socket after
accepting a connection request.
The first option is to write a single threaded server. The server
accepts incoming connections (blocking until available) and then handles
IO in the same thread. This is fairly simple, and can sometimes be enough.
The second option which is quite usual in java is to write a
multi-threaded server. One thread will listen for and accept new
connections and spawn separate threads to do IO with all involved
sockets. This is probably the easiest to program.
The third option is to use the select mechanism in the java.nio.channels
package. The API is quite hard to grok if you have no idea what it is
supposed be used for, but I'll give a short high-level explanation. In
the multi-threaded server the problem is that it is not exactly cheap to
create a huge amount of threads. By using select on a set of sockets you
can do both IO and accept connections on multiple sockets in the same
thread. To use select you register all of your sockets in the selector
set. When you call select, it will block until there is some IO to be
done on one or more of the sockets. Once there is something to do,
select will return all of the sockets with pending IO operations and
then you can process them all in one batch.

Signature
Daniel Sjöblom
Remove _NOSPAM to reply by mail