473,287 Members | 1,501 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,287 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 1793

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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.