Java socket server 不能同时接收很多数据,如何让java socket server 同时接收数据?
Java socket server cannot receive many data at the same time, how to make java socket server accept data at the same time?
我的java套接字服务器不能在一个客户端同时接受多个数据
Thread t1 = new Thread(() -> {
ServerSocket ss = null;
try {
ss = new ServerSocket(9000);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(true) {
System.out.println("Waiting Transaction ..");
Socket clientSocket = ss.accept();
InetAddress inet = clientSocket.getInetAddress();
try{
while (clientSocket.getInputStream().available() == 0) {
Thread.sleep(100L);
}
byte[] data;
int bytes;
data = new byte[clientSocket.getInputStream().available()];
bytes = clientSocket.getInputStream().read(data,0,data.length);
String dataDB = new String(data, 0, bytes, "UTF-8");
System.out.println("received data\n time : "+ new Date() +"length data : " + dataDB.length());
System.out.println(dataDB);
String dataFrom = getFromServer(dataDB);
clientSocket.getOutputStream().write(dataFrom.getBytes("UTF-8"));
}catch (BindException be){
be.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally {
clientSocket.close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
t1.start();
我尝试使用线程但它不起作用,此代码只接受第一个数据,否则另一个数据将被拒绝。服务器如何同时接收多个数据?
让我告诉你我是如何做到的,在玩套接字的同时,我创建了一个 SocketServer class
public class Server {
public static final Integer port = 9000;
private final ServerSocket server;
private final ExecutorService service = Executors.newFixedThreadPool(10);
private final AtomicInteger idGenerator = new AtomicInteger(0);
public Server() throws IOException {
this.server = new ServerSocket(port);
}
public void start() {
try {
while (true) {
Worker worker = new Worker(idGenerator.incrementAndGet(), server.accept());
service.execute(worker);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
现在每次有客户端加入,我都会创建一个工作线程并将其提交给 ExecutorService
(执行器服务有线程池和 运行 通过分配一个线程给它传递的工作线程,你可以阅读它)。
工人class长这样
public class Worker implements Runnable {
private final Socket client;
private final Logger log = LoggerFactory.getLogger(this.getClass());
public Worker(Integer id, Socket client) {
this.id = id; //Ignore it, it was for logging purpose, how many joined
this.client = client;
}
@Override
public void run() {
listenClientMessages();
closeConnection();
}
private void listenClientMessages() {
final int MAX_INPUT = 1024;
int read;
try (InputStream is = client.getInputStream()) {
byte[] buf = new byte[MAX_INPUT];
while ((read = is.read(buf)) != -1) {
String line = new String(buf, 0, read);
log.info(line);
if (line.equals("bye")) break;
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void closeConnection() {
try{
client.close();
}catch (IOException ex) {
ex.printStackTrace();
}
}
}
所以,你可以看到,我正在读取字节,而 look
byte[] buf = new byte[MAX_INPUT];
while ((read = is.read(buf)) != -1) {
String line = new String(buf, 0, read);
log.info(line);
if (line.equals("bye")) break;
}
这就是为什么处理 MAX_INPUT 或更少时,它会等待客户端发送下一个输入。
如果有帮助请告诉我。
编辑: 正如@user207421 在评论中评论的那样,closeConnection
函数是多余的,因为在 [=15= 中使用上面的 try-wit-resources 块关闭了输入流] 函数,所以不需要。
我的java套接字服务器不能在一个客户端同时接受多个数据
Thread t1 = new Thread(() -> {
ServerSocket ss = null;
try {
ss = new ServerSocket(9000);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(true) {
System.out.println("Waiting Transaction ..");
Socket clientSocket = ss.accept();
InetAddress inet = clientSocket.getInetAddress();
try{
while (clientSocket.getInputStream().available() == 0) {
Thread.sleep(100L);
}
byte[] data;
int bytes;
data = new byte[clientSocket.getInputStream().available()];
bytes = clientSocket.getInputStream().read(data,0,data.length);
String dataDB = new String(data, 0, bytes, "UTF-8");
System.out.println("received data\n time : "+ new Date() +"length data : " + dataDB.length());
System.out.println(dataDB);
String dataFrom = getFromServer(dataDB);
clientSocket.getOutputStream().write(dataFrom.getBytes("UTF-8"));
}catch (BindException be){
be.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally {
clientSocket.close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
t1.start();
我尝试使用线程但它不起作用,此代码只接受第一个数据,否则另一个数据将被拒绝。服务器如何同时接收多个数据?
让我告诉你我是如何做到的,在玩套接字的同时,我创建了一个 SocketServer class
public class Server {
public static final Integer port = 9000;
private final ServerSocket server;
private final ExecutorService service = Executors.newFixedThreadPool(10);
private final AtomicInteger idGenerator = new AtomicInteger(0);
public Server() throws IOException {
this.server = new ServerSocket(port);
}
public void start() {
try {
while (true) {
Worker worker = new Worker(idGenerator.incrementAndGet(), server.accept());
service.execute(worker);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
现在每次有客户端加入,我都会创建一个工作线程并将其提交给 ExecutorService
(执行器服务有线程池和 运行 通过分配一个线程给它传递的工作线程,你可以阅读它)。
工人class长这样
public class Worker implements Runnable {
private final Socket client;
private final Logger log = LoggerFactory.getLogger(this.getClass());
public Worker(Integer id, Socket client) {
this.id = id; //Ignore it, it was for logging purpose, how many joined
this.client = client;
}
@Override
public void run() {
listenClientMessages();
closeConnection();
}
private void listenClientMessages() {
final int MAX_INPUT = 1024;
int read;
try (InputStream is = client.getInputStream()) {
byte[] buf = new byte[MAX_INPUT];
while ((read = is.read(buf)) != -1) {
String line = new String(buf, 0, read);
log.info(line);
if (line.equals("bye")) break;
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void closeConnection() {
try{
client.close();
}catch (IOException ex) {
ex.printStackTrace();
}
}
}
所以,你可以看到,我正在读取字节,而 look
byte[] buf = new byte[MAX_INPUT];
while ((read = is.read(buf)) != -1) {
String line = new String(buf, 0, read);
log.info(line);
if (line.equals("bye")) break;
}
这就是为什么处理 MAX_INPUT 或更少时,它会等待客户端发送下一个输入。
如果有帮助请告诉我。
编辑: 正如@user207421 在评论中评论的那样,closeConnection
函数是多余的,因为在 [=15= 中使用上面的 try-wit-resources 块关闭了输入流] 函数,所以不需要。