how to get connected client ip address?

i am using SocketServer in java to connect my server with client in flash..i would like to retrieve the client ip address once the client connect to java...appreciate your help.. thank you..
[204 byte] By [gracecheaha] at [2007-11-27 8:13:10]
# 1
getInetAddress() on the Socket returns the InetAddress of the remote computer.
cotton.ma at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 2

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

gracecheaha at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 3
Please post your code.
cotton.ma at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 4

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">

Subbu_javaa at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 5
> and the value i got always is 0.0.0.0That's because you called it on the ServerSocket instead of the accepted Socket.
ejpa at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 6

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.

gracecheaha at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 7
wat do u mean by accepted socket?
gracecheaha at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 8

> 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.

cotton.ma at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 9

> 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.

cotton.ma at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 10
y do u think my code will hav many prob? yes, i still in learning level..i want to get the IP of the client because i want to keep the record which data sending in is from which client.Message was edited by: gracecheah
gracecheaha at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 11

> 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?

cotton.ma at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 12

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">

gracecheaha at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 13

> 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.

cotton.ma at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 14
ok. thanks.. i will try later..erm, may i know 1 port can be connected by more than 1 client?
gracecheaha at 2007-7-12 19:57:40 > top of Java-index,Java Essentials,Java Programming...
# 15

> 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.

cotton.ma at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 16
thank you ^^
gracecheaha at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 17

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);

}

}

}

tripuranenia at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 18

> 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.

cotton.ma at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 19
thank you, but my client is writing in flash.. not java
gracecheaha at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 20
> Hi plz try this code :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.
ejpa at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 21
yeah, i have posted the code to get all the clientsok i think u got the solution now,thenIn 1 port , we can have more than 1 clientbut in 1 port we cannot have more than 1 serverSocketanyone pls correct me if am wrong.thanxsubbu
Subbu_javaa at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 22

> 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.

keeskista at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 23

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.

ejpa at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 24
But as I said the client was Flash, so I needed to output XML. Of course uncompressed XML is probably the worst format imaginable for network transport, but Flash doesn't accept other formats. And so I used PrintWriter to send out the strings.
keeskista at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 25
Still no reason to use PrintWriter. You can use BufferedWriter or DataOutputStream to send out lines, if that's what you need to do, without having all the exceptions swallowed. And if the target is Flash you should probably not use any kind of Writer.
ejpa at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 26
> And if the target is Flash you> should probably not use any kind of Writer.Why not?(btw, my questions are just interest, I've created the thing three years ago)
keeskista at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 27
Does Flash understand Unicode?
ejpa at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...
# 28
It supports UTF-8, not sure about UTF-16.
keeskista at 2007-7-21 22:32:49 > top of Java-index,Java Essentials,Java Programming...