今天, 寫一個應用向web服務器(tomcat)發送大量數據的時候就出現 Software caused connection abort: socket write error的問題了
原先的代碼大體如下
1 //out => socket.getOutputStream(); 2 for(int i = 0; i < length; i++) 3 { 4 . . . 5 out.write(message.getByte()); 6 }
基本上, 當數據量超過100的時候就會出現 Software caused connection abort: socket write error
由於 Http協議是屬於短連接的 即交互之后即斷開連接 所以上面的長連接方式是有問題的
該代碼如下
1 for(int j = 0; j < length; j++) 2 { 3 socket = new Socket(address, port); 4 out = socket.getOutputStream(); 5 message = xxx; 6 out.write(message.getBytes()); 7 out.flush(); 8 out.close(); 9 }
就可以無限量的發送請求了
所以在http請求的時候 每條請求都用一個socket來發送應該就不會有問題了
另 附上直接向服務器發送請求的數據前綴
1 String SEND_PREFIX = "POST /SunnyRunLoad/datareceive HTTP/1.1" + DP + 2 "Host:localhost" + DP + 3 "Content-Type:application/x-www-form-urlencoded" + DP + 4 "Content-Length:53" + DP + DP;
其中 DP = "\r\n"