Results 1 to 2 of 2

Thread: error in main

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    error in main

    Code:
    	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"); 
    		}	
    	    }   
    	}
    this is the serverStart method
    Code:
        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 ) {}
    	}
        }
    so basically in the command line i do
    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)

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: error in main

    Never heard of Windows listening on NTP automatically, but who knows ...

    Try a much higher port, e.g. 10010. The ports below 1024 are reserved for "well-known services" anyway and often taken.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width