美文网首页
1.2基于tcp/udp的socket运用_BIO

1.2基于tcp/udp的socket运用_BIO

作者: 沉迷学习ing | 来源:发表于2018-08-22 23:27 被阅读0次

基于tcp/ip协议_BIO:

socket

客户端:

public class SocketClient {

    public static void main(String[] args) {
        try {
            Socket socket=new Socket("localhost",8888);

            BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream())); //读取服务端信息
            PrintWriter writer=new PrintWriter(socket.getOutputStream(),true);  //往服务端写数据

            writer.println("Hello word");
            writer.flush();
            while(true){
                String serverData=reader.readLine();

                if(serverData==null){
                    break;
                }
                System.out.println("客户端收到数据:"+serverData);
            }
            writer.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

服务端:

public class SocketServer {


    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=null;

        try{
            //启动服务,端口为8888
            serverSocket=new ServerSocket(8888);  
            while(true){
                Socket socket=serverSocket.accept();  //等待一个接收请求
                new Thread(()->{
                    try {
                        BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        PrintWriter writer =new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));

                        while(true){
                            String clientData=reader.readLine(); //读取客户端发送过来的消息

                            if(clientData==null){
                                break;
                            }

                            System.out.println("服务端接收到的数据:"+clientData);

                            writer.println("Hello 001");
                            writer.flush();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(serverSocket!=null){
                serverSocket.close();
            }
        }
    }
}

基于udp/ip协议_BIO:

DatagramSocket

客户端:

public static void main(String[] args) {
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket();
            
            //发送消息,需要设置对方ip及端口
            String msg = "hello 001";
            InetAddress inet = InetAddress.getByName("localhost");
            DatagramPacket sendpacket = new DatagramPacket(msg.getBytes(), msg.length(),inet,8989);
            socket.send(sendpacket);
            
            byte[] buf = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buf, buf.length, inet, 8989);
            socket.receive(packet);//进入监听,等待接收消息
            System.out.println("接收到消息:"+new String(buf,0,packet.getLength()));
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (socket != null) {
                socket.close();
            }
        }
    }

服务端:

public static void main(String[] args) {
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket(8989);
            byte[] buf = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);//进入监听,等待接收消息
            System.out.println("接收到消息:"+new String(buf,0,packet.getLength()));
            
            //发送消息,需要设置对方ip及端口
            String msg = "hello 002";
            DatagramPacket sendpacket = new DatagramPacket(msg.getBytes(), msg.length(), packet.getAddress(), packet.getPort());
            socket.send(sendpacket);
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if (socket != null) {
                socket.close();
            }
        }
        
    }

组播

客户端:

 InetAddress group=InetAddress.getByName("224.5.6.7");
        MulticastSocket socket = null;
        try {
            socket=new MulticastSocket(8888);

            socket.joinGroup(group);  //加到指定的组里面

            byte[] buf=new byte[1024];
            while(true){
                DatagramPacket packet=new DatagramPacket(buf,buf.length);
                socket.receive(packet);
                System.out.println("接收到的数据:"+new String(packet.getData()));
                System.out.println("接收到的数据:"+new String(buf));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (socket !=null) {
                socket.close();
            }
        }

    }

服务端:

public static void main(String[] args) {
        try {
            //地址段:224.0.0.0 - 239.255.255.255
            InetAddress group=InetAddress.getByName("224.5.6.7");

            MulticastSocket socket=new MulticastSocket();

            for(int i=0;i<10;i++){
                String data="Hello all";
                byte[] bytes=data.getBytes();
                socket.send(new DatagramPacket(bytes,bytes.length,group,8888));
                TimeUnit.SECONDS.sleep(2);
            }

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

相关文章

  • 1.2基于tcp/udp的socket运用_BIO

    基于tcp/ip协议_BIO: 客户端: 服务端: 基于udp/ip协议_BIO: 客户端: 服务端: 组播 客户...

  • 网络编程

    网络 Socket 基于TCP协议的Socket编程 基于UDP协议的Socket编程

  • Java socket

    Java Socket实现基于TCP和UDP多线程通信Java Socket编程

  • SOCKET,TCP/UDP,HTTP,FTP

    转。。。。。。。。 SOCKET,TCP/UDP,HTTP,FTP (一)TCP/UDP,SOCKET,HTTP,...

  • Socket、TCP和UDP

    Socket介绍 socket 套接字, 是基于TCP UDP 协议的 一种通信机制, 它本身就是对TCP ...

  • netty springmvc 总结

    netty 快速开发基于tcp udp等协议开发,就是快速的socket开发,而且是NIO的 补充:TCP,UDP...

  • TCP、UDP协议测试

    基于Nodejs、socket.io,开发了TCP和UDP服务平台,帮助大家做TCP和UDP协议的测试 在做硬件终...

  • iOS中的socket通信

    socket 套接字, 是基于TCP UDP 协议的 一种通信机制, 它本身就是对TCP 和UDp 协议应用的一...

  • socket到底是什么

    socket是对TCP/UDP协议的封装的接口,socket本身并不是协议。Http是基于socket之上的,通过...

  • iOS面试题:socket 连接和 Http 连接的区别

    http 是基于 socket 之上的。socket 是一套完整的 tcp,udp协议的接口。 HTTP协议:简单...

网友评论

      本文标题:1.2基于tcp/udp的socket运用_BIO

      本文链接:https://www.haomeiwen.com/subject/nzjuiftx.html