0

I am creating a chat application in Java. I am having no trouble having a server accept multiple client socket connections and maintaining multiple threads. I already managed to get them to send and receive messages.

However, I cannot figure out how to create multiple socket connections to the same specific client. On both the server and the client I already have a thread for sending and receiving chat messages. I would like to have another connection on top of that to do background stuff, such as sending files, while continuing to relay chat messages.

For example, Client1 and Client2 are messaging each other through the server, which keeps a database of accounts and chat messages. I would like to be able to send a file from Client1 to Client2 and vice versa.

Connection1: Client1 <--message--> Server <--message--> Client2
Connection2: Client1 --file--> Server --file--> Client2 
or 
Connection2: Client1 --file--> Client2

I was thinking about how each clients have IP addresses and I could use those to create the new connection. However, since I'm running this on localhost, all of the IP addresses are identical so I cannot do it this way.

2
  • IRC half-solved this problem a long time ago. Take a look at how DCC handles it. (It's not all that NAT-friendly, but you're not going to find much that is unless the server ends up involved in every byte transferred.) Commented Jan 3, 2015 at 17:57
  • Clients connect to servers, not the other way round. Commented Jan 3, 2015 at 18:54

1 Answer 1

1

On the server side: Open two ServerSockets using two different ports.

On the client side: Open two client Sockets one connecting to the Chat Server and the other to your File Server.

But, I am not sure if you win something with two connections on the same NIC. I suggest you distinguish the commands when you receive them (chat or other-messages) and serve them using two different queues (or using one queue and set hierarchy to your tasks). You may also use a ThreadPool to execute your tasks.

In any case, you could use Non-blocking I/O (NIO), take a look at Netty and Jetty projects that support it.

now for Connection2: Client1 --file--> Client2

Using a P2P between clients would improve performance and server's network resources, but of course you lose anonymity and it is much more complex. Assuming a chat conversation is only between two users (maintaining a N-N P2P chat is by far a more complex task) you can equip client devices with ServerSocket support and always make one of them work as a Server and the other as client.

In fact, you can use your existing ServerSocket to just synchronize users (use some commands for that) and guide the users. Use this socket only as user-matching server. When you need to initiate a chat between two users, ask one of them to run a ServerSocket and the other to run a client socket. Inform them for the IPs of each other and let them connect between themselves. Your server is just a mediator now, only initiating connections.

*If you run a P2P solution why not use this for chat + file-service? (if you don't need logging of the messages, you can do it this way so your Server does nothing more than being a referee).

**Regarding the port numbers to be used for P2P, you may need to set NAT rules to your router.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.