(13)tcp和udp的socket简单实现

1.tcp的实现

socket的服务端

/**
 * @Project: 3.DistributedProject
 * @description:  socket的服务端
 * @author: sunkang  一定在流写的时候flush
 *  这里printWrite 必须要用println ,因为在写入的时候加上了一个换行符号
 *
 * @create: 2018-06-27 23:00
 * @ModificationHistory who      when       What
 **/
public class ServerSocketDemo {

    public static void main(String[] args) {
        ServerSocket server = null;
        try {
            server = new ServerSocket(8083);
            Socket socket = server.accept(); //阻塞过程
            BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter os = new PrintWriter(socket.getOutputStream());
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Client:" + is.readLine()); //拿到客户端的信息
            String line = sin.readLine();
            while (!line.equals("bye")) {
                os.println(line);//输出数据
                os.flush();
                System.out.println("Server:" + line);
                System.out.println("Client:" + is.readLine());
                line = sin.readLine();
            }
            os.close();
            is.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }
}

socket的客户端

/**
 * @Project: 3.DistributedProject
 * @description:  socket的客户端
 * @author: sunkang
 * @create: 2018-06-27 23:00
 * @ModificationHistory who      when       What
 **/
public class SocketDemo {
    public static void main(String[] args) {

        try {
            Socket socket=new Socket("127.0.0.1",8083);
            BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
            PrintWriter os=new PrintWriter(socket.getOutputStream());
            BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line=sin.readLine();
            while(!line.equals("bye")){
                os.println(line);
                os.flush();
                System.out.println("Client:"+line);
                System.out.println("Server:"+is.readLine());
                line=sin.readLine();
            }
            os.close();
            is.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }
}

2.udp的实现

udp 服务端的实现

/**
 * @Project: 3.DistributedProject
 * @description:  udp 服务端的实现
 * @author: sunkang
 * @create: 2018-06-28 19:05
 * @ModificationHistory who      when       What
 **/
public class UdpServerDemo {
    public static void main(String[] args) {
        try {
            byte[] bytes = new byte[1024];
            DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length);
            DatagramSocket datagramSocket = new DatagramSocket(2345);
            datagramSocket.receive(datagramPacket);
            //打印数据包的内容
            System.out.println(new String(bytes,0,datagramPacket.getLength()));
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

udp 客户端的实现


/**
 * @Project: 3.DistributedProject
 * @description:  udp 客户端的实现
 * @author: sunkang
 * @create: 2018-06-28 19:00
 * @ModificationHistory who      when       What
 **/
public class UdpClientDemo {
    public static void main(String[] args) {
        InetAddress  inetAddress= null;
        try {
            inetAddress = InetAddress.getByName("localhost");
            byte[]  data  = "sunkang hello".getBytes();
            //数据包携带了包体的字节数据和ip地址以及端口号
            DatagramPacket datagramPacket = new DatagramPacket(data,data.length,inetAddress,2345);
            DatagramSocket datagramSocket = new DatagramSocket();
            datagramSocket.send(datagramPacket);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:((13)tcp和udp的socket简单实现)