Java
Chilkat2 Java
Move a Connection into a Socket Set
See more Socket/SSL/TLS Examples
Demonstrates Socket.TakeSocket, which moves a connection into a newly created member of this object's socket set. After one or more calls, the object acts as a set of sockets.
Background. A socket set groups multiple connections so they can be monitored together with the SelectForReading and SelectForWriting methods. After success, the source socket holds no connection.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Socket;
public class ChilkatExample {
public static void main(String[] args) {
Socket listenSock = new Socket();
try {
listenSock.bindAndListen(5000, 25);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Socket connectedSock = new Socket();
try {
listenSock.acceptNext(20000, connectedSock);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Move the connection out of connectedSock and into a newly created member of this object's socket
// set. After one or more such calls, socketSet acts as a set of connected sockets (useful with the
// SelectForReading / SelectForWriting methods). After success, connectedSock holds no connection.
Socket socketSet = new Socket();
try {
socketSet.takeSocket(connectedSock);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Connection added to the socket set.");
}
}