保证socket客户端实例同步
启动客户端连接 并启动 输入流线程 同时保证socket客户端实例同步
读线程获取当前客户端的 输入流
循环读取
上代码
服务端:
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 {
/**
* 创建 socket 服务 并监听
*/
ServerSocket server = new ServerSocket(port);
System.out.println("成功启动服务端,等待客户端连接。。。。");
while (true) {
// 监听客户连接 如果有连接 放入一个新的线程当中
Socket client = server.accept();
System.out.println(index+"号客户端连接成功");
// 开始线程
Thread clientThread = new Thread(new SocketServerRunnable(client, index + "号客户端"));
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("服务器消息:接收数据" + this.name + "并返回。" + utf);
dos.flush();
}
} catch (IOException e) {
return;
}
}
}
客户端:
package com.my.socket.client;
/**
* 可以 启动 多个 客户端 测试 可以多线程读取
* @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;
/**
* 持续读取服务端数据
*
* @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++;
//放慢读取的速度 看看会出现什么样的后果
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 {
// 连接服务端
Socket client = new Socket("127.0.0.1", 8888);
// 启动读取线程
new Thread(new SocketClientReadIO(new DataInputStream(client.getInputStream()))).start();
DataOutputStream dos = new DataOutputStream(client.getOutputStream()); // 获取输出流
int index = 0;
while (index < 100) {
index++;
// 输出信息到服务端
dos.writeUTF("你好服务端!" + index);
dos.flush();
}
try {
System.out.println("向服务端写入数据完毕 写入数据数量:" + index+"线程等待中....");
Thread.sleep(10000000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
}
}
}