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();
        }

      }

  • 相关阅读:
    mysql/oracle 小技巧自动插入当前时间
    Java StringUtil 用法示例
    timestamp与String的相互转换
    gzip/gunzip用法
    maven常用指令
    微基站、宏基站区别
    CRAN方案
    让gvim中支持utf8编辑
    java正则表达式的几个小例子
    Sql Server数据库汉字按字母、笔划、拼音首字母、排序
  • 原文地址:https://www.cnblogs.com/smallfa/p/15565695.html
Copyright © 2011-2022 走看看