473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

how to access user inputted port number client-server

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
0 1815

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

Similar topics

2
by: Bozo | last post by:
Hi, Does anyone know how to run a .Net exe using a particualr port number to connect to a web service? How do you do this programmatically. Thanks
3
by: Franky | last post by:
I need to specify a range of outgoing port numbers (i.e., 20000-21000). Is there any way to bind this range so that when system picks up a free port, it will only pick one from this range? I know I...
0
by: Sanjay Patil | last post by:
Hi , I am using IBuySpay Portal modified code. Public page of the web site uses nonssl port. Once user is logged in I am transferring to page which uses ssl port. During this process I am using...
1
by: Shafia | last post by:
Hi, What exactly do I need to do to run an asp.net application remotely? The URL contains a unique port number for each application e.g http://localhost:60169/WebSite2/Default.aspx This port...
1
by: mike g | last post by:
Hello, I have postgres set to log all connections. I run it on port 5432. I have noticed in the postgres logs that the port being used to connect to the database is logged but never equal to...
2
by: bryan.a.fowler | last post by:
I'm really new working with VB.net and I'm working on a simple program with a date input. I've got a couple of questions; 1) I'm using a DateTimePicker to show both the user inputted date as...
0
by: narayanansun | last post by:
hi, i need to the get the com port number of the connected usb device. pls help me to find out the com port number using win32 api in C#. i have device path and handle with me and want to find out...
5
by: Werner Schmidt | last post by:
Dear group, I have the following scenario: I have an application - let's call it myApp.exe - that is listening on a specific port - let's say 10000. Now I need a way to get the port number...
1
by: anneaunimme | last post by:
I have a program which allow my computer to communicate to another device using an ethernet Cable. The thing is that this program is set so that it can only work through a specific IP address and a...
3
by: Sebouh | last post by:
Hi guys. I have a question about sockets. It appears that the accept function returns a socket descriptor after creating a new socket handler. This new handler inherits the properties of the socket...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.