java实例练习——基于TCP/IP协议的多客户端通信
生活随笔
收集整理的這篇文章主要介紹了
java实例练习——基于TCP/IP协议的多客户端通信
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先說一下大概的思路:
應用多線程來實現服務器與多客戶端之間的通信
1、服務器端創建ServerSocket,循環調用accept()等待客戶端連接;
2、客戶端創建一個Socket并請求與服務器端連接;
3、服務器端接受客戶端請求,創建Socket與該客戶端簡歷專線連接;
4、建立連接的兩個Socket在一個單獨的線程上對話;
5、服務器端繼續等待其他新的連接。
閑話不多說,直接上代碼:
package net; /*** 1、客戶端*/import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException;public class Client {public static void main(String[] args) {try {//1、創建Socket,與服務器建立連接Socket socket = new Socket("localhost", 8889); //2、向服務器端發送消息OutputStream os = socket.getOutputStream(); PrintWriter pwriter = new PrintWriter(os); //將Socket的輸出字節流包裝成打印流pwriter.write("用戶名:sysker ; 密碼:sys154");//刷新流pwriter.flush();//3、關閉資源socket.shutdownOutput();//3、獲取輸入流InputStream inputStream = socket.getInputStream();InputStreamReader inputStreamReader = new InputStreamReader(inputStream);BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String getMsage=null;while((getMsage=bufferedReader.readLine())!=null){System.out.println("客戶端正在等待響應,服務器說:"+getMsage);} //socket.shutdownInput();bufferedReader.close();inputStream.close();inputStreamReader.close();pwriter.close();os.close(); socket.close();} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} package net; /*** 2、服務器端*/import java.io.IOException; import java.net.ServerSocket; import java.net.Socket;public class ManyClientServer {public static void main(String[] args) {Socket socket = null;int clientCount = 0;try {ServerSocket serverSocket = new ServerSocket(8889);while(true){socket = serverSocket.accept();ManyClientThread manyClientThread = new ManyClientThread(socket);manyClientThread.start();clientCount++;System.out.println("客戶端數量"+clientCount);System.out.println("客戶端的IP地址:"+socket.getInetAddress().getHostAddress());} } catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} package net; /*** 3、服務器通信線程*/ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket;public class ManyClientThread extends Thread{Socket socket = null;BufferedReader bufferedReader = null;public ManyClientThread(Socket socket){this.socket = socket;}public void run(){try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));String mString = null;while((mString = bufferedReader.readLine())!=null){System.out.println("我是服務器,客戶端說"+mString); }bufferedReader.close();socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(bufferedReader!=null){try {bufferedReader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(socket!=null){try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}} }轉載于:https://www.cnblogs.com/caoleiCoding/p/6441622.html
總結
以上是生活随笔為你收集整理的java实例练习——基于TCP/IP协议的多客户端通信的全部內容,希望文章能夠幫你解決所遇到的問題。