Android Socket 服务器ServerSocket发数据到客户端

使用Socket和ServerSocket在局网使用二台电脑    创建服务器和客户端连接,发送数据

第一步创 建服务器

在台试电脑 AbdroidStudro,MainActivity类创建 ServerSocket服务器,发送数据给笔记本电脑客户端

在电脑打开Telnet服务器设置

    //telnet localhost  建立聊天服务器
    public static void main(String[] arges)  {

        ServerSocket socket = null;
        try {
            //创建服务端
            socket = new ServerSocket(1234);//1234端口自己自定义
            Socket s=socket.accept();
            //向客户端发送信息
            OutputStream o=s.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(o));
            bw.write("你好,我是服务器,我叫小能");
            bw.flush();//刷新
            bw.close();//关闭流

        } catch (IOException e) {
            e.printStackTrace();
        }




    }

第二步客户端接受服务器发过来的数据ping拼接IP

private EditText sockte_et;
    private TextView obtain_tv, connect_tv;
    private Button obtain_bu, connect_bu, send_bu;
    Socket socket;
    Handler handler = null;
    InputStream is;
    InputStreamReader isr;
    BufferedReader br;
    String response;
    private boolean bool;



handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 0:
                        obtain_tv.setText(response);//显示服务器发过来的数据
                        break;
                   
                }
            }
        };



//判断是否和服务器连接成功
        connect_bu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        try {
                            socket = new Socket("192.168.5.54 ", 1234);
                            bool = socket.isConnected();
                            Message message = new Message();
                            message.what = 1;
                            message.obj = bool;
                            handler.sendMessage(message);

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });

 //接受服务器的数据
        obtain_bu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        try {
                            is = socket.getInputStream();
                            isr = new InputStreamReader(is);
                            br = new BufferedReader(isr);
                            response = br.readLine();
                            Message message = new Message();
                            message.what = 0;
                            handler.sendMessage(message);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });


   

第三步模拟器显示出来,有什么疑问QQ联系我2328364705

Android Socket 服务器ServerSocket发数据到客户端_第1张图片

你可能感兴趣的:(Socket,服务器,android,网络)