我正在使用套接字编写多个客户端-java 中的服务器应用程序。我编写了简单的客户端-服务器应用程序,一切正常,但是当我尝试将其更改为多客户端应用程序时,当我启动客户端时出现异常:
Exception in thread "pool-1-thread-1" java.lang.NullPointerException
at MiniSerwer.run(Serwer.java:110)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
我还有两个类(线程 - InWorker 和 OutWorker 用于输入和输出)。
Serwer.java(没有 InWorker 和 OutWorker):
public class Serwer {
Serwer(int port) {
ServerSocket serversocket=null;
ExecutorService exec= Executors.newCachedThreadPool();
try {
serversocket=new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
while(true) {
Socket socket=null;
try {
socket = serversocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
exec.execute(new MiniSerwer(socket)); // create new thread
}
} }
MiniSerwer - 为每个客户端创建拥有线程的助手类
class MiniSerwer implements Runnable{
Socket socket=null;
ExecutorService exec=null;
ObjectOutputStream oos=null;
ObjectInputStream ois=null;
MiniSerwer(Socket socket) {
this.socket=socket;
try {
oos=new ObjectOutputStream(socket.getOutputStream());
oos.flush();
ois=new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while(true) {
exec.execute(new InWorker(socket, ois)); // input stream
exec.execute(new OutWorker(socket, oos)); //output stream
Thread.yield();
}
}
}
我改变了我的程序,但它仍然不起作用。还有什么建议吗?
服务器:
public class Serwer implements Runnable{
ServerSocket serversocket=null;
ExecutorService exec= Executors.newCachedThreadPool();
int port;
Serwer(int port) {
this.port=port;
}
public void run() {
try {
serversocket=new ServerSocket(port);
while(true) {
Socket socket=null;
try {
socket = serversocket.accept();
exec.execute(new MiniSerwer(socket)); // create new thread
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
int port;
Scanner in = new Scanner(System.in);
System.out.println("Enter the port:");
port = in.nextInt();
ExecutorService exec=Executors.newCachedThreadPool();
exec.execute(new Serwer(port));
}
}
迷你服务器:
class MiniSerwer implements Runnable{
Socket socket=null;
ExecutorService exec=Executors.newCachedThreadPool();
ObjectOutputStream oos=null;
ObjectInputStream ois=null;
MiniSerwer(Socket socket) {
this.socket=socket;
}
public void run() {
try {
oos=new ObjectOutputStream(socket.getOutputStream());
oos.flush();
ois=new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
while(true) {
exec.execute(new InWorker(socket, ois)); // input stream
exec.execute(new OutWorker(socket, oos)); //output stream
Thread.yield();
}
}
}
当我尝试从客户端向服务器发送消息时,我有很多例外。
2 秒。在服务器端连接后(我什么都不发送):
Exception in thread "pool-2-thread-1" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:657)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:943)
at java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:992)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)