java TCP入门编程(文件传送、服务端接收多个客户端请求)

文件传送

服务端 FileServer

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

public class FileServer {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket=new ServerSocket(10086);

        Socket accept = serverSocket.accept();

        System.out.println("建立连接");

        InputStream inputStream = accept.getInputStream();

        FileOutputStream fileOutputStream=new FileOutputStream("D:\Test\hello.txt");

        byte[] bytes=new byte[1024*4];

        int count=0;

        while ((count=inputStream.read(bytes))!=-1){
            fileOutputStream.write(bytes,0,count);
        }
        fileOutputStream.close();

        inputStream.close();

        accept.close();

        serverSocket.close();

        System.out.println("接收完成");

    }
}

客户端 FileClient

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class FileClient {
    public static void main(String[] args) throws IOException {

    Socket socket=new Socket("192.168.182.1",10086);

        OutputStream outputStream = socket.getOutputStream();

        FileInputStream fileInputStream=new FileInputStream("D:\Test\init.txt");

        byte[] bytes=new byte[1024*4];
        int count=0;
        while ((count=fileInputStream.read(bytes))!=-1){
            outputStream.write(bytes,0,count);
        }
        fileInputStream.close();

        outputStream.close();

        socket.close();

        System.out.println("发送完毕");
    }
}

测试:通过测试可以实现文件的传送。

服务端接收多个客户端请求

服务端 ChatServerSocket
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServerSocket {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=new ServerSocket(10086);
        while (true){
            //如果没有客户请求,则阻塞
            Socket accept = serverSocket.accept();
            System.out.println("客户:"+accept.getInetAddress()+" 进来了");
            //当获取到客户端的连接时就会往下走,开启一个线程用来接收数据
            new Thread(new Runnable() {
                @Override
                public void run() {
                    InputStream inputStream = null;
                    try {
                        inputStream = accept.getInputStream();
                        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
                        while(true){//一直接收客户端数据
                            String readLine = bufferedReader.readLine();
                            if(readLine==null||readLine.equals("0")){  //退出循环
                                break;
                            }
                            System.out.println("客户:"+accept.getInetAddress()+"说:"+readLine);

                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            inputStream.close();
                            accept.close();
                            System.out.println("客户"+accept.getInetAddress()+":退出了");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }

    }
}

客户端 ChatClientSocket

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class ChatClientSocket {

    public static void main(String[] args) throws IOException {

         Socket socket=new Socket("192.168.182.1",10086);

        OutputStream outputStream = socket.getOutputStream();

        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"utf-8"));

        Scanner scanner=new Scanner(System.in);

        while (true){

            String next = scanner.next();
            bufferedWriter.write(next);
            bufferedWriter.newLine();
            bufferedWriter.flush();
            if(next.equals("0")){
                break;
            }
        }
        bufferedWriter.close();
        socket.close();
    }
}

测试,我们将客户端分别在本机与虚拟机上打开进行测试:

java TCP入门编程(文件传送、服务端接收多个客户端请求)

java TCP入门编程(文件传送、服务端接收多个客户端请求)
java TCP入门编程(文件传送、服务端接收多个客户端请求)

帮助到您请点赞关注收藏谢谢!!!

匿名

发表评论

匿名网友