This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Tomcat and the fly likes Tomcat and sockets for receiving and transmiting data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Tomcat and sockets for receiving and transmiting data" Watch "Tomcat and sockets for receiving and transmiting data" New topic

Tomcat and sockets for receiving and transmiting data

gabi gaminde

Joined: Jul 23, 2011
Posts: 4

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?

Thanks in advance :-)
Paul Clapham

Joined: Oct 14, 2005
Posts: 19728

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.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17044

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.
gabi gaminde

Joined: Jul 23, 2011
Posts: 4
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Tomcat and sockets for receiving and transmiting data
It's not a secret anymore!