加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Software caused connection abort: socket...错误一解决方法

(2013-01-08 12:21:06)
标签:

software

connection

abort

socket

write

分类: 工具
使用socket方式传输文件的时候,经常会碰到Software caused connection abort: socket write error 的错误,个人测试的时候,发现跟传输的文件大小总和有一定关系,所以就有了下面的一个简单的解决方案,它只是针对多个小文件的总和大于传输限制的情况能够 起一定的作用,如果一个文件的大小就超过了传输的限制,那这个方法,也不起左右。

    避免错误的方法就是,在传送完每个文件后重置一下ObjectOutputStream对象。

    this.outObjectStream.reset();

 

下面是一段程序源码:

package test.socket.sockettransfer;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Iterator;

import test.soa.message.MessagePOJO;

public class SocketClient {

    private Socket socket = null;
    private ObjectOutputStream outObjStream = null;
    private ObjectInputStream inObjStream = null;

   
   

   
    public SocketClient(TransferObject p_obj, String p_strIP, int p_iPort) throws
        Exception {
        this.socket = new Socket(p_strIP, p_iPort);
        this.outObjStream = new ObjectOutputStream(this.socket.getOutputStream());
        this.inObjStream = new ObjectInputStream(this.socket.getInputStream());
        this.transfer(p_obj);
        this.close();
    }

   
    public SocketClient(String p_strIP, int p_iPort) throws Exception {
        this.socket = new Socket(p_strIP, p_iPort);
        this.socket.setPerformancePreferences(1,0,0);
        this.socket.setKeepAlive(true);
        this.socket.setSoTimeout(600 * 10000);
        this.socket.setTcpNoDelay(true);
        this.socket.setOOBInline(true);
        //this.socket.set
        //System.out.print("Socket==" + this.socket.isConnected());
        try {
           if(this.outObjStream == null && !socket.isOutputShutdown()) {
            this.outObjStream = new ObjectOutputStream(this.socket.getOutputStream());
           }
         }catch(Exception e){}

       
       
        //System.out.println("cccccccccccccccccccc");
    }

   
    public String sendPOJO(MessagePOJO messagepojo) throws Exception
    {
     try{
          //System.out.println("Trans Object");
          outObjStream.writeObject(messagepojo);
          //System.out.println("Trans Object End");
          // 如果TransferObject实例存在附件
          TransferObject transferobject=null;
          if(messagepojo!=null && messagepojo.getTransferObjectMap()!=null && messagepojo.getTransferObjectMap().get("transferobject")!=null){
           transferobject = ((TransferObject)messagepojo.getTransferObjectMap().get("transferobject"));
          }else{
           throw new Exception("TransferObject Error!");
          }
          if (transferobject.hasFile())
          {
           Iterator  fileListItem = transferobject.getTransferFileList().values().iterator();
            while(fileListItem!=null && fileListItem.hasNext())
              {
  
                // 传输附件
                  transferFile( (TransferFile)fileListItem.next());

//重置一下对象,清空对象缓存
                  this.outObjStream.reset();
                  //this.socket.
              }
          }
          this.outObjStream.flush();
      }catch(Exception ex){
       throw ex;
     }
       
     String returnType="Error!";
     returnType=this.readSocketType();//取得接收方最后的返回状态值
     if(returnType!=null && !"".equals(returnType)){//连接成功
      return returnType;
     }else{
      return new String("Error!");
     }
    }
   
   
    public void transfer(TransferObject p_obj) throws Exception
    {
        //System.out.println("Trans Object");
        outObjStream.writeObject(p_obj);
        //System.out.println("Trans Object End");
        // 如果TransferObject实例存在附件
        if (p_obj.hasFile())
        {
         Iterator  fileListItem = p_obj.getTransferFileList().values().iterator();
          while(fileListItem!=null && fileListItem.hasNext())
            {

              // 传输附件
                transferFile( (TransferFile)fileListItem.next());
                //this.socket.
            }
        }
//        TransferObject transObj = this.readTransferObject();
//        p_obj.exchangeObject(transObj);
    }

   
    private TransferObject readTransferObject() throws Exception
    {
        Object obj = inObjStream.readObject();
        if (! (obj instanceof TransferObject))
            throw new IOException();
        TransferObject transObj = (TransferObject) obj;
        return transObj;
    }

   
    private void transferFile(TransferFile p_transferFile)
    {
        FileInputStream fileInStream = null;
        int iByteNum = 0;
      
        try {
         //System.out.println("p_transferFile.getFilePath()="+p_transferFile.getFilePath());
         //System.out.println("p_transferFile.getFileName()="+p_transferFile.getFileName());
            // 创建本读文件输入流
            fileInStream = new FileInputStream(p_transferFile.getFilePath() +
                                               p_transferFile.getFileName());
            TransferFileBegin begin = new TransferFileBegin();
            outObjStream.writeObject(begin);
            try{
                outObjStream.flush();
               }catch(Exception e){
                //e.printStackTrace();
               }
            // 从本地读取附件,封装到TransferFileBlock类中。
       
           
            //int inum=0;//XXX
            do
            {
             //inum++;//XXX
             TransferFileBlock transFileBlock = new TransferFileBlock();
                iByteNum = fileInStream.read(transFileBlock.blockByteAr);
                //System.out.println("iByteNum="+iByteNum);
                //System.out.println("发送每次文件量" + inum+" 顺序 :" +iByteNum + "内容 "+transFileBlock.blockByteAr);//XXX
                if ( (transFileBlock.iByteCount = iByteNum) == -1) {
                    break;
                }
                // 写TransferFileBlock实例到服务器端
                //System.out.println("close:" + this.socket.isConnected());
                outObjStream.writeObject(transFileBlock);
                try{
                 outObjStream.flush();
                }catch(Exception e){
                 e.printStackTrace();
                }
               // transFileBlock=null;
            }
            while (iByteNum != -1);
            System.out.println("文件总量 "+p_transferFile.getFileSize());
            TransferFileEnd end = new TransferFileEnd();
            outObjStream.writeObject(end);
            try{
                outObjStream.flush();
             }catch(Exception e){}
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                fileInStream.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

   
   
    private String readSocketType() throws Exception
    {
     Object obj=null;
     
     try{
      if(this.inObjStream == null && !socket.isInputShutdown()) {
       this.inObjStream = new ObjectInputStream(this.socket.getInputStream());
      }
      if(inObjStream!=null){
       obj = inObjStream.readObject();
      }
        if (obj instanceof String){
            return new String((String)obj);
        }else{
         return null;
        }
     }catch(Exception e){
      
     }finally{
      this.close();//关闭连接
     }
        return null;
    }
   
   
    public void close() {
     try{
      if(this.inObjStream!=null){
       this.inObjStream.close();
      }
     }catch(Exception e0){}
     
     try{
      if(this.outObjStream!=null){
       this.outObjStream.close();
      }
     }catch(Exception e1){}
     
        try {
         if(this.socket!=null){
          this.socket.close();
         }
        }
        catch (Exception e) {}
    }
   

}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有