thank you for reply my question..
just wanna confirm, getInetAddress() is get the ip of client who connected to my server of get the ip address of the server?
and the value i got always is 0.0.0.0
snyone know why? appreciate your help....
Message was edited by:
gracecheah
Hi grace,
i think this following code may help u,
InetAddress localHost = InetAddress.getLocalHost();
InetAddress[] all_IPs = InetAddress.getAllByName(localHost.getHostName());
for (int i=0; i<all_IPs.length; i++) {
System.out.println("IP address = " + all_IPs);
}
first, we are fetching the host(server) name.
based upon the server name we r then fetching the cliet machine IP which are all connected to the server.
--Subbu">
thank you for reply my question..
ServerSocket s = new ServerSocket(9999);
ip=s.getInetAddress().toString();
System.out.println( ip);
my code is as above..
Subbu, how if i do not want all ip which connected to server but i just want like, now a client send data to server, i want to check its ip.
> thank you for reply my question..
>
> ServerSocket s = new ServerSocket(9999);
> ip=s.getInetAddress().toString();
> ystem.out.println( ip);
>
> my code is as above..
Yes your code is wrong. In exactly the way ejp said it would be.
>
> Subbu, how if i do not want all ip which connected to
> server but i just want like, now a client send data
> to server, i want to check its ip.
Never mind subbu, he seems crazy.
You should be calling getInetAddress on the Socket, as I told you in the first post not ServerSocket.
> wat do u mean by accepted socket?
ServerSocket ss = new ServerSocket(...);
Socket s = ss.accept(); // s is the ACCEPTED socket
s.getInetAddress();//called on socket not server socket
But I do get the feeling your code may have alot of other problems. Call it a hunch.
> y do u think my code will hav many prob? yes, i
> still in learning level..
>
Yes. Because of that. It's to be expected.
> i want to get the IP of the client because i want to
> keep the record which data sending in is from which
> client.
>
Well. Did you get it working yet or not?
no...
this can get, but it will get all client which are connected..
InetAddress localHost = InetAddress.getLocalHost();
InetAddress[] all_IPs = InetAddress.getAllByName(localHost.getHostName());
for (int i=0; i<all_IPs.length; i++) {
System.out.println("IP address = " + all_IPs);
}
and this will get 127.0.0.1..is not the client ip actually..
Socket incoming = s.accept();
ip=incoming.getInetAddress().toString();
do u hav any idea? thanks
Message was edited by:
gracecheah">
> no...
>
> this can get, but it will get all client which are
> connected..
>
> InetAddress localHost =
> InetAddress.getLocalHost();
> InetAddress[] all_IPs =
> InetAddress.getAllByName(localHost.getHostName());
> for (int i=0; i<all_IPs.length; i++) {
> System.out.println("IP address = " + all_IPs);
> }
What did I say about shubooboo? He's crazy. Ignore him.
> and this will get 127.0.0.1..is not the client ip
> actually..
> Socket incoming = s.accept();
> ip=incoming.getInetAddress().toString();
>
> do u hav any idea? thanks
>
That last one works. It's returned 127.0.0.1 because YOU are connecting from localhost.
So connect from somewhere else and see what happens.
It works.
> ok. thanks.. i will try later..
>
> erm, may i know 1 port can be connected by more than
> 1 client?
Yes.
A Socket is a unique tuple based on four pieces of information. The host and port on one side and the host and port on the other.
You can have multiple clients connect to the same port because one of or both of the remote port (the port on the client side) and the remote (client) address will be different.
Hi plz try this code :
public ClientSoc()
{
super();
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) throws Exception
{
// TODO Auto-generated method stub
Socket sc = new Socket("192.168.2.205",9500);
if(sc.isBound()){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(sc.getOutputStream());
while(true) {
String keys = br.readLine();
if(keys != null){
pr.println(keys);
pr.flush();
}
}
}
place the above code in ur client class.
place the below code in ur server class
public ServerCla()
{
super();
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) throws Exception
{
// TODO Auto-generated method stub
ServerSocket ssc = new ServerSocket(9500);
Socket soc = ssc.accept();
if(soc.isBound()){
//BufferedReader br = new BufferedReader(new InputStream(soc.getInputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(soc.getInputStream()));
while(true){
String g = br.readLine();
if(g!=null){
System.out.println("message from client:"+g);
}
}
}
> Hi plz try this code :
>
Why?
Looking over that code I see that it is totally irrelevent to the question the OP asked. So I can only assume you have some sort of question.
Please start a new thread for your new question and when you post code please use the code formatting tags.
> Please don't post amy more rubbish like this. Both
> the loops ignore EOS and will therefore spin
> mindlessly forever. And you shouldn't use
> PrintWriters or PrintStreams over the network. And
> the isBound() tests are utterly pointless.
Not related to the OPs problem, but what should you use for sockets then? I've made a similar Java server/Flash client three years back as a school assignment, but I also used PrintWriter and BufferedReader, the same classes I used for reading/writing normal text files.
PrintWriter swallows all exceptions. It's OK for log files where you don't care too much, and the console where there won't be any. It's not acceptable for network applications where you must know (a) about data not being sent and (b) liveness of the connection. Use DataOutputStream. And don't print lines to the network, send primitives or objects.