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

