Android上实现TCP&UDP的客户端和服务端

activity_main.xml:




    

MainActivity.java:

package com.miao.tcpandudp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

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

    public void startTcpServer(View view) {
        new Thread(){
            @Override
            public void run() {
                try{
                    Log.i("miao","#################################################"+"prepared");
                    ServerSocket server = new ServerSocket(6666);
                    Socket socket = server.accept();
                    Log.i("miao", "#################################################" + "Connected");
                    OutputStream os = socket.getOutputStream();
                    InputStream is = socket.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    String data = reader.readLine();
                    Log.i("miao","#################################################"+data);

                    os.write("From Server:hi,I am server .\n".getBytes());
                    reader.close();
                    is.close();
                    os.close();
                } catch (Exception e){Log.i("miao","#################################################"+"Exception");}
            }
        }.start();
    }

    public void tcpClient(View view) {
        new Thread(){
            @Override
            public void run() {
                try{
                    Log.i("miao","#########################################"+"clicked");
                    Socket socket = new Socket("192.168.104.1",6660);
                    Log.i("miao","#########################################"+"Connected");
                    OutputStream os = socket.getOutputStream();
                    os.write("From Client:Hi,I am Client .\n".getBytes());
                    InputStream is = socket.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String data = br.readLine();
                    Log.i("miao","#########################################"+data);

                    br.close();
                    is.close();
                    os.close();
                } catch (Exception e){
                    Log.i("miao","#########################################"+"Exception");
                }
            }
        }.start();
    }

    public void startUdpServer(View view) {
        new Thread(){
            @Override
            public void run() {
                try{
                    Log.i("miao","###############################################"+"prepared");
                    DatagramSocket ds = new DatagramSocket(5555);
                    byte[] buf = new byte[1024];
                    DatagramPacket dp = new DatagramPacket(buf,1024);
                    ds.receive(dp);
                    String data = new String(dp.getData(),0,dp.getLength());
                    Log.i("miao","###############################################"+data);

                    InetAddress addr = dp.getAddress();
                    int port = dp.getPort();
                    byte[] echo = "From Server:echo..........".getBytes();
                    DatagramPacket dp2 = new DatagramPacket(echo,echo.length,addr,port);
                    ds.send(dp2);

                    ds.close();
                }catch (Exception e){
                    Log.i("miao","###############################################"+"Exception");
                }
            }
        }.start();
    }

    public void udpClient(View view) {
        new Thread(){
            @Override
            public void run() {
                try{
                    Log.i("miao","###############################################"+"prepared");
                    DatagramSocket ds = new DatagramSocket();
                    InetAddress addr = InetAddress.getByName("192.168.104.1");
                    String data = "From Client:Hi,I am Client .";
                    DatagramPacket dp = new DatagramPacket(data.getBytes(),data.length(), addr,5555);
                    ds.send(dp);

                    byte[] buf = new byte[1024];
                    DatagramPacket dp2 = new DatagramPacket(buf,1024);
                    ds.receive(dp2);
                    String echo = new String(dp2.getData(),0,dp2.getLength());
                    Log.i("miao","##########################################"+echo);

                    ds.close();
                }catch (Exception e){
                    Log.i("miao","###############################################"+"Exception");
                }
            }
        }.start();
    }
}

注意:在测试的时候,可以用电脑和手机之间通讯,也就是一方作为服务端,另一方作为客户端。

你可能感兴趣的:(Android)