Java如何实现同时监听任何端口,最好有代码例子!,
2个回答
展开全部
import java.net.*;
import java.io.*;
import javax.swing.*;
public class Server{
public Server() throws Exception{
ServerSocket socket=new ServerSocket(1000); //在1000端口监听
while(true){
Socket s=socket.accept();
while(!s.isConnected()){}
new Processor(s).start();
}
}
public static void main(String args[]) throws Exception{
new Server();
}
}
class Processor extends Thread{
Socket s=null;
public Processor(Socket s) throws Exception{
this.s=s;
}
public void run(){
try{
ObjectInputStream obj=new ObjectInputStream(s.getInputStream());
JOptionPane.showMessageDialog(null,(String)obj.readObject());
obj.close();
}catch(Exception e){}
}
}
import java.io.*;
import javax.swing.*;
public class Server{
public Server() throws Exception{
ServerSocket socket=new ServerSocket(1000); //在1000端口监听
while(true){
Socket s=socket.accept();
while(!s.isConnected()){}
new Processor(s).start();
}
}
public static void main(String args[]) throws Exception{
new Server();
}
}
class Processor extends Thread{
Socket s=null;
public Processor(Socket s) throws Exception{
this.s=s;
}
public void run(){
try{
ObjectInputStream obj=new ObjectInputStream(s.getInputStream());
JOptionPane.showMessageDialog(null,(String)obj.readObject());
obj.close();
}catch(Exception e){}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应该用多线程吧
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询