最近上课要求做一个用到socket的project,于是就想用安卓试试。现在碰到的问题是,安卓作为客户端可以发数据但是收不到数据。服务器发数据用的是PrintWriter.println(message)以及write(message+"\n"),安卓client接收用的是readLine()。我的代码在纯java,也就是pc自己和自己通信的时候是没有问题的,移到安卓上才出现了这个问题。请大家帮忙!感谢!
client:
private class IncomingReader implements Runnable { @Override public void run() { String message; try { while((message = reader.readLine()) != null){ Log.i(TAG, "read" + message); text.append(message+"\n"); } } catch (Exception e) { e.printStackTrace(); } } }
server:
private void tellEveryone(String message) { Iterator<PrintWriter> it = registeredClients.values().iterator(); while(it.hasNext()){ PrintWriter w = (PrintWriter)it.next(); w.println(message); w.flush(); } }
writer.write("-----Sorry, the name has already been used.-----\n");