嗨我真的想创建一个多线程Web服务器 我已经提到 Link 1 Link 2多线程Web服务器
import java.net.*;
import java.io.*;
public class WebServer {
ServerSocket server;
public WebServer() {
try {
server=new ServerSocket(3000);
} catch (IOException ex) {
System.out.println("exc in const "+ex.getMessage());
}
}
Socket client;
BufferedReader br;
Thread t=new Thread(new Runnable(){
@Override
public void run() {
try{
System.out.println(br.readLine());
}
catch(Exception e){
System.out.println("exc is "+e);
}
}
});
public void RUN(){
while(true){
try {
client=server.accept();
System.out.println(client.getPort());
if(client!=null){
r=new BufferedReader(new InputStreamReader(client.getInputStream()));
t.start();
}
} catch (IOException ex) {
System.out.println("ex is "+ex.getMessage());
}
}
}
public static void main(String[] args) {
WebServer webserver=new WebServer();
try {
webserver.RUN();
} catch (Exception e) {
System.out.println("main "+e);
}
}
}
在上面的代码中,我不断收到一个线程非法访问异常那就是为什么我不断收到此异常 我想打开浏览器中的多个选项卡并打开本地主机:3000然后服务器必须打印http请求和端口号,但这种情况只发生在第一个客户端,而不是其他的它显示非法线程访问异常和程序终止
将大大欣赏是否有任何页面告诉如何在Web浏览器中显示一些内容。
将帮助,如果你添加异常的堆栈跟踪 –
嗨,我使用trycatch块,所以我只得到异常的消息我们得到一个堆栈跟踪只有当程序执行停止由于没有被捕获仪式的异常仪式? – AAB
我会从实施一个简单的生产者/消费者模式开始。不要粗鲁,但我认为一个网络服务器可能会超出你的能力。 –