java socket读取基站下手环定位卡最终版本

版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/82811770

1.多线程
2.轮询在这里插入图片描述

package cn.com.gj;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import cn.com.gj.ThreadReader;
public class Crc{
	//1,循环的基站
	public static void main(String[] args) {
		Port();
//		Timer time=new Timer();
//		time.schedule(new TimerTask() {
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				Port();
//			}
//		}, 60000);
//		
				}

public static void Port(){
	ServerSocket server0 = null;
	try {
		server0 = new ServerSocket(6010);
		Send(server0,6010);
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		try {
			server0.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	ServerSocket server1 = null;
	try {
		server1 = new ServerSocket(6011);
		Send(server1,6011);
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		try {
			server1.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	try {
		ServerSocket server2= new ServerSocket(6012);
		Send(server2,6012);
	    server2.close();
		ServerSocket server3 = new ServerSocket(6013);
		Send(server3,6013);
	    server3.close();
		ServerSocket server4= new ServerSocket(6014);
		Send(server4,6014);
	    server4.close();
		ServerSocket server5 = new ServerSocket(6015);
		Send(server5,6015);
	    server5.close();
		ServerSocket server6= new ServerSocket(6016);
		Send(server6,6016);
	    server6.close();
		ServerSocket server7 = new ServerSocket(6017);
		Send(server7,6017);
	    server7.close();
		ServerSocket server8= new ServerSocket(6018);
		Send(server8,6018);
	    server8.close();
		ServerSocket server9= new ServerSocket(6019);
		Send(server9,6019);
	    server9.close();
		ServerSocket server10= new ServerSocket(6020);
		Send(server10,6020);
	    server10.close();
		ServerSocket server11= new ServerSocket(6021);
		Send(server11,6021);
		server11.close();
		ServerSocket server12= new ServerSocket(6022);
		Send(server12,6022);
		  server12.close();
		ServerSocket server13= new ServerSocket(6040);
		Send(server13,6040);
		server13.close();
		ServerSocket server14= new ServerSocket(6041);
		Send(server14,6041);
	    //关闭所有的流
		 server14.close(); 
		 Port();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	
}
	public static void Send(ServerSocket server,int m){
		//新建一个连接客户端的线程
		Thread threadread;
		try {
	
			    	    Socket socket = server.accept(); 
						System.out.println("socket:"+socket);
						new Thread(new ThreadReader(socket.getInputStream())).start();
						new Thread(new ThreadReader(socket.getInputStream())).start();
						new Thread(new ThreadReader(socket.getInputStream())).start();
						new Thread(new ThreadReader(socket.getInputStream())).start();
						new Thread(new ThreadReader(socket.getInputStream())).start();
						new Thread(new ThreadReader(socket.getInputStream())).start();	
						new Thread(new ThreadReader(socket.getInputStream())).start();
						new Thread(new ThreadReader(socket.getInputStream())).start();
						new Thread(new ThreadReader(socket.getInputStream())).start();
						new Thread(new ThreadReader(socket.getInputStream())).start();
						new Thread(new ThreadReader(socket.getInputStream())).start();
						new Thread(new ThreadReader(socket.getInputStream())).start();
						new Thread(new ThreadReader(socket.getInputStream())).start();
			    	 System.out.println("结束");

				
		} catch (IOException e) {
				// TODO Auto-generated catch block
			try {
				server.close();
				} catch (IOException e1) {
				e1.printStackTrace();
				}
			}
		                                           
		
	}
	}

在这里插入图片描述
之前一直有疑惑:
server.accept()阻塞;
但是实际上是我的iframe配置了无效的端口导致的错误;
从上个星期四到上个星期天,再到今天早上,这个问题终于解决了!

展开阅读全文

没有更多推荐了,返回首页