Reputation: 330
I have two applications that need to communicate with each other using HTTP. Each one is currently a server using the HttpServer class and makes requests using Apaches HTTP client library.
One of the applications will have knowledge of the others port address before it runs, e.g. port 8000 meaning it uses this to make a HTTP get request which can processed by the other application. However if the second application wants to send a request to the first application, it will not know which port to send the response to. This occurs because the first application will send the request using the HTTP client library that will dynamically select a port, e.g. port 6543, meaning the second application cannot save that and use it later.
Example:
App 1 loads on localhost with port 8000.
App 2 loads on localhost with port 8050.
App 1 knows that app 2 is on 8050 and sends request which gets processed by app 2.
App 2 wants to send request to app 1 but it does not know its port, because the earlier request was sent using a dynamically assigned port.
Is there anyway around this situation other than the first application sending its server port number to the application?
Upvotes: 1
Views: 6119
Reputation: 68715
I am not sure what exactly are you trying to do. If a client sends an HTTP request, it can actually read the response from the same connection, why do you want to send it through a different connection and create an overhead. If you want to achieve it , then there is no way except client-servers exchanging the port information for their request response and both of them acting as client+server also. This information can be exchanged using an HTTP header in the requests.
I believe you are here confused with TCP and HTTP ports.
However the second application will get the request, process it and not know which port to send the response to because the HTTP client library will dynamically select a port, e.g. port 6543
Your server is not just picking a dynamic port to send the response, it is underlying TCP arbitrary port used to send the response. TCP is generally the underlying transport protocol used with HTTP protocol. TCP clients send a header called 'client port' to tell the server where they want to receive the response. These port numbers are picked randomly from a range of free ports by the clients.
Upvotes: 1
Reputation: 36
I did this thing just a couple of days ago.Check out some links: Java httpclient 4.1 how to send post request with configurable port
http://www.drdobbs.com/jvm/making-http-requests-from-java/240160966
Upvotes: 0