【发布时间】:2015-02-17 08:54:17
【问题描述】:
我想用 android 向网络发送消息。 如何使用 android 中的套接字编程向网络上的一个客户端发送消息?
我搜索了一下socket编程,找到了:
ServerSocket SVsocket=null;
try {
SVsocket=new ServerSocket(8080);
Socket s=SVsocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String incomingMsg = in.readLine() + System.getProperty("line.separator");
String outgoingMsg = "goodbye from port " + "8080" + System.getProperty("line.separator");
out.write(outgoingMsg);
out.flush();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我认为它发送给所有客户端(监听 8080 端口),但我只想发送一个带有 IP 地址的客户端。
【问题讨论】: