Socket連接入門

服務器接收客戶端請求步驟: 
  1.創建一個ServerSocket實例,監聽客戶端發來的請求。 
  2.與客戶端獲取連接後,創建一個Socket實例,利用I/O流與客戶端進行通信,完畢後關閉Socket。

  當然,服務器可以接收多個客戶端的請求,所以如果服務器是一個一個順序相應肯定會帶來不好的體驗,因此使用多線程來爲多個客戶端提供服務 

package com.socket;
 
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
 
public class SocketServer {
    public static void main(String[] arg){
        try{
            int count = 0;
            //穿件服務器socket,綁定端口,並監聽此端口
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("***服務器已啓動,等待客戶端連接***");
            //循環監聽等待客戶端連接
            while(true){
                //調用accept()方法開始監聽,等待客戶端連接
                Socket socket =serverSocket.accept();
                //創建一個新的線程
                ServerThread serverThread = new ServerThread(socket);
                //啓動線程
                serverThread.start();
                count++;
                System.out.println("客戶端的數量爲:"+count);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

多線程類處理請求:

package com.socket;
 
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
 
/**
 * 服務器線程處理類
 */
public class ServerThread extends  Thread {
    //和本線程相關的Socket
    Socket socket = null;
    public ServerThread(Socket socket){
        this.socket = socket;
    }
    //線程執行操作,響應客戶端請求
    public void run(){
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        OutputStream os = null;
        PrintWriter pw = null;
        try {
            //3、獲取輸入流,並讀取客戶端信息
            is = socket.getInputStream();
 
            isr = new InputStreamReader(is);//將字節流包裝爲字符流
            br = new BufferedReader(isr);//爲輸入流添加緩衝
            String info = null;
            while((info = br.readLine()) != null){
                System.out.println("我是服務器,客戶端說:" + info);
            }
            socket.shutdownInput();//關閉輸入流
            //4、獲取輸出流,響應客戶端請求
            os = socket.getOutputStream();
            pw = new PrintWriter(os);//包裝爲打印流
            pw.write("歡迎您,服務器接收到你信息。");
            pw.flush();
 
        } 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 (IOException e){
                e.printStackTrace();
            }
        }
    }
}

 

客戶端向服務器發送請求可分爲以下步驟: 
  1.創建一個Socket實例 
  2.利用I/O流與服務器進行通信 
  3.關閉socket 

package com.socket;
 
import java.io.*;
import java.net.Socket;
 
/**
 * 客戶端
 */
public class cline {
    public  static  void main(String[] arg){
        try {
            //1、創建客戶端Socket,目標服務器地址和端口
            Socket socket = new Socket("localhost",8888);
            //2、獲取輸出流,向服務器發送信息
            OutputStream os = socket.getOutputStream();//字節輸出流
            PrintWriter pw = new PrintWriter(os);
            pw.write("用戶名:admin  ; 密碼:5555");
            pw.flush();
            socket.shutdownOutput();//關閉輸出流
 
            //3、接收服務器的響應信息
            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();
 
            pw.close();
            os.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
}

 

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