Sample code for 30+ languages & platforms
Java

Receive Bytes into a BinData

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveBd, which receives one delivery of binary data and appends it to a BinData. It returns whatever is immediately available.

Background. The BinData receive methods accumulate raw bytes in a BinData object. A single receive returns only what is currently available, whereas the counted receive waits for an exact number of bytes.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Socket;

public class ChilkatExample {

    public static void main(String[] args) {
        Socket socket = new Socket();

        // Connect to the server using TLS.
        boolean bTls = true;
        int maxWaitMs = 5000;
        try {
            socket.connect("example.com", 5000, bTls, maxWaitMs);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Receive one delivery of binary data and append it to a BinData.  ReceiveBd returns whatever is
        // immediately available; it does not loop to drain everything that may follow.
        BinData bd = new BinData();
        try {
            socket.receiveBd(bd);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Received " + bd.getNumBytes() + " bytes.");
    }
}