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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值