1. 程式人生 > 其它 >java學習之socket程式設計

java學習之socket程式設計

0x00前言和思維導圖

Socks實際上是什麼:實際上是提供了精彩通訊的埠,在通訊之前雙方都必須要創造一個端點才能通訊,其實感覺socket跟計算機的三次握手有些相似,分為三個步驟:
(1)伺服器監聽:伺服器不知道下一個客戶端的具體套接字,而是處於等待連線的狀態
(2)客戶端請求:由客戶端的套接字提出請求,連線目標是伺服器的套接字。
(3)連線確認:伺服器接收到客戶端的套接字請求,它就響應客戶端的請求,建立一個新的執行緒把伺服器套接字的具體描述發個客戶端,客戶端確認以後,這個連線就建立好了,服務端繼續進入監聽狀態。

0x01socks的程式設計

0x1客戶端的編寫

(1)首先是Socket類的構造方法public Socket(String host, int port)

,前面是你連線的服務端的ip,後面是連線埠。
(2)socketlei下面的成員方法

getInputStream() 
          返回此套接字的輸入流
getOutputStream() 
          返回此套接字的輸出流
 void bind(SocketAddress bindpoint) 
          將套接字繫結到本地地址。 
 void close() 
          關閉此套接字。 
 void connect(SocketAddress endpoint) 
          將此套接字連線到伺服器。 

(3)一些方法的使用,傳輸資料
a.outputstream.write("資料".getbytes())

需要把型別轉化成位元組型別看一看它的原始碼public void write(byte b[])
b.可以利用另一個類去完成PrintWriter : 高階輸出流
c.PrintWriter pw3 = new PrintWriter(socket.getOutputStream());傳送給伺服器或者客戶端
d.Scanner scan3 = new Scanner(socket.getInputStream())網路通訊裡面去讀取對方發給我的資訊
e.用緩衝流去接受資訊

InputStream is=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
class Mysocks_test1{
    public static void main(String[] args) {
        try {
            Socket socket=new Socket("127.0.0.1",9099);
            OutputStream os=socket.getOutputStream();
            PrintWriter pw=new PrintWriter(os);
            pw.write("客戶端傳送資訊");
            pw.flush();
            socket.shutdownOutput();
            InputStream is=socket.getInputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(is));//按照字元流讀取,緩衝流
            String info=null;
            while ((info= br.readLine())!=null){
                System.out.println("我接受到的資訊是"+info);
            }
            br.close();
            is.close();
            os.close();
            pw.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();

        }

    }
    }

0x2服務端的編寫

(1)構建一個執行緒去接受一個客戶端的請求與客戶端互動
(2)InetAddress類從名字就可以看出來ip地址類,

用法:socket.getInetAddress(),獲取ip地址
構造方法public InetAddress getInetAddress()返回的是一個InetAddress物件

看一下程式碼

點選檢視程式碼
public class JAVA_serverSocket {
    public static void main(String[] args) {
            try {
                 ServerSocket serverSocket = new ServerSocket(8080);
                Socket socket=new Socket();
                while (true){
                    socket=serverSocket.accept();
                    ServerThread thread=new ServerThread(socket);
                    thread.start();
                    InetAddress address=socket.getInetAddress();//獲取客戶端的IP
                    System.out.println("目標客戶端的ip是"+address);

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

####0x3服務端執行緒 (1)構建的時候採用的是繼承Thread的方法去構建的,還有兩種方法(寫一個類去實現介面Runnable,呼叫類物件)(匿名內部類)()。 (2)編寫完成後一定要記得關閉所有流和socket
點選檢視程式碼
public class ServerThread extends Thread{
    private Socket socket=null;
    public ServerThread(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        InputStream is=null;
        InputStreamReader isr=null;
        BufferedReader br=null;
        OutputStream os=null;
        PrintWriter pw=null;
        try {
            is=socket.getInputStream();
            isr=new InputStreamReader(is);
            br=new BufferedReader(isr);
            String info=null;
            while ((info= br.readLine())!=null);{
                System.out.println("我是服務,客戶機說"+br.readLine());
            }
            socket.shutdownInput();
            os=socket.getOutputStream();
            pw=new PrintWriter(os);
            pw.write("伺服器歡迎你");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                if (pw != null) {
                    pw.close();
                if (os != null) {
                    os.close();
                    }                    }
                if (br != null) {
                    br.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (is != null) {
                    is.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
        }
    }

的編寫 ###0x02總結 這裡只寫了一個專業的兩個客戶端,其實還有很多改進的地方可以通過代理詞去進行會話管理,還沒有進行一下手動的輸入資訊,和傳送過去的資料可以當做cmd的指令來執行,這個東西可能給以後的shell獲取有關 學習一點免殺和shell設計之後再來繼續深入 其他要用於payload的傳送,還有就是寫一些內網探測的指令碼,但是這些在python上面已經寫好了大部分的工具了,以後可能做系統的軟體開發的話可能會用到 socket就先告別一個段落把