Android学习笔记之TCP通信,客户端和服务端互相通信

主要实现客户端与服务端互相通信

客户端

public class TCPServer implements Runnable {
    private ServerSocket serverSocket;
    private boolean isRunning;

    public TCPServer(int port) throws IOException {
        serverSocket = new ServerSocket(port);
    }

    public void start() {
        isRunning = true;
        new Thread(this).start();
    }

    public void stop() {
        isRunning = false;
        try {
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (isRunning) {
            try {
                Socket clientSocket = serverSocket.accept();

                new ClientHandler(clientSocket).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    private static class ClientHandler extends Thread {
        private Socket clientSocket;

        public ClientHandler(Socket socket) {
            this.clientSocket = socket;
        }

        @Override
        public void run() {

            OutputStream os= null;
            try {
                os = clientSocket.getOutputStream();
                os.write(("数据").getBytes());
                Thread.sleep(5000);
                os.close();
                clientSocket.close();
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

客户端

public class TCPClient {
    private Socket socket;
    private InputStream input;
    private OutputStream output;
    int port=8088;
    public TCPClient() throws IOException {
        socket = new Socket(InetAddress.getLocalHost(),port);//InetAddress.getLocalHost()返回本地机的IP地址
        input = socket.getInputStream();
        output = socket.getOutputStream();
    }

    public void sendData(String data) throws IOException {
        output.write(data.getBytes());
        output.flush();
    }

    public String receiveData() throws IOException {
        byte[] buffer = new byte[1024];
        int bytesRead = input.read(buffer);
        return new String(buffer, 0, bytesRead);
    }

    public void close() throws IOException {
        socket.close();
    }
}

主活动

public class TCPActivity extends AppCompatActivity {
    private TCPServer tcpServer;
    private TCPClient tcpClient;
    private String re;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tcpactivity);

        new Thread(() -> {
            try {
                tcpServer = new TCPServer(8088);
                tcpServer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();


        new Thread(() -> {
            try {
                tcpClient = new TCPClient();
                re=tcpClient.receiveData();
                System.out.println(re+"活动");

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

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (tcpServer != null) {
            tcpServer.stop();
        }
        if (tcpClient != null) {
            try {
                tcpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值