2 u014341580 u014341580 于 2015.07.02 09:26 提问

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个回答

woyaochenggong774
woyaochenggong774   2015.07.02 10:07

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

91program
91program   Ds   Rxr 2015.07.02 12:44

Socket 功能在 Service 中实现【这才是实际的使用情况】

你都直接给代码,偶就给一个链接。这是偶前一段时间写的 Socket 通讯源代码,供参考。

Csdn user default icon
上传中...
上传图片
插入图片