Android socket编程

socket分为两种:
UDP和TCP

下面我们先来介绍UDP:

特点:1.数据要进行打包发送。 2.数据的大包大小有限制 3.面向无连接 4.安全性低不可靠 5.速度快
使用场景:QQ群聊

代码如下:发送端

public class SendSocket {
    public static void main(String[] args) {
        try {
            //第一步得到Socket对象:
            DatagramSocket ds = new DatagramSocket();

            BufferedReader bis = new BufferedReader(new InputStreamReader(System.in));
            InetAddress address = InetAddress.getLocalHost();

            int port = 10088;
            String line = null;
            while ((line = bis.readLine()) != null) {
                if ("886".equals(line)) {
                    break;
                }
                byte[] bytes = line.getBytes();
                //第二步将数据大包,数据包里封装了数据,数据长度,ip地址,端口好。
                DatagramPacket p = new DatagramPacket(bytes, bytes.length, address, port);
                //第四步发送数据
                ds.send(p);
            }

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

接收端:

public class ReceiveSocket {
    public static void main(String[] args) {
        try {
            //第一步创建Socket对象
            DatagramSocket ds = new DatagramSocket(10088);
            byte[] bys = new byte[1024];
            //这里因为需要不断接收数据所以 设置为true
            while (true) {
                //第二步建立接收数据的包大小
                DatagramPacket p = new DatagramPacket(bys, bys.length);
                //第三步接收数据;
                ds.receive(p);
                //第四步解析包里面获得的数据。
                String hostAddress = p.getAddress().getHostAddress();
                byte[] data = p.getData();
                String s = new String(data, 0, p.getLength());
                System.out.println("hostAddress = " + hostAddress + " s = " + s);
            }
            //ds.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

写好代码后先运行接收端再运行发送端。如果要实行多人聊天的话可以多创建几个端口号一致的发送端就行啦。

注意发送端和接受端的端口号要保持一致。

TCP:

特点:1.需要建立连接 2.数据无限制 3.面向连接(三次握手)4.安全性高 5.速度慢
为什么安全性高速度慢?因为在每次数据传输过程中都要进行判断时候连接成功,只用在连接成功的前提下程序才会往下走。

下面我们来看代码:
服务端:

public class ServerDemo {
    public static void main(String[] args) throws IOException {

        //第一步创建Socket服务;
        ServerSocket ss = new ServerSocket(10010);
        //第二步监听客户端;
        Socket socket = ss.accept();//阻塞;连接正常后才往下运行;
        //获取输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));

        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            //返回信息:
            PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
            pw.println("数据成功写入");
        }

        socket.close();
    }

}

客户端:

public class ClientDemo {
    public static void main(String[] args) throws IOException {
        //第一步建立客户端Socket对象。
        Socket socket = new Socket("192.168.0.212", 10010);
        //第二步写数据;
        BufferedReader br = new BufferedReader(new InputStreamReader(
                System.in));
        PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
        String line = null;
        while ((line = br.readLine()) != null) {
            if ("over".equals(line)) {
                break;
            }
            pw.println(line);
            //获得返回的输入流
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println(bufferedReader.readLine());
        }

        br.close();
        socket.close();
    }
}

注意我们要先运行服务端再运行客户端。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值