通過網絡編程、文件IO、多線程,實現客戶端和服務端對話。

思路:1、建立輸入輸出流的兩個線程類
2、客戶端和服務端類繼承輸入流線程類
3、客戶端和服務端類繼承輸出流線程類
(1)、message打印在控制檯
(2)、message打印在文件中(FileOutPutStream)
4、客戶端和服務端的main方法中建立連接。

1、建立輸入輸出流的兩個線程類

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class OutputThread extends Thread{

    private Socket socket = null;
    private String pathName;

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public  String getPathName() {
        return pathName;
    }

    public void setPathName(String pathName) {
        this.pathName = pathName;
    }

    public OutputThread(Socket socket,String pathName) {
        this.socket = socket;
        this.pathName=pathName;
    }

    @Override
    public void run() {
        OutputStream os = null;
        OutputStream os1=null;
        try {
            os1 = new FileOutputStream(pathName,true);
        } catch (FileNotFoundException e1) {

            e1.printStackTrace();
        }
        Scanner sc = null;
        try {
            os = socket.getOutputStream();
            sc = new Scanner(System.in);
            while(true){
                String message = sc.nextLine();
                os.write(message.getBytes());
                os1.write(message.getBytes());
                os1.write("\r\n".getBytes());

                os.flush();
                os1.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                os.close();
                os1.close();
                sc.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }




    }

}
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class InputThread extends Thread{

    private Socket socket;
    private String pathName;

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public String getPathName() {
        return pathName;
    }

    public void setPathName(String pathName) {
        this.pathName = pathName;
    }

    public InputThread(Socket socket) {
        this.socket = socket; 
    }

    @Override
    public void run() {
        InputStream is = null;
        try {

            is =socket.getInputStream();
            byte[] buffer = new byte[1024];
            int length = 0;
            while(-1 != (length = is.read(buffer))){
                String message = new String(buffer,0,length);
                System.out.println("回話:"+message);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }



}

2、客戶端和服務端類繼承輸入流線程類
3、客戶端和服務端類繼承輸出流線程類
代碼如下:

import java.net.Socket;

public class ClientOutputThread extends OutputThread{

    public ClientOutputThread(Socket socket,String pathName) {
        //super(socket, getPathName());
        super(socket, pathName);
    }




}
import java.net.Socket;

public class ClientInputThread extends InputThread{

    public ClientInputThread(Socket socket) {
        super(socket);

    }




}
import java.net.Socket;

public class ServerOuputThread extends OutputThread{

    public ServerOuputThread(Socket socket ,String pathName) {
        super(socket, pathName);

    }


}
import java.net.Socket;

public class ServerInputThread extends InputThread{

    public ServerInputThread(Socket socket) {
        super(socket);
        // TODO Auto-generated constructor stub
    }



}

4、客戶端和服務端的main方法中建立連接。

import java.net.Socket;

public class ClientMain {

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

        Socket socket = new Socket("127.0.0.1",2000);

        ClientInputThread inputThread = new ClientInputThread(socket);

        ClientOutputThread outputThread = new ClientOutputThread(socket,"d:/shareming/2.txt");

        inputThread.start();
        outputThread.start();

    }


}
import java.net.ServerSocket;
import java.net.Socket;

public class ServerMain {
    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(2000);
        Socket socket = ss.accept();
        ServerInputThread inputThread = new ServerInputThread(socket);
        ServerOuputThread outputThread = new ServerOuputThread(socket,"d:/shareming/2.txt");        
        inputThread.start();
        outputThread.start();

        ss.close();
    }
}

在控制檯顯示的結果爲:
服務端:

這裏寫圖片描述

客戶端:

這裏寫圖片描述

打印在文件中:

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章