普通网友 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挂了

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能