假设Tom和Jerry利用Java UDP进行聊天

假设Tom和Jerry利用Java UDP进行聊天1-1假设Tom和Jerry利用JavaUDP进行聊天,请为他们编写程序。具体如下:(1)、Tom和Jerry聊天的双方都应该具有发送端和接收端;(2)、利用DatagramSocket与DatagramPacket;(3)、实现java.lang.Runnable类,重写run()方法。Jerry代码:publicclassUDPSocket_Jerry{publicclassUDPSocket_Jerry{publicstaticvoidmain(S

1-1 假设Tom和Jerry利用Java UDP进行聊天,请为他们编写程序。具体如下:
(1)、Tom和Jerry聊天的双方都应该具有发送端和接收端;
(2)、利用DatagramSocket与DatagramPacket;
(3)、实现 java.lang.Runnable类,重写 run()方法。

Jerry代码:

public class UDPSocket_Jerry { 
   
   public class UDPSocket_Jerry { 
   
    public static void main(String[] args) { 
   
        //分别启动发信息线程和收消息线程
        Thread thread1 = new Thread(new UDPSocketClientthread());
        Thread thread2 = new Thread(new UDPSocketServerthread());
        thread1.start();
        thread2.start();
    }
}
public class UDPSocketClientthread implements Runnable { 
   
    DatagramSocket datagramSocket;

    { 
   
        try { 
   
            datagramSocket = new DatagramSocket(8081);
        } catch (SocketException e) { 
   
            e.printStackTrace();
        }
    }

    byte[] ibuf = new byte[1024];
    DatagramPacket datagramPacket = new DatagramPacket(ibuf, ibuf.length);


    @Override
    public void run() { 
   
        while (true) { 
   
            try { 
   
                datagramSocket.receive(datagramPacket);
            } catch (IOException e) { 
   
                e.printStackTrace();
            }
            System.out.println("Tom:" + new String(datagramPacket.getData(), 0, datagramPacket.getLength()));
        }
    }
}
import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UDPSocketServerthread implements Runnable { 
   
    byte[] ibuf = new byte[1024];
    String message;
    Scanner scanner = new Scanner(System.in);
    DatagramPacket datagramPacket;

    { 
   
        try { 
   
            datagramPacket = new DatagramPacket(ibuf, ibuf.length, InetAddress.getByName("127.0.0.1"), 2020);
        } catch (UnknownHostException e) { 
   
            e.printStackTrace();
        }
    }

    DatagramSocket ds;

    { 
   
        try { 
   
            ds = new DatagramSocket(8080, InetAddress.getByName("127.0.0.1"));
        } catch (SocketException | UnknownHostException e) { 
   
            e.printStackTrace();
        }
    }

    @Override
    public void run() { 
   
        while (true) { 
   
            message = scanner.nextLine();
            datagramPacket.setData(message.getBytes());
            try { 
   
                assert ds != null;
                ds.send(datagramPacket);
            } catch (IOException e) { 
   
                e.printStackTrace();
            }
        }
    }
}

Tom代码:

public class UDPSocket_Tom { 
   
    public static void main(String[] args) { 
   
        Thread thread1 = new Thread(new UDPSocketClientthread());
        Thread thread2 = new Thread(new UDPSocketServerthread());
        thread1.start();
        thread2.start();
    }
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPSocketClientthread implements Runnable{ 
   
        DatagramSocket datagramSocket;
    { 
   
        try { 
   
            datagramSocket = new DatagramSocket(2020);
        } catch (SocketException e) { 
   
            e.printStackTrace();
        }
    }
        byte[] ibuf = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(ibuf, ibuf.length);


    @Override
    public void run() { 
   
        while(true){ 
   
            try { 
   
                datagramSocket.receive(datagramPacket);
            } catch (IOException e) { 
   
                e.printStackTrace();
            }
            System.out.println("Jerry:"+new String(datagramPacket.getData(),0,datagramPacket.getLength()));
        }
    }
}
import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UDPSocketServerthread implements Runnable{ 
   
    byte[] ibuf = new byte[1024];
    String message;
    Scanner scanner = new Scanner(System.in);
    DatagramPacket datagramPacket;

    { 
   
        try { 
   
            datagramPacket = new DatagramPacket(ibuf, ibuf.length, InetAddress.getByName("127.0.0.1"), 8081);
        } catch (UnknownHostException e) { 
   
            e.printStackTrace();
        }
    }
    DatagramSocket ds;
    { 
   
        try { 
   
            ds = new DatagramSocket(8083, InetAddress.getByName("127.0.0.1"));
        } catch (SocketException | UnknownHostException e) { 
   
            e.printStackTrace();
        }
    }

    @Override
    public void run() { 
   
        while (true) { 
   
            message = scanner.nextLine();
            datagramPacket.setData(message.getBytes());
            try { 
   
                assert ds != null;
                ds.send(datagramPacket);
            } catch (IOException e) { 
   
                e.printStackTrace();
            }
        }
    }
}

代码运行截图(用两个终端开启)

在这里插入图片描述

今天的文章假设Tom和Jerry利用Java UDP进行聊天分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/26008.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注