请问我想实现java tcp socket通信,从客户端A每隔一秒发送一条数据到服务器B。数据:每个一秒钟从数据库中读取一条字符串。
所以呢,这个每个一秒怎么实现呢?
我用Thread.sleep(1000)实现,有时候会报错java.net.SocketException: Software caused connection abort: recv failed,有时候又不报错!
请问有没有更好的办法实现呢?

解决方案 »

  1.   

    “java.net.SocketException: Software caused connection abort: recv failed”
    这个错跟Thread.sleep(1000)没有关系,它是socket连接异常报的错。
    每间隔1秒发一次,用Thread.sleep(1000)就可以了。
      

  2.   


    我在想会不会在Thread.sleep(1000)的一秒内,socket自动断开了,有没有这个可能呢?
      

  3.   

    DBhelper helper = new DBhelper(c);
    SQLiteDatabase db = helper.getWritableDatabase();
    OutputStream outputStream=null;
    ByteArrayInputStream inputStream =null;
    Socket socket=null;
    int itsid;
    try {
    //创建一个Socket对象,指定服务器端的IP地址和端口号
        socket=new Socket("192.168.137.1",9000,InetAddress.getByName("192.168.137.2"),Sender.localport);
        socket.setKeepAlive(true);
        socket.setTcpNoDelay(true);
        Sender.IPpool.add(String.valueOf(socket.getLocalPort()));
        addmap.put(socket.getLocalPort(),add);
        Sender.localport++; outputStream = socket.getOutputStream();
    Cursor c=db.query("CL",null,"ip = '30000'",null,null,null,null);
    itsid=c.getCount(); new ReceiverThread(socket).start();
             
       while(c.moveToNext() && c.getCount()>0)
                {  
    j++;
    itsid = c.getPosition();
    String ip = c.getString(1);
    String content = c.getString(2);
    String contentID = c.getString(3);
    String credibility=c.getString(4);
    String str =content + "**"+credibility;
    Log.i("count","j1:"+j);
         byte data [] = str.getBytes();
         Log.i("data","data大小:"+data.length);
         byte array [] = new byte [1024];
         inputStream = new ByteArrayInputStream(data);
                    int temp=0;
                    byte head=new Integer(data.length).byteValue();
         byte[] titleBytes = new byte[4];
         Log.i("title","titleBytes前:head"+head);
         titleBytes[0]=head;
         //System.arraycopy(head, 0,titleBytes,0,data.length);
         Log.i("title","head大小:"+head);
         Log.i("title","titleBytes大小:"+titleBytes.length);
         Log.i("title","titleBytes内容:"+new String(titleBytes));
         Log.i("title","titleBytes:"+titleBytes);
         outputStream.write(titleBytes);
        
         Log.i("count","j2:"+j);
         while((temp=inputStream.read(array)) != -1){
         Log.i("UnwantedThread","UnwantedThread socket.isOutputShutdown():"+socket.isOutputShutdown());
         outputStream.write(array,0,temp);
         Log.i("title","发送的数据:"+new String(array,0,temp));
         }
         Log.i("count","j3:"+j);
         outputStream.flush();
         Log.i("Speed","端口号:"+socket.getLocalPort()+"的发送的数据速率:"+addmap.get(socket.getLocalPort()));
        
         Thread.sleep(addmap.get(socket.getLocalPort()));
         Log.i("count","j4:"+j);
                }
         
    } catch (Exception e) {
    // TODO Auto-generated catch block
    Log.i("outputStream","IO流出错!!!");
    //Log.i("outputStream",String.valueOf(e.printStackTrace()));
    Log.i("count","j5:"+j);
    e.printStackTrace();
    }

    finally
    {
    Log.i("outputStream","UnwantedThread结束!!!");
    Log.i("UnwantedThread","UnwantedThread结束!!!");
    System.out.println("UnwantedThread结束!!!");
    try {
    Thread.sleep(1000);
        if(socket!=null&&outputStream!=null&&inputStream!=null)
        {
       socket.close();outputStream.close();inputStream.close();
        }
    }catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
      

  4.   

    CS这两边不要都设置等待1秒,其中一方设置就可。
    S向C端发送数据,C端每次接到数据后,等待一秒再发送接收成功ok标示给S端,S端接收到OK立即将数据发送给C端如此循环。
      

  5.   


             //这是结构更加清晰的版本!有不清楚的地方,欢迎交流哈,谢谢
    while(c.moveToNext() && c.getCount()>0)
                {  
    j++;
    itsid = c.getPosition();
    String ip = c.getString(1);
    String content = c.getString(2);
    String contentID = c.getString(3);
    String credibility=c.getString(4);
    String str =content + "**"+credibility;
         byte data [] = str.getBytes();
         byte array [] = new byte [1024];
         inputStream = new ByteArrayInputStream(data);
                            int temp=0;
                            byte head=new Integer(data.length).byteValue();
         byte[] titleBytes = new byte[4];
         titleBytes[0]=head;
         //发送数据    
                 outputStream.write(titleBytes); 
         while((temp=inputStream.read(array)) != -1){
         outputStream.write(array,0,temp);
         }

         outputStream.flush(); 
         Thread.sleep(1000); 
                }
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      

  6.   

    不是这样的,socket不会因为你sleep()了一下就断掉,除非网络异常,或者你的连接本来就失败了......