I am new to socket programing and I have some trouble understanding how to use them with Tomcat. Let me explain it in more detail. I want to be able to send some messages from my web application to other application that is not running on Tomcat but in the same network. I have to send and receive messages from and to this application. Can I use port 8080 to do so? (the one that Tomcat is using) How can I capture messages sent from the other application? I think Tomcat is alredy a server so I don't need to build a server class for that. In fact I've tried it but it seems to block my application.
Can someone give me a hint? Maybe I am talking nonsense and I must use another port?
Of course you can send messages to that server. You would do that exactly the same way as if you were doing it from an ordinary Java application. The fact that your code would be running in Tomcat is just about irrelevant. Likewise it's irrelevant that Tomcat happens to be a server. As for what port to use, you would use the port which that server is listening at.
Only one application at a time can use a specific port number on a machine. But like Paul said, the port number you use is the one that the target server is listening on, not the one that Tomcat is listening on.
If some other machine is listening on its port 8080, that's no problem. If you tried to start another server listening on port 8080 on the same machine that's running Tomcat, the OS wouldn't let it, since port 8080's already in use. Or, for apps like Tomcat, the app itself may come up, but it won't listen on the occupied port. (Tomcat displays a "port in use" message in the log).
An IDE is no substitute for an Intelligent Developer.
Joined: Jul 23, 2011
Many thanks for the answers The problem I have is that when I try to start a listening port in the webapp it blocks it. I try to use port 7777 (as on the code example I have) and the server blocks on startup. I have a listener class (that I configure in the Web.xml file) that tries to open a port for listening.