zoukankan      html  css  js  c++  java
  • Netty 20211116 socket clientserver TCP/UDP

    1、TCP..........................................

     

     

     

     

     

     

     

     

    实例

     SocketTcpClient/SocketTcpSever

    public class SocketTcpClient {

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

        final Socket socket=new Socket();
        SocketAddress address=new InetSocketAddress(InetAddress.getLocalHost(),8080);
        socket.connect(address);
        PrintWriter socketOut=new PrintWriter(socket.getOutputStream());
        BufferedReader sockerIn=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String sendStr="客户端向服务器端:你们第六期就业薪资突破多少";
        socketOut.write(sendStr);
        socketOut.flush();
        String receiveStr=sockerIn.readLine();
        System.out.println("服务器端问题:"+receiveStr);
        socketOut.close();
        sockerIn.close();
        socket.close();
      }
    }


    public class SocketTcpServer {


      public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=new ServerSocket();
        SocketAddress address=new InetSocketAddress(InetAddress.getLocalHost(),8080);
        serverSocket.bind(address);
        System.out.println("等客户端发送信息......");
        Socket accept=serverSocket.accept();
        PrintWriter socketOut=new PrintWriter(accept.getOutputStream());
        byte buf[]=new byte[1024];
        if(accept.getInputStream().read(buf)>0){
          System.out.println("服务端接受客户端消息:"+new String(buf));
        }

        String sendStr="服务器回复 客户端 突破百W薪资";
        socketOut.write(sendStr);
        socketOut.flush();
        socketOut.close();
        accept.close();
        serverSocket.close();
      }
    }

    2、UDP..........................................

     

     

     

     

    实例

     SocketUdpClient/SocketUdpSever

      public class SocketUdpClient {

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

        /*
        DatagramSocket socket = null;
        try {

          socket = new DatagramSocket();
          byte[] buf = "hello,UDP".getBytes();
          InetAddress address;
          address = InetAddress.getByName("localhost");

          DatagramPacket packet = new DatagramPacket(buf, buf.length, address,8080);

          socket.send(packet);
        } catch (Exception e) {
          e.printStackTrace();
        }finally{

        socket.close();
      }*/

        InetAddress address=InetAddress.getByName("127.0.0.1");
        int port=8080;
        byte data[]="向服务端发送数【我来自客户端,我来自客户端】?".getBytes();
        DatagramPacket packet=new DatagramPacket(data,data.length,address,port);
        DatagramSocket socket=new DatagramSocket();
        socket.send(packet);

        byte data2[]=new byte[1024];
        DatagramPacket packet2=new DatagramPacket(data2,data2.length);
        socket.receive(packet2);
        String reply=new String(data2,0,packet2.getLength());
        System.out.println("我是客户端,服务端说:"+reply);
        socket.close();
      }

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

          DatagramSocket socket=new DatagramSocket(8080);
          byte data[]=new byte[1024];
          DatagramPacket packet=new DatagramPacket(data,data.length);
          System.out.println("******服务端已启动,等客户端发数据");
          socket.receive(packet);
          String info=new String(data,0,packet.getLength());
          System.out.println("我是服务器,客户端说:"+info);
          socket.close();
        }

      }

  • 相关阅读:
    CSS预编译:less入门
    JavaScript学习(五):函数表达式
    关于JavaScript new 的一些疑问
    JavaScript学习(四):面对对象的程序设计
    JavaScript学习(三):引用类型
    JavaScript学习(二):变量、作用域和内存问题
    JavaScript学习(一):基本概念
    匿名函数的this指向为什么是window?
    阿里云ECS在CentOS 6.8中使用Nginx提示:nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)的解决方法
    Centos释放缓存
  • 原文地址:https://www.cnblogs.com/smallfa/p/15565695.html
Copyright © 2011-2022 走看看