如何从服务器读取字符串数据? (swift客户端 - android服务器)

时间:2016-09-06 15:14:55

标签: android sockets server client

这是快速代码(客户端):

    let text: String = "neslihan"

    var data = NSData(data: text.dataUsingEncoding(NSASCIIStringEncoding)!)

    outputStream.write(UnsafePointer<UInt8>(data.bytes), maxLength: data.length)

这是android代码(服务器端)

  public class SocketServerThread extends Thread {
    DataInputStream dataInputStream = null;
    ServerSocket serverSocket;
    public int socketServerPORT = 3671;
    Socket socket = null;

    @Override
    public void run() {

        try {
            serverSocket = new ServerSocket();
            serverSocket.setReuseAddress(true);
            serverSocket.bind(new InetSocketAddress(socketServerPORT));

            while (true) {

                socket = serverSocket.accept();
                dataInputStream = new DataInputStream(socket.getInputStream());

                Log.d("messageFromClient = " , dataInputStream.readUTF());
           }
      }
 }

有连接,但我看不到日志。我怎样才能读取字符串数据?

2 个答案:

答案 0 :(得分:0)

使用flush提交写入数据

outputStream.flush()

答案 1 :(得分:0)

...
while(true){
   socket = serverSocket.accept();
   BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

   Log.d("messageFromClient = " , br.readLine());
}
...