zoukankan      html  css  js  c++  java
  • UDP 与 TCP简单入门理解示例

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    

    //服务器端的代码 写了一个简单的匿名多线程 无锁机制
    public class TCP_ServerDemo { public void testTCP_Thread(){ new Thread() { public void run() { ServerSocket ss = null; Socket s =null;
                    byte[] b = new byte[1024];
              try {
                ss = new ServerSocket(8888);

               } catch (IOException e)
              {
                e.printStackTrace();
              }
    while(true) { try { s = ss.accept(); System.out.println("连接到一个客户端"); InputStream inputStream = s.getInputStream();int len =0; while ( (len = inputStream.read(b)) != -1){ String str = new String(b,0,len); System.out.println(str); } OutputStream os = s.getOutputStream(); os.write("客户端你好信息已经收到".getBytes()); s.shutdownOutput(); os.close(); inputStream.close(); s.close(); ss.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); } public static void main(String[] args) { new TCP_ServerDemo().testTCP_Thread(); } }




    //TCP客服端
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;

    public class TCP_Demo {

    public static void main(String[] args) {

    try {
    //1Socket 建立需要连接到的主机地址和端口
    Socket s = new Socket("127.0.0.1",8888);

    OutputStream os = s.getOutputStream();
    InputStream in = s.getInputStream();

    os.write("hello".getBytes());
    s.shutdownOutput(); //告诉服务器发送完成


    byte[] b = new byte[1024];
    int len =0;
    while ( (len = in.read(b)) != -1){
    String str = new String(b,0,len);
    System.out.println(str);
    }

    os.close();
    s.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    }









    //简单的udp客服端
    public class UDP_Demo {

    public static void main(String[] args) {

    try {
    //建立数据接口端口
    DatagramSocket ds = new DatagramSocket();
    byte[] data = "udp come in".getBytes();

    //数据打包
    DatagramPacket dp = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 9999);

    //发送数据
    ds.send(dp);
    ds.close();

    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    //udp服务器端
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.SocketException;

    public class UDP_ServerDemo {
    public static void main(String[] args) {

    try {
    //建立数据接口端口
    DatagramSocket ds = new DatagramSocket(9999);

    byte[] bytes = new byte[1024];
    DatagramPacket dp = new DatagramPacket(bytes, bytes.length);

    ds.receive(dp);
    String str = new String(dp.getData(),0,dp.getLength());

    System.out.println(dp.getAddress().getHostAddress());
    System.out.println(str);

    ds.close();
    } catch (Exception e) {
    e.printStackTrace();
    }

    }
    }


    坚持
  • 相关阅读:
    C++模板进阶指南:SFINAE
    SFINAE and enable_if
    Effective Modern C++:05右值引用、移动语义和完美转发
    C++左值和右值
    Effective Modern C++:04智能指针
    Effective Modern C++:03转向现代C++
    Effective Modern C++:02auto
    Effective Modern C++:01类型推导
    c++ vitual继承
    c++正确处理 is-a has-a关系
  • 原文地址:https://www.cnblogs.com/gaoSJ/p/12831059.html
Copyright © 2011-2022 走看看