Hello,
I trying to create a messenger(just for practice, so nothing fancy!). I created the server and client programs.
Suppose a client(user A) initiated a connection with the server, so it's online. Now, another user(user B) wants to chat with that user(A). How do I give User B the IP address of User A. I have tried this one,

String ipAddress = connectSocket.getInetAddress().getHostAddress();

this returns me the IP address. But dont User B needs the port number of User A also to connect with that user(A)?

If yes, then how do i do it?

Any advice will help.

thanks.

Sounds like you expect A to connect directly to B? That implies that B has a server running an open server port that's accessible from the internet.
Normally an app like this uses the server to route messages from one user to another, so no user needs to know anything about the other's connection, and the clients do not need to operate in server mode.

@jamesCherrill,
yeah. I want something like peer-to-peer connection. I actually haven't given it much of thought. But client-server-client sounds like too much hassle to chat!

If user-to-user is too complicated then I can do the centralized server connection.

But thanks for the reply.

client-server-client really isn't hard at all. You already have the code for receiving messages from a client and sending them to a client, so the forwarding code is a very small and easy addition.
On the other hand, configuring machines that are probably behind a NAT router and one or more firewalls to receive incoming connections is a total minefield of problems.

yeah, now that you mention it!

Okay, thanks man.

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.