Link to home
Start Free TrialLog in
Avatar of R6
R6

asked on

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
Avatar of sudhakar_koundinya
sudhakar_koundinya

Show us your code snippet
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
http://www.codeproject.com/useritems/INIFile.asp

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

hahahaha

Sorry...
Avatar of R6

ASKER

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
>> 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


>>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

Avatar of R6

ASKER

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