Java
Java
Bind and Listen for Incoming Connections
See more Socket/SSL/TLS Examples
Demonstrates Socket.BindAndListen, which binds a TCP listener to a port and places it into listening mode, then accepts an incoming connection.
Background. The backlog argument bounds the queue of pending connections. When
ClientIpAddress is empty, the listener binds to all interfaces. After listening, call AcceptNext to accept connections.Chilkat Java Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
CkSocket socket = new CkSocket();
// Bind a TCP listener to a port and place it into listening mode. The 2nd argument is the backlog,
// the maximum number of pending connections allowed to queue.
success = socket.BindAndListen(5000,25);
if (success == false) {
System.out.println(socket.lastErrorText());
return;
}
// After the listener is established, accept the next incoming connection into a new Socket.
CkSocket connectedSock = new CkSocket();
success = socket.AcceptNext(20000,connectedSock);
if (success == false) {
System.out.println(socket.lastErrorText());
return;
}
System.out.println("Accepted a connection.");
}
}