异常:Softwarecaudconnectionabort:socketwriteerror 最近在实验《Tomcat与Java Web开发技术详解》书中例⼦程序时,发现⼀个异常:Software caud connection abort: socket write error ⽤Java 套接字创建HTTP 客户与服务器程序
服务器程序
package ver;
import java.io.InputStream;
import java.io.OutputStream;
*;
public class HTTPServer {
/**
* @param args
*/
public static void main(String[] args) {
int port;
ServerSocket rverSocket;
try{
port = Integer.parInt(args[0]);
}catch(Exception e){
System.out.println("port = 8080 (默认)");
port = 8080;
}
try{
rverSocket = new ServerSocket(port);
System.out.println("服务器正在监听端⼝: "+LocalPort());
while(true){
try{
//等待客户的TCP连接请求
final Socket socket = rverSocket.accept();
System.out.println("建⽴⼀个客户端的新的TCP连接,该客户的地址为:"+InetAddress()+":"+Port());
rvice(socket);番茄虾仁意面
交通标志
}catch(Exception e){
e.printStackTrace();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void rvice(Socket socket)throws Exception {
// TODO Auto-generated method stub
/*读取HTTP请求信息*/
InputStream socketIn = InputStream();
Thread.sleep(500);
int size = socketIn.available();
byte[] buffer = new byte[size];
String request = new String(buffer);
System.out.println(request);
/*解析HTTP请求*/
//获得HTTP请求的第⼀⾏
String firstLineOfRequest=request.substring(0,request.indexOf("\r\n"));
//解析HTTP请求的第⼀⾏
String[] parts=firstLineOfRequest.split(" ");
String uri = parts[1];
/*决定HTTP响应正⽂的类型,*/
String contentType ;
散文投稿
if(uri.indexOf("html")!=-1||uri.indexOf("htm")!=-1)
{
contentType="txt/html";
}
电脑启动后黑屏
el if(uri.indexOf("jpg")!=-1||uri.indexOf("jpeg")!=-1)
{
contentType = "image/jpeg";
}
el if(uri.indexOf("gif")!=-1)
{
contentType = "image/gif";
}
el
{
contentType = "application/octet-stream";
}
/*创建HTTP响应结果*/
//HTTP响应第⼀⾏
String responFirstLine = "HTTP/1.1 200 OK \r\n";
//HTTP响应头
String responHeader = "Content-Type:"+contentType+"\r\n\r\n";
//获得响应正⽂数的输⼊流
InputStream in = ResourceAsStream("root/"+uri); /*发送HTTP响应结果*/
OutputStream socketOut = OutputStream();
//发送HTTP响应的第⼀⾏
socketOut.Bytes());
//发送HTTP响应的头
socketOut.Bytes());
//发送HTTP响应的正⽂
int len=0;
buffer = new byte[128];
while((ad(buffer))!=-1)
{
socketOut.write(buffer,0,len);
}
Thread.sleep(1000);
socket.clo();
}
}
客户端程序
package client;
import java.io.InputStream;
import java.io.OutputStream;
Socket;
public class HTTPClient {
/**
* @param args
*/
public static void main(String[] args) {
//确定HTTP请求的uri
String uri="index.htm";
if(args.length!=0)
{
uri=args[0];
doGet("localhost",8080,uri);
}
}
public static void doGet(String host, int port, String uri) {
Socket socket = null;
try
{
socket=new Socket(host,port);
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
//创建HTTP请求
StringBuffer sb = new StringBuffer("GET "+uri+" HTTP/1.1\r\n");
//HTTP请求头
sb.append("Accept: */*\r\n");
sb.append("Accept-Language: zh-cn\r\n");
sb.append("Accept-Encoding: gzip, deflate\r\n");
sb.append("Ur-Agent: HTTPClient\r\n");
sb.append("Host: localhost:8080\r\n");
sb.append("Connection: Keep-Alive\r\n\r\n");
//发送HTTP请求
快餐外送OutputStream socketOut = OutputStream();
socketOut.String().getBytes());
Thread.sleep(2000);
//接收响应结果
InputStream socketIn = InputStream();
int size = socketIn.available();
byte[] buffer = new byte[size];
System.out.println(new String(buffer));
}
大连现代博物馆
catch(Exception e)
{
梦见别人结婚是什么征兆e.printStackTrace();
}
finally
{
try
{
socket.clo();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
执⾏过程:1.在eclip中对服务端程序debug启动
2.在eclip中对客户端程序debug启动,配置启动参数“hello1.htm”漫画人物大全
3.在服务端红⾊部分,函数rvice中设置断点,单步调试,出现异常Software caud connection abort: socket write error
分析:单步调试耗时⽐较长,服务端接收的socket已经被关闭。