By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,305 Members | 827 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,305 IT Pros & Developers. It's quick & easy.

how to access user inputted port number client-server

P: 74
Hello,

Im building a client-server application and the user needs to input the hostName and the portNumber the client application and needs to send the portnumber to the server application. I have everything set up (properly, i think) but i keep getting a connection error on the portNumber because my instantiated portNumber is 0; On the server side, when i System.out.print the portNumber, i see it is in fact 0. I know why this is happening; its happening because the server is running before the client and the server is taking the default client's port number, but i dont know how to get around this.
Is there a way to ask the server to wait for the portNumber until the user has inputted the value?

here is my code: (for now i am concerned with the hostName and port number, the rest of the code doesn't do much. its supposed to take a client's string and the server is supposed to upper-case it and return it to the server... that functionality isn't completely working yet)

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. class TCPServer {
  5.  
  6.     static ServerSocket welcomeSocket;
  7.     static Socket connectionSocket;
  8.     static BufferedReader inFromClient;
  9.     static DataOutputStream outToClient;
  10.     static String clientSentence;
  11.     static String capitalizedSentence;
  12.  
  13.     public static int getTCPClientPortNo(){
  14.         return TCPClient.portNumber;
  15.     }
  16.  
  17.     public static void receiveServerConnection(){
  18.         try{
  19.             System.out.println("The port number is: " + getTCPClientPortNo());
  20.  
  21.             welcomeSocket = new ServerSocket(getTCPClientPortNo());
  22.         }
  23.         catch(Exception e){
  24.             System.out.println("The port number is not available");
  25.         }
  26.     }
  27.  
  28.     public static void acceptSocketConnection(){
  29.         while (true) {
  30.  
  31.             try{
  32.                 connectionSocket = welcomeSocket.accept();
  33.  
  34.                 inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  35.  
  36.                 outToClient = new DataOutputStream(connectionSocket.getOutputStream());
  37.  
  38.                 clientSentence = inFromClient.readLine();
  39.                 System.out.println("clientSentence: " + clientSentence );
  40.  
  41.                 capitalizedSentence = clientSentence.toUpperCase() + "\n";
  42.  
  43.                 outToClient.writeBytes(capitalizedSentence);
  44.             }
  45.             catch(Exception e){
  46.                 System.out.println("Some error occured");
  47.  
  48.             }
  49.  
  50.         }
  51.     }
  52.  
  53.  
  54.     public static void main(String args[]) throws Exception {
  55.  
  56.         receiveServerConnection();
  57.  
  58.  
  59.     }
  60. }
  61.  



and the client code is:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. class TCPClient {
  6.  
  7.     static String host, port;
  8.     static int portNumber = 0;
  9.     static boolean connection = true;
  10.  
  11.     static Scanner kb = new Scanner(System.in);
  12.     static Socket clientSocket;
  13.  
  14.     public static void getServerInfo(){
  15.         try{
  16.         System.out.print("Please enter the host name: ");
  17.         BufferedReader hostInFromUser = new BufferedReader(new InputStreamReader(System.in));
  18.         TCPClient.host = hostInFromUser.readLine();
  19.         System.out.println("Host Name: " + TCPClient.host);
  20.  
  21.  
  22.         System.out.print("Please enter the port number: ");
  23.         BufferedReader portInFromUser = new BufferedReader(new InputStreamReader(System.in));
  24.         TCPClient.port = portInFromUser.readLine();
  25.         TCPClient.portNumber = Integer.parseInt(TCPClient.port);
  26.         System.out.println("Port Number: " + TCPClient.portNumber);
  27.  
  28.         System.out.println("The client application will now attempt to connect to the server...");
  29.  
  30.         serverConnect(host, portNumber);
  31.         }
  32.         catch(Exception e){
  33.             System.out.println("There has been an unexpecetd error, please try again...");
  34.  
  35.         }
  36.     }
  37.  
  38.     public static void serverConnect(String hostName, int portNumber){
  39.         try{
  40.             clientSocket = new Socket(hostName, portNumber);
  41.             System.out.println("Successfully connected to the server!");
  42.             connection=true;
  43.  
  44.         }
  45.         catch(Exception e){
  46.             System.out.println("The connection to the server has been refused, please try again...");
  47.             connection=false;
  48.         }
  49.     }
  50.  
  51.     public static void main(String args[]) throws Exception {
  52.  
  53.         String sentence;
  54.         String modifiedSentence;
  55.         do{
  56.             TCPClient.getServerInfo();
  57.  
  58.         }
  59.         while(TCPClient.connection ==false);
  60.         {
  61.             TCPClient.getServerInfo();
  62.  
  63.         }
  64.         BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  65.  
  66.  
  67.         DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  68.  
  69.         BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  70.         sentence = inFromUser.readLine();
  71.  
  72.         outToServer.writeBytes(sentence + "\n");
  73.  
  74.         modifiedSentence = inFromServer.readLine();
  75.  
  76.         System.out.println("FROM SERVER: " + modifiedSentence);
  77.  
  78.         clientSocket.close();
  79.     }
  80. }
  81.  

so again, the question is: is there a way to tell the server to wait until the user inputs the port number before the server gets it?

thanks a lot
Sep 18 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.