zoukankan      html  css  js  c++  java
  • {网络编程}和{多线程}应用:基于TCP协议【实现多个客户端发送文件给一个服务器端】--练习

    要求:

    实现多个客户端发送文件给一个服务器端
    
    提示:多个人创建客户端发送文件,服务端循环接收socket,从socket中获取文件
    

    说明:这里我们只要建立一个服务端就可以了,然后让多台电脑使用客户端给这个服务端发送文件。

    特别注意:服务端和客户端端口号的对接,以及正确的IP地址


    代码:

    客户端:

    package Homework4;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class Client {
        public static void main(String[] args) {
            System.out.println("客户端已启动");
            File file=new File("Example");
            file.mkdirs();
            File file2=new File("Example\a.txt");
            Socket socket=null;
            InputStream is=null;
            OutputStream os=null;
            FileInputStream fis=null;
            try {
                socket=new Socket("10.16.152.24",7777);
                is=socket.getInputStream();
                fis=new FileInputStream(file2);
                os=socket.getOutputStream();
    
                byte[] bs=new byte[1024];
                int count=0;
                //发送文件
                while((count=fis.read(bs))!=-1){
                    os.write(bs, 0, count);
                    os.flush();
                }
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                if(is!=null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(os!=null){
                    try {
                        os.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(fis!=null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(socket!=null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
    
        }
    
    }
    

    服务器端:

    这里可以不使用多线程:

    但使用单线程有一个弊端:服务器每次只能在接收完其中一个客户端发过来的文件后,才能接收下一个客户端发送来的文件。即如果服务器在接收文件,那么客户端必须等服务器接收完毕后,才能发送过去。

    因此,这里要使用多线程。

    package com.qf.demo3;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server {
    
        public static void main(String[] args) {
            try {
                ServerSocket serverSocket = new ServerSocket(8888);
                while(true){
                    Socket socket = serverSocket.accept();
                    MyThread thread = new MyThread(socket);
                    thread.start();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    }
    
    class MyThread extends Thread{
    
        Socket socket ;
        public MyThread(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {
            InputStream is =  null;
            FileOutputStream fos = null;
            try {
                 is = socket.getInputStream();
                byte[] bs = new byte[1024];
                int count = 0;
                long l = System.currentTimeMillis();
                fos = new FileOutputStream(new File(l+".txt"));
                while((count = is.read(bs))!=-1){
                    fos.write(bs, 0, count);
                    fos.flush();
                }
                System.out.println("接收文件完毕");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(fos!=null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(is!=null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(socket!=null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
    
        }
    }
    
  • 相关阅读:
    ‘Host’ is not allowed to connect to this mysql server
    centos7安装mysql
    further configuration avilable 不见了
    Dynamic Web Module 3.0 requires Java 1.6 or newer
    hadoop启动 datanode的live node为0
    ssh远程访问失败 Centos7
    Linux 下的各种环境安装
    Centos7 安装 python2.7
    安装scala
    Centos7 安装 jdk 1.8
  • 原文地址:https://www.cnblogs.com/TCB-Java/p/6809616.html
Copyright © 2011-2022 走看看