• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Buffer size or other? Please help

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic