首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

经过Wifi实现设备间的通信

2012-07-03 
通过Wifi实现设备间的通信服务器端,建立监听(方法更新,可以同时连接多个Client)客户端,发送消息public cla

通过Wifi实现设备间的通信
服务器端,建立监听(方法更新,可以同时连接多个Client)


客户端,发送消息
public class Client {private Socket socket;private BufferedReader in;private PrintWriter out;public Client(String domain, int port) {connect(domain, port);get(in);}private void connect(String domain, int port) {try {socket = new Socket(domain, port);in = new BufferedReader(new InputStreamReader(socket.getInputStream()));out = new PrintWriter(socket.getOutputStream(), true);} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}private void get(final BufferedReader in) {new Thread(new Runnable() {public void run() {try {String line = in.readLine();while (true) {line = in.readLine();}} catch (IOException e) {e.printStackTrace();}}}).start();}public void send(String msg) {out.println(msg);}public void disconnect() {try {out.close();in.close();socket.close();} catch (IOException e) {e.printStackTrace();}}}

热点排行