• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1678
  • Last Modified:

What is a Broken Pipe exception and how do i fix it?

hi experts,

im working on a client/server program that consists of a shop and a database.  the problem arises when browser initiates a http post method to shop and the shop establishes a connection to database.  the database meant to authorize the purchase and so on then send a reply back to the shop.  once the shop receives this reply, it should act accordingly by replying an appropriate http response to the browser.  

however, upon hitting the submit button on the browser side, it seems waiting endlessly.  i have to issue another command like another get request in order to get this previous post done.  the database then would process the post form and signal back to shop.  but my shop would throw a java.net.SocketException: Broken Pipe and the browser wont be getting any reply from the shop.

So, can someone please tell me what goes wrong?

thanks

r6
0
R6
Asked:
R6
  • 6
  • 3
  • 2
  • +1
3 Solutions
 
sudhakar_koundinyaCommented:
Show us your code snippet
0
 
aozarovCommented:
It seems that you have problem with your "shop" method which cause your servlet to stuck.
Try to take a Thread dump (kill -3 or CTR-\ on linux or CTR-BREAK on windows) to see where you get stuck exactly.
For more on how to create a thread dump see: http://www.weblogic.com/docs51/techsupport/threaddump.html
When you send your send request (e.g. get-request) from the browser the browser cancel the first one and close
its socket. Not sure why the second request cause the handling of the first request on the server to complete (maybe the detection
of the closed socket??) but it seems that you are still trying to send back the response for the first request and there you get this java.net.SocketException because that socket is not there anymore (was closed by the browser).
0
 
TimYatesCommented:
>> java.net.SocketException: Broken Pipe

This generally means that the client has closed that connection (they have navigated to another page, or shut the browser)

I agreee with sudhakar_koundinya that some code would be good...

Have you tried putting debug (System.out.println) into your shop sevlet?

You should be able to narrow it down to the line that it is hanging on...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
TimYatesCommented:
http://www.codeproject.com/useritems/INIFile.asp

will read INI files (if it is a standard windows ini file format)
0
 
TimYatesCommented:
whoops...  wrong Q ;-)

hahahaha

Sorry...
0
 
R6Author Commented:
hi experts.
please pardon my stupid codes hehe

shop.java
....
while(true) {
                  connectionSocket = listenSocket.accept();
                  inputFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                  outputToClient = new DataOutputStream(connectionSocket.getOutputStream());
                  requestMessageLine = inputFromClient.readLine();
                  tokenizedLine = new StringTokenizer(requestMessageLine);
                  method = tokenizedLine.nextToken();
                     if (method.equals("POST")) {
.........
                     postLine = itemnumber + " " + itemquan + " " + userfirst + " " + userfamily + " " + cardno + " " + postcode;
                      clientSocket = new Socket("", bank_port);
                                    
                   outputToServer = new DataOutputStream(clientSocket.getOutputStream());
                    inputFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                      outputToServer.writeBytes(postLine + '\n');
                      status = inputFromServer.readLine();
                      if (status.equals("case1")) {
                                          outputToClient.writeBytes("HTTP/1.0 200 OK\r\n");
                                          outputToClient.writeBytes("\r\n");
                                          outputToClient.writeBytes("<B>The user information entered is invalid</B>");
                                    }
                                    else if(status.equals("case2")) {
                                          outputToClient.writeBytes("HTTP/1.0 200 OK\r\n");
                                          outputToClient.writeBytes("\r\n");
                                          outputToClient.writeBytes("<B>Your account does not have sufficient credit for the requested transaction</B>");
                                    }
                                    else if(status.equals("case3")) {
                                          outputToClient.writeBytes("HTTP/1.0 204 No Content\r\n");
                                          outputToClient.writeBytes("\r\n");
                                          outputToClient.writeBytes("Transaction Approved\r\n");
                                    }
                                    
                                    clientSocket.close();

......................
the broken pipe exception is thrown when the control reaches the first outputToClient.writeBytes of each case.


database.java

.....
Socket connectionSocket = welcomeSocket.accept();
                  System.out.println("Connection from " + connectionSocket);
                  
                  BufferedReader inputFromClient = new BufferedReader (new InputStreamReader(connectionSocket.getInputStream()));
                  DataOutputStream outputToClient = new DataOutputStream (connectionSocket.getOutputStream());
                  
                  customerDetail = inputFromClient.readLine();  // read postLine of Store  
                  tokenizedLine = new StringTokenizer(customerDetail);
......
if (bank.sufficientCreditCheck(dbReader, amount)) {
                              outputToClient.writeBytes("case3" + '\n');  // case3: the current purchase is being made
                              System.out.println("THIS PURCHASE IS AUTHORIZED");
                         }
                         else {
                              outputToClient.writeBytes("case2" + '\n');  //  case2: this identified customer does not have sufficient credit
                              System.out.println("THIS CUSTOMER DOES NOT HAVE SUFFICIENT CREDIT");
                         }
                   }
                   else {
                        outputToClient.writeBytes("case1" + '\n');  //  case1: non-existing customer
                        System.out.println("not existing customer!");
...........

these codes are the relevent section, i hope they show u experts where the problems might be!

thanks

r6

0
 
sudhakar_koundinyaCommented:
Actually Http Based messages should end with CRLF

Now considering your codes
Below code should be something like this
>>outputToClient.writeBytes("HTTP/1.0 200 OK\r\n");
>>outputToClient.writeBytes("\r\n");
>>outputToClient.writeBytes("<B>The user information entered is invalid</B>");

outputToClient.writeBytes("HTTP/1.0 200 OK\r\n");
outputToClient.writeBytes("<B>The user information entered is invalid</B>");
outputToClient.writeBytes("\r\n");

Check if that helps you

Regards
Sudhakar


0
 
sudhakar_koundinyaCommented:
And also in database.java

All the messages you are ending with LF only. it should be CRLF

>>outputToClient.writeBytes("case3" + '\n');  // case3: the current purchase is being made

shud be

outputToClient.writeBytes("case3\r\n" );  // case3: the current purchase is being made
0
 
sudhakar_koundinyaCommented:
>> connectionSocket = listenSocket.accept();

From the above code, I am assuming you are developing your own http server that fulfills your specific purposes. Is that right??

If not your code is wrong
Idea should be something like this as an example

Regards
Sudhakar


0
 
sudhakar_koundinyaCommented:
>>Idea should be something like this as an example

Here is the link
http://www.javaalmanac.com/egs/java.net/Post.html

And also check the examples from here too
http://www.javaalmanac.com/egs/java.net/pkg.html


I once again presume that you are developing your own http server. If not your idea of coding is wrong and I suggest to go through the examples posted in above links

Regards
Sudhakar

0
 
sudhakar_koundinyaCommented:
0
 
R6Author Commented:
hi sudhakar,
>>From the above code, I am assuming you are developing your own http server that fulfills your specific purposes. Is that right??
correct.

>>And also in database.java
All the messages you are ending with LF only. it should be CRLF
>>outputToClient.writeBytes("case3" + '\n');  // case3: the current purchase is being made
why all the messages have to end with CRLF?  By the way, the shop and database are interacting over TCP only, no http protocol involved.

>>outputToClient.writeBytes("HTTP/1.0 200 OK\r\n");
outputToClient.writeBytes("<B>The user information entered is invalid</B>");
outputToClient.writeBytes("\r\n");
i thought lines after \r\n are regarded as entity body.

thanks!

r6
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now