学习网络编程

tcp

发送端

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

        Socket socket = new Socket("127.0.0.1", 10000);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("龙王再次".getBytes());

        outputStream.close();
        socket.close();
    }
}

接收端


public class server {
    public static void main(String[] args) throws IOException {
        ServerSocket socket = new ServerSocket(10000);
        Socket accept = socket.accept();
        InputStream inputStream = accept.getInputStream();
        InputStreamReader isr = new InputStreamReader(inputStream);
        BufferedReader bis = new BufferedReader(isr);
        int b ;
        while ( (b=bis.read()) !=-1){
            System.out.println((char)b);
        }
        bis.close();
        socket.close();
    }
}

UDP

单播

发送端

public class demo1 {
    public static void main(String[] args) throws IOException {
        //发送数据
        //创建对象


        DatagramSocket ds = new DatagramSocket();

        String str = "龙王在此";
        byte[] bytes = str.getBytes();
//        System.out.println(bytes);
        InetAddress address = InetAddress.getByName("127.0.0.1");
        int port = 10086;
        //打包数据
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length,address,port);

        //发送
        ds.send(dp);
        //释放资源
        ds.close();

    }
}

接收

public class jieshoudemo {
    public static void main(String[] args) throws IOException {
        //接收数据

        //创建对象--注意,一定要设置端口,和发送时填写的一样
        DatagramSocket ds = new DatagramSocket(10086);
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
        //jieshou--阻塞的,直到接收到
        ds.receive(dp);

        //解析

        byte[] data = dp.getData();
        int length = dp.getLength();
        InetAddress address = dp.getAddress();
        int port = dp.getPort();
        SocketAddress socketAddress = dp.getSocketAddress();
        System.out.println("接收到数据"+new String(data,0,length));
        System.out.println("该数据是从"+address+"发送的,"+"端口号为"+port);
        System.out.println(socketAddress);

        //释放资源
        ds.close();
    }
}

组播

服务端


public class rece {
    public static void main(String[] args) throws IOException {
        InetAddress address = InetAddress.getByName("255.255.255.255");
        MulticastSocket ms = new MulticastSocket(10000);
        ms.joinGroup(address);


//        DatagramSocket ds = new DatagramSocket(10086);
        byte [] bytes=new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
        while (true) {
            ms.receive(dp);
            byte[] data = dp.getData();
            int length = dp.getLength();
            String hostName = dp.getAddress().getHostName();
            String hostAddress = dp.getAddress().getHostAddress();
            System.out.println("地址为:"+hostAddress+
                    ",主机名为:"+hostName+"的用户发送了消息:"
                    +new String(data,0, length));
        }
    }
}

客户端

public class send {
    public static void main(String[] args) throws IOException {
//        DatagramSocket ds = new DatagramSocket();
        MulticastSocket ms = new MulticastSocket(10000);
        Scanner sc = new Scanner(System.in);
        while (true) {
            String s = sc.nextLine();
            if (s.equals("886")){
                return;
            }
            byte[] bytes = s.getBytes();
            InetAddress address = InetAddress.getByName("255.255.255.255");
            DatagramPacket dp = new DatagramPacket(bytes, 0,bytes.length,address,10000);

            ms.send(dp);
            ms.close();
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值