好贷网好贷款

android socket 即时通信

发布时间:2016-12-4 10:01:37 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"android socket 即时通信",主要涉及到android socket 即时通信方面的内容,对于android socket 即时通信感兴趣的同学可以参考一下。

public class TestClient { static DataInputStream dis =null; static DataOutputStream dos= null; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub TestClient tc = new TestClient(); tc.connectServer(); } public void connectServer(){ try { Socket socket = new Socket("172.20.205.222",8888); InputStream is = socket.getInputStream(); OutputStream ous = socket.getOutputStream(); dis = new DataInputStream(is); dos = new DataOutputStream(ous); writeMethod(); readMethod(); //dos.close(); //dis.close(); //socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void writeMethod(){ new WriteThread().start(); } public void readMethod(){ new ReadThread().start(); } class WriteThread extends Thread{ boolean flag = true; @Override public void run() { // TODO Auto-generated method stub while(flag){ Scanner scanner = new Scanner(System.in); String inputContent = scanner.next(); //阻塞方法 if(inputContent!=null){ try { dos.writeUTF(inputContent); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } class ReadThread extends Thread{ boolean flag = true; @Override public void run() { // TODO Auto-generated method stub while(flag){ String returnContent; try { returnContent = dis.readUTF(); if(returnContent!=null){ System.out.println("服务器端对客户端说:"+returnContent); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

上一篇:thc paper
下一篇:hdu 4288

相关文章

相关评论