2012-04-15 117 views
1

我有一个情况,其中客户端建立与服务器的连接后,收到一个文件,当使用相同的连接(持久)我最终得到上述错误。 下面是实现代码:软件导致连接中止:套接字写入错误

Scanner in = new Scanner(file); 

this.clientSocket = new Socket(this.host,this.port); 

this.os = new DataOutputStream(this.clientSocket.getOutputStream()); 

this.is = this.clientSocket.getInputStream(); 

while(in.hasNextLine()){ 
    newFile = in.nextLine(); 
    System.out.println(newFile); 

    this.os.writeBytes(newFile + '\n'); 
    this.os.flush(); 
    scanFileList(); 
    writeFile(); 

} 

,并在服务器端执行的是:

final class HttpRequest implements Runnable { 
    final static String CRLF = "\r\n"; 
    Socket socket; 
    static String dir; 
    BufferedOutputStream outToClient = null; 
    // Constructor 
    public HttpRequest(Socket socket) throws Exception { 
     this.socket = socket; 
     dir = "C:\\Users\\"; 
} 


     // Implement the run() method of the Runnable interface. 
public void run() { 
    try { 

     // Get a reference to the socket's input and output streams. 
     InputStream is = socket.getInputStream(); 
     outToClient = new BufferedOutputStream(socket.getOutputStream()); 
     processRequest(is,outToClient); 
     } catch (Exception e) { 
       System.out.println(e); 
     } 
} 

private void processRequest(InputStream is,BufferedOutputStream os) throws Exception { 

    // Set up input stream filters. 
    BufferedReader br = new BufferedReader(new InputStreamReader(is)); 

    // Get the request line of the HTTP request message. 
    String fileName = br.readLine(); 



    // Prepend a "." so that file request is within the current directory. 
    System.out.println(fileName); 
    // Open the requested file. 
    File myFile = null ; 

    boolean fileExists = true ; 

    myFile = new File(dir + fileName); 

    FileInputStream fis = null ; 
    try { 
     fis = new FileInputStream(dir + fileName); 
     System.out.println(fis); 
    } catch (FileNotFoundException e) { 
     fileExists = false ; 
    } 

    // Debug info for private use 
    System.out.println("Incoming!!!"); 


    // Send the entity body. 
    if (fileExists) { 
    sendBytes(myFile, os); 
    //fis.close(); 
    } 
    // Close streams and socket. 
    is.close(); 
    os.close(); 
    br.close(); 
    socket.close(); 
    } 

    private static void sendBytes(File myFile, 
    BufferedOutputStream os) throws Exception { 
     // Construct a 1K buffer to hold bytes on their way to the socket. 
     byte[] mybytearray = new byte[(int) myFile.length()]; 


     FileInputStream fis = null; 
     // Copy requested file into the socket's output stream. 
     try { 
      fis = new FileInputStream(myFile); 
     } catch (FileNotFoundException ex) { 
      // Do exception handling 
     } 
     BufferedInputStream bis = new BufferedInputStream(fis); 

     try { 
      bis.read(mybytearray, 0, mybytearray.length); 
      os.write(mybytearray, 0, mybytearray.length); 
      os.flush(); 

      // File sent, exit the main method 
      return; 
     } catch (IOException ex) { 
     // Do exception handling 
      } 
     } 

    } 

时,在客户端程序试图写入通过服务器的错误发生:this.os. writebytes(newFile +/n);

Testfile01.bmp 

    writing 

    saved 

    Testfile02.bmp 

Exception in thread "main" java.net.SocketException: Socket closed 
at java.net.SocketOutputStream.socketWrite(Unknown Source) 
at java.net.SocketOutputStream.write(Unknown Source) 
at java.io.DataOutputStream.writeBytes(Unknown Source) 
at TCPClientPersistentNp.openSocket(TCPClientPersistentNp.java:53) 
at TCPClient.main(TCPClient.java:66) 
+1

你的问题是什么? – 2012-04-15 09:38:42

+0

为什么无法向循环中的第二次运行向服务器发送请求我只获得一个文件。然后最终得到那个错误。 – DesperateLearner 2012-04-15 09:39:50

+0

[Software caused connection abort:socket write error]可能重复(http://stackoverflow.com/questions/10157669/software-caused-connection-abort-socket-write-error) – Mat 2012-04-15 09:40:52

回答

1

我不打算仔细阅读您的代码(请参阅上面的注释...),但显然有一些非常奇怪/错误的地方。简单地说,如果你打算和一个基于HTTP的服务器交谈,你不能只是打开一个套接字并写东西。您的客户端必须创建格式良好的HTTP请求,并处理返回的HTTP响应。

客户端发生异常是因为服务器端...实际上您的代码...已经关闭了另一端的连接。

+0

你好斯蒂芬,我已经格式化了代码。谢谢您的帮助。我看到了错误。我默认关闭套接字而不检查连接类型。真的吗? – DesperateLearner 2012-04-15 10:06:37

+0

问题是您正在关闭连接。而且我也不明白为什么你调用这个HTTP,因为它与真正的HTTP协议没有任何相似之处。 – 2012-04-15 16:49:58

1

这是干什么用的?

is.close(); 
os.close(); 
br.close(); 
socket.close(); 

在处理完每个请求后,您是否明确地关闭了所有内容?这就是你说的一个持续的连接实现吗?

相关问题