• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Socket programming in Servlet

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Nirmal Mekala Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is your container running under a Java Security Policy that dissallows communication with the network?
 
Nirmal Mekala Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you given socket permission to your class files in conf/catalina.policy ?
 
Nirmal Mekala Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the rest of the code ... contiued from above
reply
    Bookmark Topic Watch Topic
  • New Topic