Sample code for 30+ languages & platforms
Java

Send Bytes on a Socket Connection

See more Socket/SSL/TLS Examples

Demonstrates how to send a mixture of binary (non-text) and text bytes on a socket connection.

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) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Socket socket = new Socket();

        // Connect to some host:port
        boolean ssl = false;
        int maxWaitMillisec = 20000;
        int port = 5555;
        try {
            socket.connect("test.com", port, ssl, maxWaitMillisec);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // We wish to send a 0x00 byte followed by the us-ascii string "10800"
        BinData bd = new BinData();

        bd.appendByte(0);
        bd.appendString("10800", "utf-8");

        // Send the entire contents of bd.
        try {
            socket.sendBd(bd, 0, 0);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }
    }
}