Trail: Custom Networking
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Server { public static void main(String[] args) { new Server().startServer(); } public void startServer() { final ExecutorService clientProcessingPool = Executors.newFixedThreadPool(10); Runnable serverTask = new Runnable() { public void run() { try { ServerSocket serverSocket = new ServerSocket(9999); System.out.println("Waiting for clients to connect..."); while (true) { Socket clientSocket = serverSocket.accept(); clientProcessingPool.submit(new ClientTask(clientSocket)); } } catch (Exception e) { System.err.println("Unable to process client request"); e.printStackTrace(); } } }; Thread serverThread = new Thread(serverTask); serverThread.start(); } private class ClientTask implements Runnable { private final Socket clientSocket; private ClientTask(Socket clientSocket) { this.clientSocket = clientSocket; } public void run() { System.out.println("Got a client !"); // Do whatever required to process the client's request try { System.out.println("Just connected to " + clientSocket.getRemoteSocketAddress()); OutputStream outToServer = clientSocket.getOutputStream(); DataOutputStream out = new DataOutputStream(outToServer); out.writeUTF("Hello from " + clientSocket.getLocalSocketAddress()); InputStream inFromServer = clientSocket.getInputStream(); DataInputStream in = new DataInputStream(inFromServer); System.out.println("Server says " + in.readUTF()); clientSocket.close(); try { clientSocket.close(); } catch (Exception e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } } }
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketTimeoutException; public class GreetingServer extends Thread { private ServerSocket serverSocket; public GreetingServer(int port) throws IOException { serverSocket = new ServerSocket(port); serverSocket.setSoTimeout(1000000); } public void run() { while (true) { try { System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "..."); Socket server = serverSocket.accept(); System.out.println("Just connected to " + server.getRemoteSocketAddress()); DataInputStream in = new DataInputStream(server.getInputStream()); System.out.println(in.readUTF()); DataOutputStream out = new DataOutputStream(server.getOutputStream()); out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress() + "\nGoodbye!"); server.close(); } catch (SocketTimeoutException s) { System.out.println("Socket timed out!"); break; } catch (IOException e) { e.printStackTrace(); break; } } } public static void main(String[] args) { int port = Integer.parseInt(args[0]); try { Thread t = new GreetingServer(port); t.start(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class GreetingClient { public static void main(String[] args) { String serverName = args[0]; int port = Integer.parseInt(args[1]); try { System.out.println("Connecting to " + serverName + " on port " + port); Socket client = new Socket(serverName, port); System.out.println("Just connected to " + client.getRemoteSocketAddress()); OutputStream outToServer = client.getOutputStream(); DataOutputStream out = new DataOutputStream(outToServer); out.writeUTF("Hello from " + client.getLocalSocketAddress()); InputStream inFromServer = client.getInputStream(); DataInputStream in = new DataInputStream(inFromServer); System.out.println("Server says " + in.readUTF()); client.close(); } catch (IOException e) { e.printStackTrace(); } } }
public class TestJava { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } }
No comments:
Post a Comment