java TCP入門程式設計(檔案傳送、服務端接收多個客戶端請求)

一起碼程式碼發表於2020-11-02

檔案傳送

服務端 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();
    }
}

測試,我們將客戶端分別在本機與虛擬機器上開啟進行測試:

在這裡插入圖片描述

在這裡插入圖片描述
在這裡插入圖片描述

幫助到您請點贊關注收藏謝謝!!!

相關文章