this is the serverStart methodCode:else if (args[0].equals("-s")) { if (args.length < 2) { System.out.println("You must specify a port number. E.g, java cw1 -s 231"); } else { System.out.println("Please enter a secret key which both the client and server will share:"); BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); String line = r.readLine().trim(); SECRET_KEY = line; try { int port = Integer.parseInt(args[1]); serverStart(port); } catch (NumberFormatException e) { System.out.println("Invalid port number"); } } }
so basically in the command line i doCode:public static void serverStart(int port) throws IOException { ServerSocket s = new ServerSocket(port); System.out.println("Server is now awaiting for incoming connections on port " + port); while (true) { try { Socket client = s.accept(); DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream())); DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream())); } catch (Exception e ) {} } }
java cw1 -s 123
but before it should start listening on port 123, it should ask me for the key which it does. i enter 'hello' and it gives me error
Code:C:\Documents and Settings\admin\Desktop\Cryptography>java cw1 -s 123 java cw1 -s 123 Please enter a secret key which both the client and server will share: hello Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind at java.net.PlainSocketImpl.socketBind(Native Method) at java.net.PlainSocketImpl.bind(Unknown Source) at java.net.ServerSocket.bind(Unknown Source) at java.net.ServerSocket.<init>(Unknown Source) at java.net.ServerSocket.<init>(Unknown Source) at cw1.serverStart(cw1.java:62) at cw1.main(cw1.java:36)




Reply With Quote