java學習之socket程式設計
阿新 • • 發佈:2022-04-22
0x00前言和思維導圖
Socks實際上是什麼:實際上是提供了精彩通訊的埠,在通訊之前雙方都必須要創造一個端點才能通訊,其實感覺socket跟計算機的三次握手有些相似,分為三個步驟:
(1)伺服器監聽:伺服器不知道下一個客戶端的具體套接字,而是處於等待連線的狀態
(2)客戶端請求:由客戶端的套接字提出請求,連線目標是伺服器的套接字。
(3)連線確認:伺服器接收到客戶端的套接字請求,它就響應客戶端的請求,建立一個新的執行緒把伺服器套接字的具體描述發個客戶端,客戶端確認以後,這個連線就建立好了,服務端繼續進入監聽狀態。
0x01socks的程式設計
0x1客戶端的編寫
(1)首先是Socket類的構造方法public Socket(String host, int port)
(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();
}
}
}
點選檢視程式碼
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();
}
}
}
}