This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes Socket programming in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Socket programming in Servlet" Watch "Socket programming in Servlet" New topic
Author

Socket programming in Servlet

Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
Hi,
I have a situation. Is using sockets in a servlet
problematic or an exceptional situation? Do we need to
do something special to make them work? I have a java
program using socket communication. This runs fine as
long as I run it as a normal java application. But
when I use the same code in a servlet the
communication breaks up in the middle.
Can you suggest what needs to be corrected or point me
to some material as I've tried all the sources that I
can think of. I couldnt get any material that talks
about socket programming in a servlet. May they are
either or technologies, of which I'm not aware.
Kindly help me out ... its really urgent.
Thank you in advance,

Regards
Nirmal Kumar


Nirmal Kumar<br />If you have to aim, aim for the moon. Even if you miss you will land among the stars
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
If I found it necessary to do socket communication in a servlet context I would try to isolate ALL of the socket code in a "helper" class that could be tested outside the servlet context. I would certainly not use the request-response Thread to work directly with a socket.

I hope you realize that
the
communication breaks up in the middle.

is not a particularly useful error report - what exception or error is thrown from what code?
Bill
James Clinton
Ranch Hand

Joined: Jun 23, 2003
Posts: 190
Is using sockets in a servlet
problematic or an exceptional situation?


This is not unusual.

Finally, to re-iterate what William said, ensure you use an appropreiate pattern when building the application.
Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
Hi,
Sorry for not giving you the complete picture.
As William said I should give the complete error report ... the problem is that there are no errors or exceptions. I start the communication with a remote host from the servlet. The remote host listens on a particular port(4444). Once it receives a connection request on this port it immediately creates two new sockets( on first available ports) and sends these port numbers, which act as a control connection and data connection(as in ftp) to the requestor on the previous (4444) port. The requestor (the servlet) then communicates with the host through the new control port. The query to be searched for by the host is sent over this control port. After receiving the query the host searches in an index which resides on the host. The results are communicated one result at a time on the data port. The results are a set of objects. Object communication is possible as I'm using an Object stream for data communication. This whole setup worked fine when I was using a normal java class instead of a servlet.
The problem is communication is smooth till the host receives the query (I have tested this by printing the query on the host). After this the host sends the first result and is waiting for an acknowledgement from the servlet.The ack is never received as the servlet code falls through to completion without sending the ack or continuing the communication with the host.

Also as suggested by both of you (William and James) I was initially using a helper class for the socket communication. The problem was same as above. I was told that whenever a servlet calls the method of an included class it starts as a seperate thread or process and the servlet itself which is a different thread executes to completion without waiting for the new thread. That is why I included the socket communication in my servlet.

Could you please tell me if there is some fundamental design or technology error in this.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Nirmal Mekala Kumar:

I was told that whenever a servlet calls the method of an included class it starts as a seperate thread or process and the servlet itself which is a different thread executes to completion without waiting for the new thread.


You may be confusing 'included' with imported.

If you instanciate an imported helper class and call one of it's methods, no new thread is going to be created automatically.

If you're including anothoer Servlet with a requestDispatcher, that's another story but I don't think that's what you're trying to do.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
Hi Ben,
Actually i have tried both, i.e include as a servlet and import as a class. But both give the same problem.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

So, the Plain old Java object (POJO) works if called from the command line but not if called from a servlet?

Are you sure there are no errors being reported?
Can you post the code to the standalone version?
Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
Hi Ben,
I didnt post the code because its too long to be posted here. Dont curse me but its not even commented. So if you could tell me which part you would like to see, I could post that.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you have the same java object and you call the same method; once from that object's main method and once from a servlet
and...
it works from the main method but not from the servlet, then there has to be something different about the two environments.

You're not catching exceptions and squashing them, are you?
Are there external jar files that the method depends on?
Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
Hi Ben,
There are no external jars that the method depends on. Ofcourse, the remote host that the method communicates with, uses external jars
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Is your container running under a Java Security Policy that dissallows communication with the network?
Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
I dont know if the container is running under any security policy. But I have disabled the firewalls. I have to check about the java security policy. Also if there is a java security policy problem, is it possible that it allows a normal socket communication while stopping Object streams.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

So if you could tell me which part you would like to see, I could post that.


The Servlet part which sends the first request to the Host and receives a response and then sends request via 2 new ports received as response to the first request.

Question: Is the servlet code exactly the same as the one from your command line or did you make any adjustments for a container environment ?

cheers,
ram.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

What is your container, anyway? I have had strange things happen when I do socket communications from servlets inside Websphere. Everything works okay if the configuration is correct, but if the configuration is wrong then the socket communication still works but other random things start failing.

I'm not going into details because I don't know if they are relevant to you.
Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
I'm using Tomcat5.5 container. I have'nt done much configuration. Could you tell me what specific config parameters should I look at
Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
The Servlet part which sends the first request to the Host and receives a response and then sends request via 2 new ports received as response to the first request.

Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
Sorry for posting such a long code. But I had to.
The code is still not complete. This part of the code works just fine. After this code I send a qeury on the control port. The remote host receives this query and calls a helper class method to search for the query in its index. After searching it sends one result(Object) at a time. The problem arises here. The servlet has to acknowledge every result sent. The remote host sends the first result and is wating for the ack which it never receives. The servlet doesnt send the ack nor continues any other communication further and just falls through the rest of the code.
If the code I posted is not sooo annoying I will post the rest of the code
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Nirmal Mekala Kumar:
Sorry for posting such a long code.


Actually, I would rather that you had printed the entire source file.
I would like to see what you're doing in the catch and finally blocks.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
It looks to me like you are trying to do everything in the servlet request Thread - a bad idea IMHO (as I explained before). Bill
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489



from your earlier post

The ack is never received as the servlet code falls through to completion without sending the ack or continuing the communication with the host.




From the code in your last posting I dont see the servlet sending any acknowledgement. It opens an i/p and o/p stream, reads the port numbers from the i/p streams, does nothing with the o/p stream and closes the connection.

Here is the relevant snippet


cheers,
ram.
Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
Ben
Actually, I would rather that you had printed the entire source file.
I would like to see what you're doing in the catch and finally blocks.

I have posted the rest of the code in the next post. Please note tht it is continuation of the above code.

William
It looks to me like you are trying to do everything in the servlet request Thread -a bad idea IMHO (as I explained before). Bill

I had this part in a helper class. There are actually more things to be done. This is just a test code to see if it actually works.

ramprasad
From the code in your last posting I dont see the servlet sending any acknowledgement. It opens an i/p and o/p stream, reads the port numbers from the i/p streams, does nothing with the o/p stream and closes the connection.

Sorry for not posting the complete code earlier. The rest of the code is in the next posting. I'm not sending an ack on the initial port(4444). Once the servlet recieves the new port numbers it starts off communicating on the nwe ports.
Sarath PS
Greenhorn

Joined: Oct 22, 2004
Posts: 11
Have you given socket permission to your class files in conf/catalina.policy ?
Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
This is the rest of the code ... contiued from above
 
GeeCON Prague 2014
 
subject: Socket programming in Servlet