Something went wrong on our end
TcpExample4ThreadServer.java 1.82 KiB
package TcpExamples;
import java.io.IOException;
import java.net.*;
/**
* An example of using threads to handle multiple connections
* at the same time.
*
* @author mcgredo
*/
public class TcpExample4ThreadServer {
public static void main(String[] args) // execution starts here
{
try
{
ServerSocket serverSocket = new ServerSocket(2317);
int connectionCount = 0; // state variable
System.out.println("TcpExample4ThreadServer ready to accept socket connections...");
while(true) // infinite loop
{
Socket clientConnection = serverSocket.accept(); // block until connected
connectionCount++; // unblocked, got another connection
System.out.println("=============================================================");
System.out.println("TcpExample4ThreadServer.handlerThread invocation for connection #" + connectionCount + "...");
TcpExample4HandlerThread handlerThread = new TcpExample4HandlerThread(clientConnection);
handlerThread.start(); // invokes the run() method in that object
System.out.println("TcpExample4ThreadServer.handlerThread is launched, awaiting another connection...");
}
}
catch(IOException e)
{
System.out.println("Problem with TcpExample4ThreadServer networking:"); // describe what is happening
System.out.println("Error: " + e);
// Provide more helpful information to user if exception occurs due to running twice at one time
if (e instanceof java.net.BindException)
System.out.println("*** Be sure to stop any other running instances of programs using this port!");
}
System.out.println("=============================================================");
}
}