Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

a question about socket and arrays !

Thread view: 
wutongjoe@hotmail.com - 22 Apr 2004 11:47 GMT
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
size 256.I used a while loop to see whether the next byte is a -1,if it
is then stop .

            int c,j=0;
            mByteData=new byte[256];
            while ((c=mByteSource.read())!= -1&&j<256){
                mByteData[j]=(byte)c;
               j++;
            }

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 ?)

Q2 : if I copy the array byte after byte using a for loop like

       
        byte[] b2=new byte[256];
        for(int i=0;i<256;i++){
               b2[i]=b1[i];
       
        }

        how can I write bytes to fileB and make fileA and fileB identical?
(MD5 sum)
        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.

---------------------------------------------------------------------------
Question about socket:

I am going to send packets to the server using lots of threads(one
packet per thread) and what should the server do? does it listen with
same amount of socket threads and then  use them to send back the
acknowledgments? or just one socket listening on all coming packets ?
(after received,I will write them to a file,so it is like a uploading app)

Am I clear enough ?
Daniel Sjöblom - 22 Apr 2004 17:51 GMT
> 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.