用java語言建構一個網頁伺服器,實作客戶端與伺服器之間通訊,實作客戶端擁有獨立執行緒,互不干擾。
應用多執行緒來實作伺服器與多執行緒之間的通訊的基本步驟
伺服器端建立ServerSocket,循環呼叫accept()等待客戶端連結
客戶端建立一個Socket並要求和伺服器端連結
伺服器端接受客戶端請求,建立socekt與該客戶端建立專線連結
建立連結的socket在單獨的執行緒上對話
伺服器繼續等待新的連結
伺服器端Server.java
package test.concurrent.socket; import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * Created by dong on 15-6-22. * 基于TCP协议的Socket通信,实现用户登录 * 服务器端 */ public class Server { public static void main(String[] args) { try { //1、创建一个服务器端Socket,即ServerSocket, 指定绑定的端口,并监听此端口 ServerSocket serverSocket = new ServerSocket(8888); Socket socket = null; //记录客户端的数量 int count = 0; System.out.println("***服务器即将启动,等待客户端的链接***"); //循环监听等待客户端的链接 while (true){ //调用accept()方法开始监听,等待客户端的链接 socket = serverSocket.accept(); //创建一个新的线程 ServerThread serverThread = new ServerThread(socket); //启动线程 serverThread.start(); count++; //统计客户端的数量 System.out.println("客户端的数量: " + count); InetAddress address = socket.getInetAddress(); System.out.println("当前客户端的IP : " + address.getHostAddress()); } } catch (IOException e) { e.printStackTrace(); } } }
伺服器端
伺服器端Server.java
package test.concurrent.socket; import java.io.*; import java.net.Socket; /** * Created by dong on 15-6-22. * 服务器端线程处理类 */ 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 { //获取一个输入流,并读取客户端的信息 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(); //关闭输入流 //获取输出流,响应客户端的请求 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 (is != null) is.close(); if (isr != null) isr.close(); if (br != null) br.close(); if (socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }處理類ServerThread.java
package test.concurrent.socket; import java.io.*; import java.net.Socket; /** * Created by dong on 15-6-22. * 客户端 */ public class Client { public static void main(String[] args) { try { //1、创建客户端Socket,指定服务器端口号和地址 Socket socket = new Socket("localhost",8888); //2、获取输出流,向服务器发送信息 OutputStream os = socket.getOutputStream(); //字节输出流 PrintWriter pw = new PrintWriter(os); //将输出流包装为打印流 pw.write("用户名:tom; 密码:456"); pw.flush(); socket.shutdownOutput(); //关闭输出流 InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String info = null; //循环读取 while ((info = br.readLine()) != null){ System.out.println("我是客户端:服务器说:" + info); } br.close(); is.close(); isr.close(); pw.close(); os.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }客戶端Client.java
rrreee
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援PHP中文網。
更多java多執行緒實作伺服器端與多客戶端之間的通訊相關文章請關注PHP中文網!