java socket多线程并发消息读取".properties"文件ip和端口配置

本题考察了socket通信、多线程并发、读取文件,io流、面向对象封装,继承、类型转换等知识点。

client代码:

public void start(){
		Socket socket;
		String ss="";
		//调用同一包内ReadProperties类的ReadProperties()方法
		ReadProperties rp=new ReadProperties();
		rp.ReadProperties();
		String IP=rp.getIP();//从a.properties中获得ip
		//从a.properties中获得port,将String类型强转成int类型
		int port=Integer.parseInt(rp.getPort());
		try {
			socket=new Socket(IP,port);
//			System.out.println("IP="+IP);
			KeFuduanjie kf=new KeFuduanjie(socket);//客户端接收消息
			//客户端发送消息【kfs线程发送消息】
			KeFuWuFasong kfs = new KeFuWuFasong(socket);
			kfs.setName("kfs");
			ClientReadFile crf=new ClientReadFile(socket);//从硬盘读取文件
			kf.start();
			kfs.start();
			crf.start();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
	class KeFuduanjie extends Thread{
		Socket socket;
		String ss;
		BufferedReader br;
		public KeFuduanjie(Socket socket){
			this.socket=socket;
			try {
				br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//负责接收服务端来的消息
		public void run(){
			while(true){
				try {
					ss=br.readLine();
					System.out.println(socket.getLocalPort()+"服务器---->客服端: "+ss);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	class KeFuWuFasong extends Thread{
		Socket socket;
		BufferedWriter bw;
		BufferedReader br;
//		OutputStream os;
//		String word;
		public KeFuWuFasong(Socket socket){
			this.socket=socket;
			try {
//				 os=socket.getOutputStream();//2将socket转换成IO流 
				br=new BufferedReader(new InputStreamReader(System.in));//不从控制台输入怎样写死?
				bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//				word="hi";
//				Thread.sleep(2000);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		public void run(){
			while(true){
				try {
//					os.write(word.getBytes());
					bw.write(br.readLine());
					bw.newLine();
					bw.flush();//或用bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
	}
	class ClientReadFile extends Thread{
		Socket socket;
		DataInputStream dis;
		DataOutputStream dos;
		public ClientReadFile(Socket socket){
			this.socket=socket;
			try {
				dis=new DataInputStream(new FileInputStream("F:/commons-logging.properties"));
				dos=new DataOutputStream(socket.getOutputStream());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		public void run(){
//			while(true){
				try {

					Thread.sleep(10000);
					System.out.println("wait 10s");
					int i=0;
					   byte[] b=new byte[1024];
					   String s;
					   while((i=dis.read(b))>0){
					    s=new String(b,"gbk");
					    dos.write(b,0,i);
					    System.out.println("读取的文件信息:"+s);
					   }

					dos.flush();//或用bw.close();
					dis.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
//			}
		}

server端代码:

public class Server {
	public static void main(String[] args) {
		Server server=new Server();
		server.start();
	}
	public void start(){
		ReadProperties rp=new ReadProperties();
		rp.ReadProperties();
		int port=Integer.parseInt(rp.getPort());//2014-4-9将String类型强转成int类型
		try {
			ServerSocket ss=new ServerSocket(port);
			Socket socket = null;
			ServerToClientThread stct = null;
			while(true){
				socket = ss.accept();   //迎接(接收)客户端的Socket访问
				stct = new ServerToClientThread(socket);  //分配一个新线程负责和信赖的Socket沟通
				stct.start();	
				}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
class ServerToClientThread extends Thread{
	Socket socket;
	BufferedReader br;
	BufferedWriter bw;
	String s;
	//建立的同时,和客户端的Socket建立输入、输出流
	public ServerToClientThread(Socket socket){	
		this.socket = socket;
		try {
			bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			} catch (IOException e) {
				e.printStackTrace();
				}
		}
	public void run(){
		ServerReceiveFromClient srfc = new ServerReceiveFromClient(socket);
		srfc.start();
		while(true){
			try {
				bw.write("欢迎光临。");
				bw.newLine();
				bw.flush();
				Thread.sleep(10*1000);
				} catch (IOException e) {
					e.printStackTrace();
					} catch (InterruptedException e) {
						e.printStackTrace();
						}
			}
		}
	}
class ServerReceiveFromClient extends Thread{
	Socket socket;
	BufferedReader br;
	String s;
	public ServerReceiveFromClient(Socket socket){
		this.socket = socket;
		try {
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			} catch (IOException e) {
				e.printStackTrace();
				}
		}
	public void run(){
		while(true){
			try {
				s = br.readLine();
				System.out.println("客户端:"+socket.getInetAddress().getHostAddress()+"发送了:"+s);
				} catch (Exception e) {
					e.printStackTrace();
					}
			}
		}
	}


properties文件内容:

ip=127.0.0.1   
port=8080

java socket多线程并发消息读取".properties"文件ip和端口配置,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。