-4

请告诉我一个从 android 发送消息协议 tcp 的简单示例。Sand(ipServer,Port)的方法如何实现;

4

2 回答 2

2
String host = ...; // The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address
int port = ...;
Socket socket = new Socket(InetAddress.getByName(host), port);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true); // true for auto flush

现在,您可以通过 writer 发送消息,例如:

writer.println("Hello World");

您可以通过阅读器读取传入的数据,例如:

String incoming = reader.readLine();
于 2012-06-05T13:10:16.827 回答
0

您在网上询问的主题有大量文章。其中之一是:http ://www.helloandroid.com/tutorials/simple-connection-example-part-ii-tcp-communication

亲切的问候,

于 2012-06-05T13:08:52.063 回答