• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Buffer size or other? Please help

 
Hongqing Lu
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TestServlet runs in Tomcat, it try to get data from socket server then response to client.
It runs well when the data is small. However it will crash when the data is big enough(about 8k). The break point is in the while loop when reading data from BufferedReader in method doSendRequestTCN2.
I have tyied several methods, but all faild.
I have no idea about it.
Is it caused by buffer size or other?
Please help!!!
Sample source code:
public class TestServlet extends HttpServlet {
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
}
public void service(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException {
try {
PrintWriter out = resp.getWriter();
// get response from socket server
String response = Test.doSendRequestTCN2(requestString);
out.println(response);
} catch (Exception ex) {
ex.printStackTrace();
throw new ServletException("Exception in service method: " +
ex.getMessage());
}
}
}
class Test{
public static String doSendRequestTCN2(String input){
StringBuffer fileLines = new StringBuffer();
String response = null;
try{
client = new Socket(host_ip, host_port);

socketWriter = new PrintWriter(client.getOutputStream());
// get stream from socket server
socketReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
socketWriter.println(input);
socketWriter.flush();
String line = null;
int i = 0;
int sizeOfBuffer = 100;
char[] cbuf = new char[sizeOfBuffer];

int chars = 0;
// read from buffered stream
while ( (chars = socketReader.read(cbuf)) >= 0) {
fileLines.append(cbuf, 0, chars);
i+=1;
}
response = fileLines.toString();
socketWriter.close();
socketReader.close();
client.close();
}
catch (Exception e) {
e.printStackTrace();
throw new WMSException(e);
}
return response;
}
}
Please help!
Thanks in advance!!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic