Get port Number of the client Socket program

Hi All,

I am new to socket programming.I have a client and server socket program which communicates between them.I need to get the port number of the client program programmatically in the server socket program.The code for the server socket which i mentioned below uses a port number which has been hard coded.This value has to be dynamically get by the server socket program.

Is there any sample codes or links that might be useful

Server.java

import java.net.*;

import java.io.*;

publicclass Server{

publicstaticvoid main(String[] ar){

[b]/*This port number has to received by the server program*/ Now the port numbers are hard coded [/b]

[b]int port = 6666;/*// just a random port. make sure you enter something between 1025 and 65535.*/[/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();

}

}

}

[3420 byte] By [Ananth-Ra] at [2007-11-27 1:58:10]
# 1

hi,

take a look at "getRemoteSocketAdress()".

http://java.sun.com/javase/6/docs/api/java/net/Socket.html#getRemoteSocketAddress()

i hope that works, you can also send the client port to the server manually, such as with getLocalSocketAddress.

Why you want to know the Client-Port?

best regards marcel

Message was edited by:

marcel_hecker

marcel_heckera at 2007-7-12 1:34:32 > top of Java-index,Core,Core APIs...
# 2

Do you mean it should be received by user input, or that it should somehow detect it automatically?

For the user input part, just use DataInputStream to read a String from System.in and convert it to an integer using Integer.valueOf.

There is no way to detect it automatically from a connection other than:

1. Listening to ALL ports, and seeing which comes up.

2. Monitoring the connection on a sniffer level, which is somewhat beyond the scope Java allows.

In any way, the concept of a dynamic port for the server to use is wrong for two reasons:

1. It's the duty of the client to connect to the right port on the server, not the other way around. Generally, a single server listens to a single port, but many clients may connect to the same server, and must therefore confirm to said single port.

2. A port number generally identifies to program that uses it. The main point of having many ports is having many programs acting as various servers on the same computer. A server may allow a configurable port, if the server owner takes the responsibility of informing potential clients of the actual port externally to the program(e.g. publishing it on a web page, along with the server's IP), but that's it.

SlugFillera at 2007-7-12 1:34:32 > top of Java-index,Core,Core APIs...
# 3
Hi slug,i think he will use a non dynamic server-port and wants to know the localport from the connected client, or?greets
marcel_heckera at 2007-7-12 1:34:32 > top of Java-index,Core,Core APIs...
# 4

@marcel_hecker:

> The code for the server socket which i mentioned below uses a port number which has been hard coded.This value has to be dynamically get by the server socket program.

The port he's looking for to make dynamic is the port made static in the code. If you look at said code, you see that the static port is only used in the ServerSocket constructor, that is, it is the port used for listening.

This sort of contradicts your interpretation. Moreover, the example code he's showing does not require the client port at all. I don't know how much is similar and how much is different between that example and what he's trying to code, but it does give a generic assumeable regarding the intention of the question.

Then again, I may be completely wrong about this, but since there isn't a second post from this person, I have to go by what information is available.

SlugFillera at 2007-7-12 1:34:32 > top of Java-index,Core,Core APIs...