普通网友 2015-07-02 01:26 采纳率: 19%
浏览 1589

android下socket通信的问题

直接上代码:

 public class SocThread extends Thread{
    private String ip = "211.100.96.**";
    private int port = 8006;
    public Socket client = null;
    PrintWriter out;
    BufferedReader in;
    String tmprecvbuf="";
    String recvbuf="";
    String tmpsendbuf="0<DOC SBH=\"NL130110095C\" BBH=\"1.0.0.0\" CZYID=\"30014\" DOCID=\"201\">"+
            "<XTTB><YHYZ><KH></KH><YHM>46346678</YHM><MM>888888</MM></YHYZ><CXXTGX><CXBBH>96</CXBBH>"+
            "<SJBBH>54</SJBBH><XTBBH>3.0.7</XTBBH><ICMYBBH>3</ICMYBBH></CXXTGX><HD><QHD>0</QHD></HD></XTTB></DOC>\0";

    public void conn() {
        try{
            client = new Socket(ip, port);
            client.setSoTimeout(5000);
            if(client.isConnected()) 
                Log.i("444","socket is connected!");
            else {
                Log.i("444","socket is NOT connected!");
            }
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
        }catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        conn();
        try{
            out.println(tmpsendbuf.length());
            Log.i("555","len of sendbuf:"+tmpsendbuf.length());
            out.println(tmpsendbuf);

            int i=0;
            long time_start=System.currentTimeMillis();
            Log.i("555","System.currentTimeMillis-time_start:"+(System.currentTimeMillis()-time_start));
            while((System.currentTimeMillis()-time_start)<(long)10000){
                Log.i("555",String.valueOf(i++));
                tmprecvbuf = in.readLine();
                if (tmprecvbuf != null) {
                    Log.i("555"," len=" + tmprecvbuf.length());
                    recvbuf+=tmprecvbuf;
                }
            }
            if(recvbuf!=null)
                Log.i("555", recvbuf);
        } catch (UnknownHostException e1){
            e1.printStackTrace();
        } catch (IOException e1) {  
            e1.printStackTrace();  
        }  catch (Exception e) {
            e.printStackTrace();
        }finally {  
            try {  
                client.close();  
                Log.i("555", "client is closed.");
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  

    }
}

问题来啦:
进while循环以后一直是收不到任何东西。。。有一些警告信息帖在下面:
图片说明

调用就两行,调用应该没什么问题:
Thread cThread=new SocThread();
cThread.start();

  • 写回答

2条回答 默认 最新

  • 数字改变世界 2015-07-02 02:07
    关注

    个人理解:首先,tcp通信属于长连接,没必要设置超时,你的logcat打印是socket超时,说明你的客户端在5s内没有收到消息,dalvikvm挂了

    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果