Socket() Socket(InetAddress address, int port)throws UnknownHostException, IOException Socket(InetAddress address, int port, InetAddress localAddress, int localPort)throws IOException Socket(String host, int port)throws UnknownHostException, IOException Socket(String host, int port, InetAddress localAddress, int localPort)throws IOException
boolean isConnection=socket.isConnected() && !socket.isClosed(); //判断当前是否处于连接
ServerSocket()throws IOException ServerSocket(int port)throws IOException ServerSocket(int port, int backlog)throws IOException ServerSocket(int port, int backlog, InetAddress bindAddr)throws IOException
public void service(){
while(true){
Socket socket=null;
try{
socket=serverSocket.accept();//从连接队列中取出一个连接,如果没有则等待
System.out.println("新增连接:"+socket.getInetAddress()+":"+socket.getPort());
...//接收和发送数据
}catch(IOException e){e.printStackTrace();}finally{
try{
if(socket!=null) socket.close();//与一个客户端通信结束后,要关闭Socket
}catch(IOException e){e.printStackTrace();}
}
}
}
public void service(){
while(true){
Socket socket=null;
try{
socket=serverSocket.accept(); //主线程获取客户端连接
Thread workThread=new Thread(new Handler(socket)); //创建线程
workThread.start(); //启动线程
}catch(Exception e){
e.printStackTrace();
}
}
}
class Handler implements Runnable{
private Socket socket;
public Handler(Socket socket){
this.socket=socket;
}
public void run(){
try{
System.out.println("新连接:"+socket.getInetAddress()+":"+socket.getPort());
Thread.sleep(10000);
}catch(Exception e){e.printStackTrace();}finally{
try{
System.out.println("关闭连接:"+socket.getInetAddress()+":"+socket.getPort());
if(socket!=null)socket.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有