直接上代码:
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();