Java網絡編程之基于TCP協議
更新時間:2021年05月24日 10:51:04 作者:花傷情猶在
本文主要將Java基于TCP的網絡編程主要分解成5個功能:功能分解1:單向通信功能分解,2:雙向通信功能分解,3:對象流傳送功能分解,4:加入完整的處理異常方式功能分解,5:多線程接收用戶請求,需要的朋友可以參考下
一、單向通信
功能:客戶端發(fā)送一句話到服務器:
客戶端:
public class TestClient {//客戶端 //這是一個main方法,是程序的入口: public static void main(String[] args) throws IOException { //1.創(chuàng)建套接字:指定服務器的ip和端口號: Socket s = new Socket("192.168.199.217",8888); //2.對于程序員來說,向外發(fā)送數據 感受 --》利用輸出流: OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); //利用這個OutputStream就可以向外發(fā)送數據了,但是沒有直接發(fā)送String的方法 //所以我們又在OutputStream外面套了一個處理流:DataOutputStream dos.writeUTF("你好"); //3.關閉流 + 關閉網絡資源: dos.close(); os.close(); s.close(); } }
服務器端:
public class TestServer {//服務器 //這是一個main方法,是程序的入口: public static void main(String[] args) throws IOException { //1.創(chuàng)建套接字: 指定服務器的端口號 ServerSocket ss = new ServerSocket(8888); //2.等著客戶端發(fā)來的信息: Socket s = ss.accept();//阻塞方法:等待接收客戶端的數據,什么時候接收到數據,什么時候程序繼續(xù)向下執(zhí)行。 //accept()返回值為一個Socket,這個Socket其實就是客戶端的Socket //接到這個Socket以后,客戶端和服務器才真正產生了連接,才真正可以通信了 //3.感受到的操作流: InputStream is = s.getInputStream(); DataInputStream dis = new DataInputStream(is); //4.讀取客戶端發(fā)來的數據: String str = dis.readUTF(); System.out.println("客戶端發(fā)來的數據為:"+str); //5.關閉流+關閉網絡資源: dis.close(); is.close(); s.close(); ss.close(); } }
測試:
先開啟客戶端還是先開啟服務器:先開服務器,再開啟客戶端 側面驗證:先開客戶端:出錯。
如圖:
二、雙向通信
客戶端:
import java.io.*; import java.net.Socket; public class TestClient {//客戶端 //這是一個main方法,是程序的入口: public static void main(String[] args) throws IOException { //1.創(chuàng)建套接字:指定服務器的ip和端口號: Socket s = new Socket("192.168.199.217",8888); //2.對于程序員來說,向外發(fā)送數據 感受 --》利用輸出流: OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); //利用這個OutputStream就可以向外發(fā)送數據了,但是沒有直接發(fā)送String的方法 //所以我們又在OutputStream外面套了一個處理流:DataOutputStream dos.writeUTF("你好"); //接收服務器端的回話--》利用輸入流: InputStream is = s.getInputStream(); DataInputStream dis = new DataInputStream(is); String str = dis.readUTF(); System.out.println("服務器端對我說:"+str); //3.關閉流 + 關閉網絡資源: dis.close(); is.close(); dos.close(); os.close(); s.close(); } }
服務器端:
import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class TestServer {//服務器 //這是一個main方法,是程序的入口: public static void main(String[] args) throws IOException { //1.創(chuàng)建套接字: 指定服務器的端口號 ServerSocket ss = new ServerSocket(8888); //2.等著客戶端發(fā)來的信息: Socket s = ss.accept();//阻塞方法:等待接收客戶端的數據,什么時候接收到數據,什么時候程序繼續(xù)向下執(zhí)行。 //accept()返回值為一個Socket,這個Socket其實就是客戶端的Socket //接到這個Socket以后,客戶端和服務器才真正產生了連接,才真正可以通信了 //3.感受到的操作流: InputStream is = s.getInputStream(); DataInputStream dis = new DataInputStream(is); //4.讀取客戶端發(fā)來的數據: String str = dis.readUTF(); System.out.println("客戶端發(fā)來的數據為:"+str); //向客戶端輸出一句話:---》操作流---》輸出流 OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF("你好,我是服務器端,我接受到你的請求了"); //5.關閉流+關閉網絡資源: dos.close(); os.close(); dis.close(); is.close(); s.close(); ss.close(); } }
注意:關閉防火墻
三、對象流傳送
封裝的User類:
import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 9050691344308365540L; private String name; private String pwd; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public User(String name, String pwd) { this.name = name; this.pwd = pwd; } }
客戶端:
import java.io.*; import java.net.Socket; import java.util.Scanner; public class TestClient {//客戶端 //這是一個main方法,是程序的入口: public static void main(String[] args) throws IOException { //1.創(chuàng)建套接字:指定服務器的ip和端口號: Socket s = new Socket("192.168.199.217",8888); //錄入用戶的賬號和密碼: Scanner sc = new Scanner(System.in); System.out.println("請錄入您的賬號:"); String name = sc.next(); System.out.println("請錄入您的密碼:"); String pwd = sc.next(); //將賬號和密碼封裝為一個User的對象: User user = new User(name,pwd); //2.對于程序員來說,向外發(fā)送數據 感受 --》利用輸出流: OutputStream os = s.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); oos.writeObject(user); //接收服務器端的回話--》利用輸入流: InputStream is = s.getInputStream(); DataInputStream dis = new DataInputStream(is); boolean b = dis.readBoolean(); if(b){ System.out.println("恭喜,登錄成功"); }else{ System.out.println("對不起,登錄失敗"); } //3.關閉流 + 關閉網絡資源: dis.close(); is.close(); oos.close(); os.close(); s.close(); } }
服務器:
import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class TestServer {//服務器 //這是一個main方法,是程序的入口: public static void main(String[] args) throws IOException, ClassNotFoundException { //1.創(chuàng)建套接字: 指定服務器的端口號 ServerSocket ss = new ServerSocket(8888); //2.等著客戶端發(fā)來的信息: Socket s = ss.accept();//阻塞方法:等待接收客戶端的數據,什么時候接收到數據,什么時候程序繼續(xù)向下執(zhí)行。 //accept()返回值為一個Socket,這個Socket其實就是客戶端的Socket //接到這個Socket以后,客戶端和服務器才真正產生了連接,才真正可以通信了 //3.感受到的操作流: InputStream is = s.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); //4.讀取客戶端發(fā)來的數據: User user = (User)(ois.readObject()); //對對象進行驗證: boolean flag = false; if(user.getName().equals("娜娜")&&user.getPwd().equals("123123")){ flag = true; } //向客戶端輸出結果:---》操作流---》輸出流 OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeBoolean(flag); //5.關閉流+關閉網絡資源: dos.close(); os.close(); ois.close(); is.close(); s.close(); ss.close(); } }
四、加入完整的處理異常方式
服務器端:
import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class TestServer {//服務器 //這是一個main方法,是程序的入口: public static void main(String[] args) { //1.創(chuàng)建套接字: 指定服務器的端口號 ServerSocket ss = null; Socket s = null; InputStream is = null; ObjectInputStream ois = null; OutputStream os = null; DataOutputStream dos = null; try { ss = new ServerSocket(8888); //2.等著客戶端發(fā)來的信息: s = ss.accept();//阻塞方法:等待接收客戶端的數據,什么時候接收到數據,什么時候程序繼續(xù)向下執(zhí)行。 //accept()返回值為一個Socket,這個Socket其實就是客戶端的Socket //接到這個Socket以后,客戶端和服務器才真正產生了連接,才真正可以通信了 //3.感受到的操作流: is = s.getInputStream(); ois = new ObjectInputStream(is); //4.讀取客戶端發(fā)來的數據: User user = (User)(ois.readObject()); //對對象進行驗證: boolean flag = false; if(user.getName().equals("娜娜")&&user.getPwd().equals("123123")){ flag = true; } //向客戶端輸出結果:---》操作流---》輸出流 os = s.getOutputStream(); dos = new DataOutputStream(os); dos.writeBoolean(flag); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } finally { //5.關閉流+關閉網絡資源: try { if(dos!=null){ dos.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(ois!=null){ ois.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(is!=null){ is.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(s!=null){ s.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(ss!=null){ ss.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
客戶端:
import java.io.*; import java.net.Socket; import java.util.Scanner; public class TestClient {//客戶端 //這是一個main方法,是程序的入口: public static void main(String[] args){ //1.創(chuàng)建套接字:指定服務器的ip和端口號: Socket s = null; OutputStream os = null; ObjectOutputStream oos = null; InputStream is = null; DataInputStream dis = null; try { s = new Socket("192.168.199.217",8888); //錄入用戶的賬號和密碼: Scanner sc = new Scanner(System.in); System.out.println("請錄入您的賬號:"); String name = sc.next(); System.out.println("請錄入您的密碼:"); String pwd = sc.next(); //將賬號和密碼封裝為一個User的對象: User user = new User(name,pwd); //2.對于程序員來說,向外發(fā)送數據 感受 --》利用輸出流: os = s.getOutputStream(); oos = new ObjectOutputStream(os); oos.writeObject(user); //接收服務器端的回話--》利用輸入流: is = s.getInputStream(); dis = new DataInputStream(is); boolean b = dis.readBoolean(); if(b){ System.out.println("恭喜,登錄成功"); }else{ System.out.println("對不起,登錄失敗"); } } catch (IOException e) { e.printStackTrace(); } finally{ //3.關閉流 + 關閉網絡資源: try { if(dis!=null){ dis.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(is!=null){ is.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(oos!=null){ oos.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(s!=null){ s.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
五、多線程接收用戶請求
問題:
- 服務器針對一個請求服務,之后服務器就關閉了(程序自然結束了)
需要解決:
- 服務器必須一直在監(jiān)聽 ,一直開著,等待客戶端的請求
- 在當前代碼中,客戶端不用動了
更改服務器代碼:
服務器線程:
import java.io.*; import java.net.Socket; public class ServerThread extends Thread {//線程:專門處理客戶端的請求 InputStream is = null; ObjectInputStream ois = null; OutputStream os = null; DataOutputStream dos = null; Socket s = null; public ServerThread(Socket s){ this.s = s; } @Override public void run() { try{ //2.等著客戶端發(fā)來的信息: is = s.getInputStream(); ois = new ObjectInputStream(is); //4.讀取客戶端發(fā)來的數據: User user = (User)(ois.readObject()); //對對象進行驗證: boolean flag = false; if(user.getName().equals("娜娜")&&user.getPwd().equals("123123")){ flag = true; } //向客戶端輸出結果:---》操作流---》輸出流 os = s.getOutputStream(); dos = new DataOutputStream(os); dos.writeBoolean(flag); }catch (IOException | ClassNotFoundException e) { e.printStackTrace(); }finally { try { if(dos!=null){ dos.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(ois!=null){ ois.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(is!=null){ is.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
服務器端:
import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class TestServer {//服務器 //這是一個main方法,是程序的入口: public static void main(String[] args) { System.out.println("服務器啟動了"); //1.創(chuàng)建套接字: 指定服務器的端口號 ServerSocket ss = null; Socket s = null; int count = 0;//定義一個計數器,用來計數 客戶端的請求 try { ss = new ServerSocket(8888); while(true){//加入死循環(huán),服務器一直監(jiān)聽客戶端是否發(fā)送數據 s = ss.accept();//阻塞方法:等待接收客戶端的數據,什么時候接收到數據,什么時候程序繼續(xù)向下執(zhí)行。 //每次過來的客戶端的請求 靠 線程處理: new ServerThread(s).start(); count++; //輸入請求的客戶端的信息: System.out.println("當前是第"+count+"個用戶訪問我們的服務器,對應的用戶是:"+s.getInetAddress()); } } catch (IOException e) { e.printStackTrace(); } } }
到此這篇關于Java網絡編程之基于TCP協議的文章就介紹到這了,更多相關Java基于TCP的網絡編程內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java concurrency集合之ConcurrentSkipListSet_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Java concurrency集合之ConcurrentSkipListSet的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06使用@ConfigurationProperties實現類型安全的配置過程
這篇文章主要介紹了使用@ConfigurationProperties實現類型安全的配置過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02