Hey. I made a software using Sockets to create a server, and then use clients ( bots ), which do the math and report back to the server. There's a few problems going on in my code. For some reason, the server is using a port not specified by the code. (???), and the handshake isn't working.
Launcher
import java.util.Scanner; import static kit.BuildInfo.*; public class Launcher { public static void main(String[] args) { System.out.println("CrowdSource Data Miner - Prime Number Finder\nBy Ethan Manzi\nVersion " + version + " - " + build + "\n\n"); System.out.println("Please select what software you would like to run"); System.out.println("[S]erver\n[C]lient\n[B]oth"); Scanner s = new Scanner(System.in); String option = s.nextLine(); if (option.equalsIgnoreCase("s")) { System.out.println("Starting server....."); new server.main(); } else if (option.equalsIgnoreCase("c")) { System.out.println("Starting Miner....."); new client.main(); } else if (option.equalsIgnoreCase("b")) { System.out.println("Attemping to run server and miner....."); } } } /* System.out.print("Server Address: "); Scanner s = new Scanner(System.in); String svr = s.nextLine(); ipAddress = svr; */
server.main
package server; import sun.misc.Launcher; import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; import static kit.BuildInfo.*; public class main { public static int clients = 0; static DataInputStream input; static PrintStream output; static ServerSocket MyService; static String idenity = ""; public static int n = 1; public main() { Scanner s = new Scanner(System.in); System.out.print("Server Identifier: "); idenity = s.nextLine(); System.out.println("Running server at localhost:" + port); capter(); } private void capter() { try { MyService = new ServerSocket(61875); } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { System.out.println("Server Open!"); System.out.println(MyService.getLocalPort()); if (MyService.isClosed()) { System.out.println("NOT OPEN"); } if (MyService.isBound()) { System.out.println("Bound"); } } while (true) { // accept connections from clients try { Socket clientSocket = MyService.accept(); new Thread(new Runnable() { public void run() { try { System.out.println("New Connection"); Socket fwd = clientSocket; client(fwd); } catch (Exception e) { } finally { } } }).start(); } catch (Exception e) { e.printStackTrace(); } } } static Writer out; static Socket m; int client(Socket s) { m=s; // get communication try { new File("Primes.txt").delete(); // new BufferedWriter(new FileWriter("Primes.txt",true)); input = new DataInputStream(s.getInputStream()); output = new PrintStream(s.getOutputStream()); } catch (Exception e) { e.printStackTrace(); System.out.println("\nError in getting communication"); System.exit(1); } finally { } //preform handshake try { output.println("listening"); String intent = input.readLine(); if (intent.equalsIgnoreCase("ping")) { output.println(clients); output.println(idenity); output.println(build); output.println("exit"); String reply = input.readLine(); disconnect(); return 0; } else if (intent.equalsIgnoreCase("connect")) { String id = input.readLine(); if (id.equalsIgnoreCase(identifer)) { output.println("100"); } else { output.println("662"); disconnect(); } } } catch (Exception e) { e.printStackTrace(); System.out.println("\nError in getting handshake"); System.exit(1); } //begin clients++; while (true) { try { String sta = input.readLine(); if (sta.equalsIgnoreCase("ready")) { int lock = n; n++; output.println(lock); String reply = input.readLine(); if (reply.equalsIgnoreCase("yes")) { out = new BufferedWriter(new FileWriter("Primes.txt",true)); out.append("\n" + lock); out.close(); } } } catch (Exception e) { } finally { } } } private void disconnect() { try { input.close(); output.close(); m.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { System.out.println("Client Disconnected"); clients--; } } } /* Codes: 100 - Accepted 662 - Invalid Version */
client.main
package client; import jdk.nashorn.internal.runtime.ECMAException; import java.io.DataInputStream; import java.io.PrintStream; import java.net.Socket; import java.util.Scanner; import static kit.BuildInfo.*; public class main { public final String ip; public final int Threads; public int inp = port; DataInputStream input; PrintStream output; Socket MyClient; int ct = 0; public main() { Scanner s = new Scanner(System.in); System.out.print("Server: "); ip = s.nextLine(); System.out.print("Threads: "); Threads = Integer.parseInt(s.nextLine()); System.out.print("port(" + port + ") Enter [0] for default: "); int p = s.nextInt(); if (p==0) {} else { inp=p; } //ping the server System.out.println("Pining server: " + ip + ":" + inp); try { MyClient = new Socket(ip, inp); System.out.println("Status: " + MyClient.isConnected()); input = new DataInputStream(MyClient.getInputStream()); output = new PrintStream(MyClient.getOutputStream()); } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { try { System.out.println("Establishing Handshake...."); String responce; responce = input.readLine(); if (responce.equalsIgnoreCase("listening")) { System.out.println("Sending command...."); output.println("ping"); System.out.println("Getting Data...."); String clientCount = input.readLine(); String id = input.readLine(); String build = input.readLine(); String status = input.readLine(); output.println("OK"); try { output.close(); input.close(); MyClient.close(); System.out.println("Disconnected"); } catch (Exception e) {System.out.println("System already disconnected!");} } else { System.out.println("Critical Error in Handshake!"); System.exit(1); } } catch (Exception e) { e.printStackTrace(); } } for (int i=0; i<Threads; i++) { Thread t = new Thread(new Runnable() { public void run() { int lock=ct;ct++; connect(lock); } }); t.start(); } } private void connect(int n) { System.out.println("Thread " + n + " - online"); //handshake try { MyClient = new Socket(ip, inp); System.out.println("Establishing Handshake...."); String responce; responce = input.readLine(); if (responce.equalsIgnoreCase("listening")) { output.println("connect"); output.println(identifer); String st = input.readLine(); System.out.println("Handshake Reply: " + st); } else { System.out.println("Critical Error in Handshake!"); System.exit(1); } } catch(Exception e) { } while (true) { try { output.println("ready"); int test = Integer.parseInt(input.readLine()); boolean prime = true; System.out.println("Checking number"); for (int i=2; i<test-1; i++) { if (test % i == 0) { prime=false; } } if (prime) { output.println("yes"); } else { output.println("yes"); } } catch (Exception e) { e.printStackTrace(); if (MyClient.isConnected()) { System.out.println("Still connected"); } else { System.out.println("Lost Connection!"); System.exit(1); } System.exit(1); } } } }
kit.BuildInfo
package kit; public class BuildInfo { public static final String version = "Alpha 1"; public static final int build = 17; public static final String identifer = "thisisliketotallymysecondtrydoingthis"; public static final int port = 2121; }