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
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
Show us your code snippet
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.codeproject.com/useritems/INIFile.asp
will read INI files (if it is a standard windows ini file format)
will read INI files (if it is a standard windows ini file format)
whoops... wrong Q ;-)
hahahaha
Sorry...
hahahaha
Sorry...
ASKER
hi experts.
please pardon my stupid codes hehe
shop.java
....
while(true) {
connectionSocket = listenSocket.accept();
inputFromClient = new BufferedReader(new InputStreamReader(connecti onSocket.g etInputStr eam()));
outputToClient = new DataOutputStream(connectio nSocket.ge tOutputStr eam());
requestMessageLine = inputFromClient.readLine() ;
tokenizedLine = new StringTokenizer(requestMes sageLine);
method = tokenizedLine.nextToken();
if (method.equals("POST")) {
.........
postLine = itemnumber + " " + itemquan + " " + userfirst + " " + userfamily + " " + cardno + " " + postcode;
clientSocket = new Socket("", bank_port);
outputToServer = new DataOutputStream(clientSoc ket.getOut putStream( ));
inputFromServer = new BufferedReader(new InputStreamReader(clientSo cket.getIn putStream( )));
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( "Transacti on 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("Connec tion from " + connectionSocket);
BufferedReader inputFromClient = new BufferedReader (new InputStreamReader(connecti onSocket.g etInputStr eam()));
DataOutputStream outputToClient = new DataOutputStream (connectionSocket.getOutpu tStream()) ;
customerDetail = inputFromClient.readLine() ; // read postLine of Store
tokenizedLine = new StringTokenizer(customerDe tail);
......
if (bank.sufficientCreditChec k(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
please pardon my stupid codes hehe
shop.java
....
while(true) {
connectionSocket = listenSocket.accept();
inputFromClient = new BufferedReader(new InputStreamReader(connecti
outputToClient = new DataOutputStream(connectio
requestMessageLine = inputFromClient.readLine()
tokenizedLine = new StringTokenizer(requestMes
method = tokenizedLine.nextToken();
if (method.equals("POST")) {
.........
postLine = itemnumber + " " + itemquan + " " + userfirst + " " + userfamily + " " + cardno + " " + postcode;
clientSocket = new Socket("", bank_port);
outputToServer = new DataOutputStream(clientSoc
inputFromServer = new BufferedReader(new InputStreamReader(clientSo
outputToServer.writeBytes(
status = inputFromServer.readLine()
if (status.equals("case1")) {
outputToClient.writeBytes(
outputToClient.writeBytes(
outputToClient.writeBytes(
}
else if(status.equals("case2"))
outputToClient.writeBytes(
outputToClient.writeBytes(
outputToClient.writeBytes(
}
else if(status.equals("case3"))
outputToClient.writeBytes(
outputToClient.writeBytes(
outputToClient.writeBytes(
}
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("Connec
BufferedReader inputFromClient = new BufferedReader (new InputStreamReader(connecti
DataOutputStream outputToClient = new DataOutputStream (connectionSocket.getOutpu
customerDetail = inputFromClient.readLine()
tokenizedLine = new StringTokenizer(customerDe
......
if (bank.sufficientCreditChec
outputToClient.writeBytes(
System.out.println("THIS PURCHASE IS AUTHORIZED");
}
else {
outputToClient.writeBytes(
System.out.println("THIS CUSTOMER DOES NOT HAVE SUFFICIENT CREDIT");
}
}
else {
outputToClient.writeBytes(
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And also in database.java
All the messages you are ending with LF only. it should be CRLF
>>outputToClient.writeByte s("case3" + '\n'); // case3: the current purchase is being made
shud be
outputToClient.writeBytes( "case3\r\n " ); // case3: the current purchase is being made
All the messages you are ending with LF only. it should be CRLF
>>outputToClient.writeByte
shud be
outputToClient.writeBytes(
>> 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
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
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
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.writeByte s("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.writeByte s("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
>>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.writeByte
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.writeByte
outputToClient.writeBytes(
outputToClient.writeBytes(
i thought lines after \r\n are regarded as entity body.
thanks!
r6