tags:

views:

854

answers:

3

I have created a socket programming for server client communication. I am reading data using read(byte[]) of DataInputStream Also writing data using write(byte[]) of DataOutputStream.

Whenver I am sending small amount of data my program works fine. But If I send a data of 20000 characters and send it 10 times then i am able to recieve the data 8 times perfectly but not the 2 times.

SO can I reliably send and receieve data using read and write in socket programming.

Thanks Bapi

+6  A: 

My guess is that you're issuing a single call to read() and assuming it will return all the data you asked for. Streams don't generally work that way. It will block until some data is available, but it won't wait until it's got enough data to fill the array.

Generally this means looping round. For instance:

byte[] data = new byte[expectedSize];
int totalRead = 0;
while (totalRead < expectedSize)
{
    int read = stream.read(data, totalRead, expectedSize-totalRead);
    if (read == -1)
    {
        throw new IOException("Not enough data in stream");
    }
    totalRead += read;
}

If you don't know how many bytes you're expecting in the first place, you may well want to still loop round, but this time until read() returns -1. Use a buffer (e.g. 8K) to read into, and write into a ByteArrayOutputStream. When you've finished reading, you can then get the data out of the ByteArrayOutputStream as a byte array.

Jon Skeet
+1  A: 

Absolutly -- TCP Sockets is a reliable network protocol provided the API is used properly.

You really need to check the number of bytes you receive on each read() call.

Sockets will arbiterily decide you have enough data and pass it back on hte read call -- the amount can dependon many factors (buffer size, memory availibility, network respose time etc.) most of which are unpredicatable. For smaller buffers you normally get as many bytes as you asked for, but, for larger buffer sizes read() will often return less data than you asked for -- you need to check the number of bytes read and repeat the read call for the remaining bytes.

It is also possible that something in your network infrastructure (router, firewall etc.) is misconfigred and trucating large packets.

James Anderson
A: 

Your problem is that in the server thread, you must call outputstream.flush(), to specify that the buffered data should be send to the other end of the communication

I have used flush(). But it did not work fine but if i put time dalay then it works.
Deepak
So how can I send a large file over tcp java
Deepak