Java Requires Chilkat v11.0.0+
Chilkat2 Java
Transfer a File using Sockets (TLS or non-TLS)
See more Socket/SSL/TLS Examples
Demonstrates how to two programs, one a socket writer and the other a socket reader, can transfer a file. The connection can be TLS or a regular non-encrypted TCP connection.Chilkat Java Downloads
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.
// On the sending side, we'll load the file into a BinData object and send.
// On the receiving side, we'll read from the socket connection into a BinData, and save to a file.
// This example assumes the file is not crazy-large, and that the entire contents
// can fit into memory.
// (If the file is too large for memory, there are other ways to send. It just involves streaming or
// sending the file chunk-by-chunk..)
// This section of code is for the sender.
BinData bdToSend = new BinData();
bdToSend.loadFile("somePath/someFile.dat");
// Assume success for the example...
Socket sndSock = new Socket();
boolean bUseTls = true;
int port = 5555;
int maxWaitMs = 5000;
sndSock.connect("some_domain_or_ip.com", port, bUseTls, maxWaitMs);
// Assume success for the example...
// Tell the receiver how many bytes are coming.
int numBytes = bdToSend.getNumBytes();
boolean bBigEndian = true;
sndSock.sendInt32(numBytes, bBigEndian);
// Send the file data (sends the entire contents of bdToSend).
sndSock.sendBd(bdToSend, 0, 0);
// Get an acknowledgement.
try {
sndSock.receiveInt32(bBigEndian);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Did the receiver get the correct number of bytes?
if (sndSock.getReceivedInt() != numBytes) {
System.out.println("The receiver did not acknowledge with the correct number of bytes.");
return;
}
System.out.println("File sent!");
// ------------------------------------------------------------------------------------
// The code below is for the receiving side (running on some other computer..)
Socket listenSock = new Socket();
try {
listenSock.bindAndListen(5555, 25);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Get the next incoming connection
// Wait a maximum of 20 seconds (20000 millisec)
Socket rcvSock = new Socket();
try {
listenSock.acceptNext(20000, rcvSock);
// The sender will first send the big-endian integer for the number of bytes
// that are forthcoming..
rcvSock.receiveInt32(bBigEndian);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
int numBytesComing = rcvSock.getReceivedInt();
// Receive that many bytes..
BinData bdReceived = new BinData();
try {
rcvSock.receiveBdN(numBytesComing, bdReceived);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Acknowledge the sender by sending back the number of bytes we received.
rcvSock.sendInt32(bdReceived.getNumBytes(), bBigEndian);
// Close the connection.
maxWaitMs = 20;
rcvSock.closeConnection(maxWaitMs);
// Save the received data to a file.
bdReceived.writeFile("somePath/someFile.dat");
// Assume success for the example...
System.out.println("File received!");
}
}