Marek Čačko
Marek Čačko

Reputation: 1010

multiple sockets between server and client

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?

Upvotes: 1

Views: 508

Answers (2)

user207421
user207421

Reputation: 310860

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.

Upvotes: 2

iil
iil

Reputation: 154

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

Upvotes: -1

Related Questions