wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes port  number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "port  number " Watch "port  number " New topic
Author

port number

Sanjul Jain
Greenhorn

Joined: Oct 21, 2004
Posts: 14
Hello!

Instead of giving default value of port number 7777, I want to

let user specify the value of port number.

Can you please help me out.


Regards,
Sanjul

Here is my code:

//server code:
import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;

class TCPAdditionServer1{
public static void main(String[] argv)throws Exception{
String clientSentence,capitalizedSentence;
int sum=0;
String sumString = "";
InetAddress serverAddress;

int listen_port = 7777;
//int listen_port = Socket.getLocalPort();

//ServerSocket welcomeSocket=new ServerSocket(7777);
ServerSocket welcomeSocket=new

ServerSocket(listen_port);

//ServerSocket welcomeSocket=new

ServerSocket(welcomeSocket.getPort());

//int cpn = welcomeSocket.getLocalPort();
// System.out.println(" get local port: " + cpn);

//int cpn1 = welcomeSocket.getPort();
//System.out.println(" get remote port: " + cpn1);

// welcomeSocket(cpn);

while(true){

Socket connectionSocket = null;

try {
connectionSocket=welcomeSocket.accept();
} catch (IOException e) {
System.out.println("Accept failed:");
System.exit(-1);
}

BufferedReader inFromClient=new BufferedReader(new

InputStreamReader(connectionSocket.getInputStream()));

DataOutputStream outToClient=new

DataOutputStream(connectionSocket.getOutputStream());
clientSentence=inFromClient.readLine();

StringTokenizer st= new

StringTokenizer(clientSentence);
while(st.hasMoreTokens()) {
sum += Integer.parseInt(st.nextToken());
}

int clientPortNumber =

connectionSocket.getLocalPort();
System.out.println("Waiting for connections on

port " + clientPortNumber + " ... ");


System.out.println("Received connection from "

+ connectionSocket.getRemoteSocketAddress());


sumString = Integer.toString(sum);
clientSentence = " ";
capitalizedSentence = clientSentence + sumString +

"\n";
outToClient.writeBytes(capitalizedSentence);

}
}

}

========================================
//client code
import java.io.*;
import java.net.*;
import java.net.InetAddress;

class TCPAdditionClient1{
public static void main(String[] argv)throws Exception{
String sentence,sentc,portNumberString;
int portNumber= 7777;
String modifiedSentence;
InetAddress clientAddress;
BufferedReader inFromUser1=new BufferedReader(new

InputStreamReader(System.in));
BufferedReader inFromUser2=new BufferedReader(new

InputStreamReader(System.in));
BufferedReader inFromUser3=new BufferedReader(new

InputStreamReader(System.in)); //portNumberString

try{

System.out.println("Enter port number (Press Enter for

default value(7777)): ");
portNumberString = inFromUser3.readLine();

int portNumberInteger =

Integer.parseInt(portNumberString);

portNumber = portNumberString.length() == 0 ? 7777 :

portNumberInteger;


//System.out.println("port number:" + portNumber);

//working

System.out.println("Enter hostname or IP of server:

(Press Enter for default value(localhost)): ");
sentc = inFromUser1.readLine();

String hostName = sentc.length() == 0 ? "localhost" :

sentc;
Socket clientSocket=new Socket(hostName,portNumber);

SocketAddress sa =

clientSocket.getRemoteSocketAddress();

if (clientSocket.isConnected()== true);
{
System.out.println("Connecting to server " + "'" + sa

+ "'" + "..." + " Connected.");
}

DataOutputStream outToServer=new

DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer=new BufferedReader(new

InputStreamReader(clientSocket.getInputStream()));

System.out.println();
System.out.println("Enter a string of integers

seperated by non-numeric characters:");
sentence = inFromUser2.readLine();

outToServer.writeBytes(sentence+"\n");
modifiedSentence=inFromServer.readLine();
System.out.println("From Server----->The Sum

is:"+modifiedSentence);

clientSocket.close();

} catch(UnknownHostException e) {
System.err.println(" Don't know about host: ");
System.exit(1);
} catch(IOException e) {
System.err.println(" Couldn't get I/O for the

connection");
System.exit(1);
}

}
}

===================
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

When posting code, please use the code-tags.

You may edit your post, and insert them later, if you forgot them - i.e.: now.

---
To the content of your question:
Normally, a service is running on a server, allowing multiple clients to connect to it.
The port is a kind of adress, where the service is reachable.

The server is started (months) before the client requests it.

How can the client inform the server, that it likes to access the service on port xy?
And why?

Do you want to start a server for every client?
When the first client begs to use Port 7778, and the second client likes to have it waiting on 7776?

What if the client is asking for a port already in use - i.e.: port 80, 21, 7 or some other well known port?

You may start a server on port 7777, to wait for the information of client where to listen, and start a new Thread, listening on that port.
But you only moved the fixed port to another level.

Think about the port-number like your telephone-number.
People who want to call you need to know your number, and cannot specify the number, under which they want to reach you.
You could give them a primary-request number, where they may call you, and tell you 'I want to reach you under 666-44-345', and you could then call your telephone-company, and ask them, to get a second telephone number '666-44-345', and if it is possible - the number isn't in use right now, wait on the call on your second phone.
But why?

---
Additional hints:
You needn't import java.lang.*;

And if you open a stream for reading (from System.in or whereether), you don't need a new Streamobject for every readLine()-call.
Generate one object, and read from that:

[ October 24, 2004: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: port number
 
Similar Threads
cleint-server interaction
Running a client/server apllication
display http header
Push/Grab archive file from server
bugs in client\server program - can't connect to remote server