Dynamically get the Port Number

Hi All,

I am new to Socket programming I need some help on how to retrieve a port of client socket program and get them dynamically populated in the server socket and then accept the connection using socket s=s.accept

.My server side coding is as follows.

import java.net.*;

import java.io.*;

publicclass Server{

publicstaticvoid main(String[] args){

int port = 6666; [b]/*This port number has to be dynamically received by this server program*/[/b]

try{

ServerSocket ss =new ServerSocket(port);// create a server socket and bind it to the above port number.

System.out.println("Waiting for a client...");

Socket socket = ss.accept();// make the server listen for a connection, and let you know when it gets one.

System.out.println("Got a client :) ... Finally, someone saw me through all the cover!");

System.out.println();

// Get the input and output streams of the socket, so that you can receive and send data to the client.

InputStream sin = socket.getInputStream();

OutputStream sout = socket.getOutputStream();

// Just converting them to different streams, so that string handling becomes easier.

DataInputStream in =new DataInputStream(sin);

DataOutputStream out =new DataOutputStream(sout);

String line =null;

while(true){

line = in.readUTF();// wait for the client to send a line of text.

System.out.println("The client just sent me this line : " + line);

System.out.println("I'm sending it back...");

out.writeUTF(line);// send the same line back to the client.

out.flush();// flush the stream to ensure that the data reaches the other end.

System.out.println("Waiting for the next line...");

System.out.println();

}

}catch(Exception x){

x.printStackTrace();

}

}

}

please help me regarding this.Thanks in Advance

[3136 byte] By [Ananth-Ra] at [2007-11-27 1:57:39]
# 1
and get them dynamically populated in the server socketwhat do you mean exactly? i can't get this part
calvino_inda at 2007-7-12 1:33:29 > top of Java-index,Java Essentials,Java Programming...
# 2

Hello Sir,

How do i get the port number of a client in a server socket program.The values of the port shouldn't be hard coded.The port number of the client should be programmatically received by the server program.How do i get along with this any sample code or links would be more useful

Hope this is more clear?Thanks in Advance!!

Ananth-Ra at 2007-7-12 1:33:29 > top of Java-index,Java Essentials,Java Programming...
# 3
what about the getPort() method in class Sockettry getPort() on 'socket', 'socket' being the one created with the accept() method
calvino_inda at 2007-7-12 1:33:29 > top of Java-index,Java Essentials,Java Programming...
# 4

> How do i get the port number of a client in a server socket program.

You can't do that until a client has already connected to the server. And the client can't connect to the server until it knows what port the server is listening to.

> The values of the port shouldn't be hard coded.

Yes, it should. How else can the client know what port to connect to?

DrClapa at 2007-7-12 1:33:30 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi MR.DrClap

Thanks for your information but can we do a check like this?

First specify a default port Number some values between 0 and 65536 then we use the getLocalPort() to get the port number of the client socket program.After which we can specify a condition to check if the port number of the Client is received then we accept the connection using Socket s=s.accept()

and proceed with the operation else we exit out of the program and print a error stating that the port number cannot be occuired by the server socket program.

Ananth-Ra at 2007-7-12 1:33:30 > top of Java-index,Java Essentials,Java Programming...
# 6

> First specify a default port Number some values

> between 0 and 65536

Yes, actually between 1024 and 65535, and avoiding the IANA-allocated port numbers too, so probably really between 40000 ad 65535.

> then we use the getLocalPort() to

> get the port number of the client socket

> program.

No. The client doesn't have a port number until it connects to your server.

> After which we can specify a condition to

> check if the port number of the Client is received

Such as what? I don't know what you're talking about here.

> then we accept the connection using Socket

> s=s.accept()

Yes

> and proceed with the operation

> else we exit out of the program and print a error

> stating that the port number cannot be occuired by

> the server socket program.

You don't need 3/4 of this even iif it did make sense, which it doesn't. All you need is the fixed server port, the accept, and the check of the remote port (and presumably IP address) of the accepted socket.

Everything else you've written here is nonsense. The requirement is nonsense too. Are you aware that the client will use either (i) a dynamically allocated port number, so the check is pointless, or (ii) a fixed outgoing port number so it can try them all to find the ones that your server will accept?

So you are actually adding zero security here, just a nuisance piece of code in the server.

And you can accomplish all of this and more via a Security Manager and java.net.SocketPermission 'accept' anyway.

ejpa at 2007-7-12 1:33:30 > top of Java-index,Java Essentials,Java Programming...