Search code examples
javatcpserversocket

Binding Server Socket


I'm creating simple ServerSocket based server in Java with 2 main services (let's say Service A and Service B). I'd like to know how can I bind two different methods with two different routes so:

net.tcp://localhost:5555/service-A was handled by method A and net.tcp://localhost:5555/service-B was handled by method B

I know it's possible in WCF. Can I do this with ServerSockets?


Solution

  • No that's not possible. ServerSocket will bind to a port and listen to all incoming connections. However you could handle the "routing" after the incoming connection (i.e the return from the accept method) by reading the incoming request, parsing it and passing the data to your "services". From the look of it you want to do "HTTP" like routing, I would suggest that you use some kind of HTTPServer/Service instead of the "raw" ServerSocket.