保證socket客戶端實(shí)例同步
啟動(dòng)客戶端連接? 并啟動(dòng) 輸入流線程 同時(shí)保證socket客戶端實(shí)例同步
讀線程獲取當(dāng)前客戶端的 輸入流
循環(huán)讀取
上代碼
服務(wù)端:
package com.my.socket.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
? ? private static int port=8888;
? ? private static int index = 1;
? ? public SocketServer(int port) {
? ? ? ? this.port = port;
? ? }
? ? public static void main(String[] args) {
? ? ? ? try {
? ? ? ? ? ? /**
? ? ? ? ? ? * 創(chuàng)建 socket 服務(wù) 并監(jiān)聽
? ? ? ? ? ? */
? ? ? ? ? ? ServerSocket server = new ServerSocket(port);
? ? ? ? ? ? System.out.println("成功啟動(dòng)服務(wù)端,等待客戶端連接袒炉。乍钻。白对。。");
? ? ? ? ? ? while (true) {
? ? ? ? ? ? ? ? // 監(jiān)聽客戶連接 如果有連接 放入一個(gè)新的線程當(dāng)中
? ? ? ? ? ? ? ? Socket? client = server.accept();
? ? ? ? ? ? ? ? System.out.println(index+"號(hào)客戶端連接成功");
? ? ? ? ? ? ? ? // 開始線程
? ? ? ? ? ? ? ? Thread clientThread = new Thread(new SocketServerRunnable(client, index + "號(hào)客戶端"));
? ? ? ? ? ? ? ? clientThread.start();
? ? ? ? ? ? ? ? index++;
? ? ? ? ? ? }
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}
package com.my.socket.server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
/**
* @author zhanghaotian
*
*/
public class SocketServerRunnable implements Runnable {
? ? private volatile Socket client = null;
? ? private volatile String name;
? ? public SocketServerRunnable(Socket client, String name) {
? ? ? ? this.client = client;
? ? ? ? this.name = name;
? ? }
? ? @Override
? ? public void run() {
? ? ? ? try {
? ? ? ? ? ? DataOutputStream dos = new DataOutputStream(this.client.getOutputStream());
? ? ? ? ? ? DataInputStream dis = new DataInputStream(this.client.getInputStream());
? ? ? ? ? ? while (true) {
? ? ? ? ? ? ? ? if (client.isClosed())
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? String utf = dis.readUTF();
? ? ? ? ? ? ? ? dos.writeUTF("服務(wù)器消息:接收數(shù)據(jù)" + this.name + "并返回垄潮。" + utf);
? ? ? ? ? ? ? ? dos.flush();
? ? ? ? ? ? }
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? return;
? ? ? ? }
? ? }
}
客戶端:
package com.my.socket.client;
/**
*? 可以 啟動(dòng)? 多個(gè) 客戶端 測(cè)試? 可以多線程讀取
* @author zhanghaotian
*
*/
public class SocketClient {
? ? public static void main(String[] args) throws Exception {
? ? ? ? ? ? new Thread(new SocketClientRunnable()).start();
? ? }
}
package com.my.socket.client;
import java.io.DataInputStream;
import java.io.IOException;
/**
* 持續(xù)讀取服務(wù)端數(shù)據(jù)
*
* @author zhanghaotian
*
*/
public class SocketClientReadIO implements Runnable {
? ? private volatile DataInputStream dis = null;
? ? private volatile int index = 0;
? ? public? SocketClientReadIO(DataInputStream dis) {
? ? ? ? this.dis = dis;
? ? }
? ? @Override
? ? public void run() {
? ? ? ? while (true) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? System.out.println("我是讀取線程:"+dis.readUTF());
? ? ? ? ? ? ? ? index++;
? ? ? ? ? ? ? ? //放慢讀取的速度 看看會(huì)出現(xiàn)什么樣的后果
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? Thread.sleep(2000);
? ? ? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? ? ? // TODO Auto-generated catch block
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return;
? ? }
}
package com.my.socket.client;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.plaf.SliderUI;
public class SocketClientRunnable implements Runnable {
? ? @Override
? ? public void run() {
? ? ? ? try {
? ? ? ? ? ? // 連接服務(wù)端
? ? ? ? ? ? Socket client = new Socket("127.0.0.1", 8888);
? ? ? ? ? ? // 啟動(dòng)讀取線程
? ? ? ? ? ? new Thread(new SocketClientReadIO(new DataInputStream(client.getInputStream()))).start();
? ? ? ? ? ? DataOutputStream dos = new DataOutputStream(client.getOutputStream()); // 獲取輸出流
? ? ? ? ? ? int index = 0;
? ? ? ? ? ? while (index < 100) {
? ? ? ? ? ? ? ? index++;
? ? ? ? ? ? ? ? // 輸出信息到服務(wù)端
? ? ? ? ? ? ? ? dos.writeUTF("你好服務(wù)端!" + index);
? ? ? ? ? ? ? ? dos.flush();
? ? ? ? ? ? }
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? System.out.println("向服務(wù)端寫入數(shù)據(jù)完畢 寫入數(shù)據(jù)數(shù)量:" + index+"線程等待中....");
? ? ? ? ? ? ? ? Thread.sleep(10000000);
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? // TODO Auto-generated catch block
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? } catch (IOException e) {
? ? ? ? }
? ? }
}