1

I have a java server aplication which comunicate with multiple clients via SocketChannel. On this channel, client sends a request and server sends an answer. Now I want add feature that server can sends file to the client. I don't want send it via the socket whith I am using for comunication so is good idea to have more sockets between one client and one server? If yes how to handle them? Have I use something like this?

SocketChannel socket = serverSocket.accept()
if(!addressSet.contains(socket.address)) {
    it is comunicate socket
}
else {
    it is date transfer socket
}

or is there some better way?

2
  • You have to use same socket from which the client is connected. No matter its a data transfer socket or communicator socket. Commented Apr 14, 2014 at 20:46
  • But if a client creates two sockets it would be works? That I would choose which one to use to send a message or data. On the other hand, I realize that I cannot be 100% sure that two sockets with same IP is from same client (it can be router and two different clients) Commented Apr 14, 2014 at 21:01

2 Answers 2

2

Create a new ServerSocket on a random port once you accept a client connection, then tell him that port number. He should then connect to that as the data connection. Then,have the server accept one connection from it, which better be from him, then close that ServerSocket. It's not foolproof but it's reasonably strong.

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

1 Comment

"Then,have the server accept one connection from it, which better be from him" I'm a bit late but how do I check if it's the same client?
-1

Yes there is a better way. Use ServerSocketChannel and the method public abstract SocketChannel accept() throws IOException

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.