Broken Pipe Error-unable to write to socket for second attempt
843829Sep 3 2003 — edited Sep 3 2003Hello,
I am opening socket connection wuth C-server program.
I am able to write and read from socket for first time but when tried to write second time in the same sequence gives broken pipe error.
As seen in the code initialize() methods works perfectly fine,
Pipe broken error is encountered for processRequest() method for writeBytes(begnmsg+"\n") method.
Please suggest solution asap.
Following is the code
import java.io.*;
import java.net.*;
public class PowerModelClient {
Socket kkSocket = null;
BufferedReader in = null;
PrintWriter out = null;
DataOutputStream os = null;
char m_eomChar = '\n';
public PowerModelClient(){
try{
//Opens socket connection with C-server program,
openConnection();
System.out.println("connected to server");
//Sends data base information to C-server program and receives acknowledgement from it.
String recv = initialize();
System.out.println("Initialize successful"+recv);
try{
// Sleep for a short period of time
Thread.sleep ( 10000 );
}
catch (InterruptedException ie) {}
System.out.println("Initialize successfu 22l");
//Sends data base information to C-server program and receives acknowledgement from it.
processRequest();
//Closes socket connection
closeConnection();
System.out.println("Comesout to do QUIT ");
}
catch(Exception e){
System.out.println("Error in main::"+e.toString());
}
}
public static void main(String[] args) throws IOException {
new PowerModelClient();
}
/*
Opens socket connection with C-server program,
*/
public void openConnection() throws IOException{
try {
kkSocket = new Socket("server",port); os = new DataOutputStream(kkSocket.getOutputStream());
//out = new PrintWriter(kkSocket.getOutputStream(),true);
in = new BufferedReader(((java.io.Reader)(new InputStreamReader(kkSocket.getInputStream()))));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: server.");
System.exit(1);
} catch (IOException e) {
System.out.println("Couldn't get I/O for the connection to: server."+e.getMessage());
System.exit(1);
}
}
/*
Sends data base information to C-server program and receives acknowledgement from it.
This makes the C program to connect with Database.
The data send to socket should be tagged between @BEGMSG@\nand @ENDMSG@\n
Similary data obtained from socket will be tagged between @BEGMSG@\n and @ENDMSG@\n
*/
public String initialize() throws IOException{
int ibyteread = 0;
String fromServer = "";
String beginmsg=padString("@BEGMSG@");
String endmsg = padString("@ENDMSG@");
String recv = "";
if (kkSocket != null ) {
try {
// Set the socket timeout for ten seconds
// kkSocket.setSoTimeout(10000);
//String data = padString("INIT\tabcd\tisderssddsdg\tdrtrs1\twetewte"); os.writeBytes(beginmsg+m_eomChar);
in.read();
os.flush();
os.writeBytes(data+m_eomChar);
in.read();
os.flush();
os.writeBytes(endmsg+m_eomChar);
os.flush();
recv = readResponse(in);
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
}
// Exception thrown when network timeout occurs
catch (InterruptedIOException iioe)
{
System.err.println ("Remote host timed out during read operation");
}
catch (IOException e) {
System.err.println("IOException: " + e);
}
catch (Exception e) {
System.err.println("Exception: " + e);
}
}
return recv;
}
/*
Sends data after database connection is established to socket for entitlement processing
and will receive acknowledgement from server.
*/
public void processRequest() throws IOException{
int ibyteread = 0;
String beginmsg=padString("@BEGMSG@");
String endmsg = padString("@ENDMSG@");
String fromServer = "";
if (kkSocket != null ) {
System.out.println("processRequest");
try {
kkSocket.setSendBufferSize(1024);
kkSocket.setSoTimeout (10000);
kkSocket.setTcpNoDelay(true);
os.writeBytes(beginmsg+m_eomChar);
int i = in.read();
System.out.println("Data read is :"+i);
os.writeBytes(padString("PAEL\t1056209\t269732\t02/01/2003")+m_eomChar);
in.read();
System.out.println("Data read is :"+i);
out.flush();
System.out.println("Data read is :"+ibyteread);
os.writeBytes(endmsg+m_eomChar);
ibyteread = in.read();
readResponse(in);
System.out.println("-----end Processing:--------");
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
}
// Exception thrown when network timeout occurs
catch (InterruptedIOException iioe)
{
System.err.println ("Remote host timed out during read operation");
}
catch (Exception e) {
System.err.println("Exception: " + e);
}
}
}
/*
The socket requires data to be of length 1024 character, so the renaining length is padded with empty string"\0"
*/
public String padString(String value){
StringBuffer strBuff = new StringBuffer(value);
while(strBuff.toString().length() != 1023){
strBuff.append("\0");
}
//System.out.println("the length is "+ strBuff.toString().length());
return strBuff.toString();
}
/*
Close socket
*/
public void closeConnection() throws IOException{
in.close();
os.close();
kkSocket.close();
}
public String readResponse(BufferedReader in) {
boolean flag = false;
java.lang.StringBuffer stringbuffer = new StringBuffer(1024);
try {
// for(char ac[] = new char[1]; in.read(ac, 0, 1) != -1;) {
char ac[] = new char[1024];
if (in.read(ac, 0, 1) != -1) {
stringbuffer.append(ac[0]);
for(; in.read(ac, 0, 1) != -1 && ac[0] != m_eomChar; stringbuffer.append(ac[0]));
if(ac[0] == m_eomChar) {
System.out.println("received '" + stringbuffer.toString() + "'");
} else {
System.out.println("Message received by client was not properly terminated and was ignored."+stringbuffer.toString());
}
} else {
System.out.println("WARN: no response?");
}
} catch(java.io.IOException ioexception) {
System.out.println("Error in client " + ioexception + "Killing client.");
}
return stringbuffer.toString();
}
}