Android 使用socket 实现简单的客户端和服务器端互相通信

1、服务端解析:
1)使用BufferedReader 包装获得的数据,为了使连接长时间不断,必须使用while(true)
2)另外开启一个新的线程用户回复客户端,这里使用了PrintWriter 包装数据,从键盘输入数据,结束以”over”

public class SocketServer {  

    public static void main(String[] args) throws IOException {  
        ServerSocket server = new ServerSocket(10000);  
        final Socket socket = server.accept();  

        new Thread(){  
            public void run() {  
                //获得客户端发来的数据  
                try {  
                    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
                    while(true){  
                        System.out.println("Receive from client : " + reader.readLine());  
                    }  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            };  
        }.start();  

        new Thread(){  
            public void run() {  
                try {  
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);  
                    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  
                    String readline = in.readLine();  
                    System.out.println(readline);  
                    while(true){  
                        out.println(readline);  
                        System.out.println(" server send: " + readline);  
                        readline = in.readLine();  
                        if("over".equals(readline))  
                            break;  
                    }  
                } catch (IOException e) {                     
                    e.printStackTrace();  
                }  
            };  
        }.start();  
    }  

}  

2、客户端:
原里与服务器端相似,只是先输入数据,然后才得到服务器反馈的信息

public class SocketClient {  

    public static void main(String[] args) throws IOException {         
        final Socket socket = new Socket("192.168.31.239", 10000);                
        //回复服务端  
        new Thread(){  
            public void run() {  
                try {  
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);  
                    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  
                    String readline = in.readLine();  
                    System.out.println(readline);  
                    while(true){  
                        out.println(readline);  
                        System.out.println(" client send: " + readline);  
                        readline = in.readLine();  
                        if(readline.equals("over"))  
                            break;  
                    }  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            };  
        }.start();  


        new Thread(){  
            public void run() {  
                //获得客户端发来的数据  
                try {  
                    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
                    while(true){  
                        System.out.println("Receive from server : " + reader.readLine());  
                    }  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            };  
        }.start();  


    }  

}  

———————————————————————
(java 架构师全套教程,共760G, 让你从零到架构师,每月轻松拿3万)
有需求者请进站查看,非诚勿扰

https://item.taobao.com/item.htm?spm=686.1000925.0.0.4a155084hc8wek&id=555888526201

01.高级架构师四十二个阶段高
02.Java高级系统培训架构课程148课时
03.Java高级互联网架构师课程
04.Java互联网架构Netty、Nio、Mina等-视频教程
05.Java高级架构设计2016整理-视频教程
06.架构师基础、高级片
07.Java架构师必修linux运维系列课程
08.Java高级系统培训架构课程116课时
(送:hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门)
——————————————————————–

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lovoo/article/details/51764824
想对作者说点什么? 我来说一句

android socket通讯例子

2011年07月07日 44KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭