TCP发送接收结合IO流.网络编程单播组播和实时接收发送

TCP发送接收结合IO流

public class test6 {
    private  static Scanner sc=new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        Socket socket=new Socket("127.0.0.1",8888);     //创建流套接字并将其连接到指定IP指定端口号
        System.out.println("输入想要复制的文件目录:");              //提示信息
        String s1 = sc.nextLine();                                  //输入
        FileInputStream s = new FileInputStream(s1);                //新建输入文件流
        File files = new File(s1);                                  //新建文件

        if (files.exists()&&files.getName().contains("jpg")&&files.length()<1024*1024*2){  //文件条件判定
            System.out.println("文件存在小于2M且格式为jpg格式");                         //输出判定

        BufferedInputStream bis=new BufferedInputStream(s);                              //新建文件字节缓冲流读取本地文件

        OutputStream os=socket.getOutputStream();             //写到服务器 --- 网络中的流
        BufferedOutputStream bos=new BufferedOutputStream(os);   //获取输出流,写数据

        int b;
        while ((b=bis.read())!=-1){
            bos.write(b);//通过网络写到服务器中
        }
bos.flush();//刷新
        socket.shutdownOutput();//结束标记

        BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream())); //读取关流
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        bis.close();
        socket.close();
        }else {                //文件有误是判定
            try {
                System.out.println("文件有误");
            }catch (Exception e){
                e.getMessage();
            }

        }
    }

    //接收端
static  class serverdemo{
    public static void main(String[] args) throws IOException {
        ServerSocket ss=new ServerSocket(8888);//创建绑定到指定端口的服务器套接字

        Socket accept=ss.accept();     //侦听要连接到此套接字并接受它

        BufferedInputStream bis=new BufferedInputStream(accept.getInputStream());  //输入字节流

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a\\copy.jpg")); //输出到指定目录

        int b;
        while ((b=bis.read())!=-1){ //读输入的
            bos.write(b);   //写进目录
        }
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));//反馈写入到客户端
        bw.write("上传成功");                                                              //反馈为上传成功
        bw.newLine();                                                                          //转行
        bw.flush();                                                                            //关闭流


        bos.close();   //释放资源
        accept.close(); //释放资源
        ss.close(); //释放资源
    }

}
}

网络编程单播

public class test03 {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds=new DatagramSocket();
        Scanner sc=new Scanner(System.in);
        while (true){
            String s=sc.nextLine();
            if (s.equals("886")){
                break;
            }
            byte[] bytes=s.getBytes();
            DatagramPacket dp=new DatagramPacket(bytes, bytes.length,InetAddress.getByName("127.0.0.1"),10000);
                    ds.send(dp);

        }
        ds.close();
    }
    static class receivedemo{
        public static void main(String[] args) throws IOException {
            DatagramSocket ds=new DatagramSocket(10000);
            while (true){
                byte[] bytes=new byte[1024];
                DatagramPacket dp=new DatagramPacket(bytes, bytes.length);
                ds.receive(dp);
                System.out.println("数据是:"+new String(dp.getData(),0,dp.getLength()));
            }
        }
    }
}

网络编程组播

public class test04 {
  private static Scanner sc=new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        DatagramSocket ds=new DatagramSocket();
        while (true){
            String s=sc.nextLine();
            if (s.equals("886")){
                break;
            }
            byte[] bytes=s.getBytes();
            DatagramPacket dp=new DatagramPacket(bytes, bytes.length,InetAddress.getByName("224.0.1.1"),10001);
            ds.send(dp);
        }
        ds.close();
    }

}

public class test05 {
    /*public static void main(String[] args) throws IOException {
        MulticastSocket ms = new MulticastSocket(10001);
        while (true){
            DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);
            ms.joinGroup(InetAddress.getByName("127.0.0.1"));
            ms.receive(dp);
            ms.close();
            System.out.println(new String(dp.getData(), 0, dp.getLength()));

        }

    }*/
    public static void main(String[] args) throws IOException {
        // 1. 创建接收端Socket对象(MulticastSocket)
        MulticastSocket ms = new MulticastSocket(10001);
        // 3. 把当前计算机绑定一个组播地址,表示添加到这一组中.
        ms.joinGroup(InetAddress.getByName("224.0.1.1"));
        // 2. 创建一个箱子,用于接收数据
        while (true) {
            DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);
            // 4. 将数据接收到箱子中
            ms.receive(dp);
            // 5. 解析数据包,并打印数据
            byte[] data = dp.getData();
            int length = dp.getLength();
            System.out.println(new String(data, 0, length));
            // 6. 释放资源
            //ms.close();
        }
    }

}

网络编程结合线程实时接收发送

public class test8_1 {
    private static  boolean b=false;

   private static Scanner sc=new Scanner(System.in);
    public static void main(String[] args) throws InterruptedException, SocketException {

        Object lock=new Object();

            Thread t1 = new Thread(() -> {
                DatagramSocket ds = null;
                try {
                    ds = new DatagramSocket();
                } catch (SocketException e) {
                    e.printStackTrace();
                }
                while (true) {
                                    synchronized (lock) {
                                        String s = sc.nextLine();
                                        if ("2b".equals(s)) {
                                            break;
                                        }
                                        byte[] bytes = s.getBytes();
                                        DatagramPacket dp = null;
                                        try {
                                            dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.95.42"), 12345);
                                        } catch (UnknownHostException e) {
                                            e.printStackTrace();
                                        }
                                        try {
                                            ds.send(dp);

                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                        b=true;
                                     lock.notify();            //开启另一线程
                                        try {
                                            lock.wait();        //自己等待
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                    }
            });


        DatagramSocket ds1 = new DatagramSocket(12345);
        Thread t2 = new Thread(() -> {

            while (true){
                synchronized (lock) {
                    if (b == true){

                        byte[] bytes=new byte[1024];
                        DatagramPacket dp=new DatagramPacket(bytes, bytes.length);
                        try {
                            ds1.receive(dp);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        System.out.println("数据是:"+new String(dp.getData(),0,dp.getLength()));
                        b=false;
                        lock.notify();    //开启另一线程
                try {
                       lock.wait();        //自己等待
                    }
               catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                        try {
                            lock.wait();           //t2线程是第一次进入的就让他等待
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

            t2.start();
            t1.start();

       // Thread.sleep(200);//为了t1和t2能把数据全部添加完毕

        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值