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
- import java.io.*;
- import java.net.*;
- class TCPServer {
- static ServerSocket welcomeSocket;
- static Socket connectionSocket;
- static BufferedReader inFromClient;
- static DataOutputStream outToClient;
- static String clientSentence;
- static String capitalizedSentence;
- public static int getTCPClientPortNo(){
- return TCPClient.portNumber;
- }
- public static void receiveServerConnection(){
- try{
- System.out.println("The port number is: " + getTCPClientPortNo());
- welcomeSocket = new ServerSocket(getTCPClientPortNo());
- }
- catch(Exception e){
- System.out.println("The port number is not available");
- }
- }
- public static void acceptSocketConnection(){
- while (true) {
- try{
- connectionSocket = welcomeSocket.accept();
- inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
- outToClient = new DataOutputStream(connectionSocket.getOutputStream());
- clientSentence = inFromClient.readLine();
- System.out.println("clientSentence: " + clientSentence );
- capitalizedSentence = clientSentence.toUpperCase() + "\n";
- outToClient.writeBytes(capitalizedSentence);
- }
- catch(Exception e){
- System.out.println("Some error occured");
- }
- }
- }
- public static void main(String args[]) throws Exception {
- receiveServerConnection();
- }
- }
and the client code is:
Expand|Select|Wrap|Line Numbers
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class TCPClient {
- static String host, port;
- static int portNumber = 0;
- static boolean connection = true;
- static Scanner kb = new Scanner(System.in);
- static Socket clientSocket;
- public static void getServerInfo(){
- try{
- System.out.print("Please enter the host name: ");
- BufferedReader hostInFromUser = new BufferedReader(new InputStreamReader(System.in));
- TCPClient.host = hostInFromUser.readLine();
- System.out.println("Host Name: " + TCPClient.host);
- System.out.print("Please enter the port number: ");
- BufferedReader portInFromUser = new BufferedReader(new InputStreamReader(System.in));
- TCPClient.port = portInFromUser.readLine();
- TCPClient.portNumber = Integer.parseInt(TCPClient.port);
- System.out.println("Port Number: " + TCPClient.portNumber);
- System.out.println("The client application will now attempt to connect to the server...");
- serverConnect(host, portNumber);
- }
- catch(Exception e){
- System.out.println("There has been an unexpecetd error, please try again...");
- }
- }
- public static void serverConnect(String hostName, int portNumber){
- try{
- clientSocket = new Socket(hostName, portNumber);
- System.out.println("Successfully connected to the server!");
- connection=true;
- }
- catch(Exception e){
- System.out.println("The connection to the server has been refused, please try again...");
- connection=false;
- }
- }
- public static void main(String args[]) throws Exception {
- String sentence;
- String modifiedSentence;
- do{
- TCPClient.getServerInfo();
- }
- while(TCPClient.connection ==false);
- {
- TCPClient.getServerInfo();
- }
- BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
- DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
- BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- sentence = inFromUser.readLine();
- outToServer.writeBytes(sentence + "\n");
- modifiedSentence = inFromServer.readLine();
- System.out.println("FROM SERVER: " + modifiedSentence);
- clientSocket.close();
- }
- }
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