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

 

 

 

 

Netty 20211116 socket client-server   TCP/UDP_服务器端

 

 Netty 20211116 socket client-server   TCP/UDP_客户端_02

 

 Netty 20211116 socket client-server   TCP/UDP_服务器端_03

 

 Netty 20211116 socket client-server   TCP/UDP_服务端_04

 

 Netty 20211116 socket client-server   TCP/UDP_数据_05

 

 Netty 20211116 socket client-server   TCP/UDP_客户端_06

 

 Netty 20211116 socket client-server   TCP/UDP_服务端_07

 

 Netty 20211116 socket client-server   TCP/UDP_服务器_08

 

 Netty 20211116 socket client-server   TCP/UDP_服务器端_09

 

 

实例

 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..........................................

Netty 20211116 socket client-server   TCP/UDP_数据_10

 

 Netty 20211116 socket client-server   TCP/UDP_客户端_11

 

 Netty 20211116 socket client-server   TCP/UDP_客户端_12

 

 Netty 20211116 socket client-server   TCP/UDP_服务器端_13

 

 Netty 20211116 socket client-server   TCP/UDP_数据_14

 

实例

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

    }

  }