⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 distriubutea.java

📁 文件为java程序
💻 JAVA
字号:
package org.hsuper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class DistriubuteA {

	/**
	 * @param args
	 */
	private static final String KS="---hello,huachao---";
	private static final String ask="say hello to A";
	private String KB;
	private ServerSocket ss=null;
	private Socket ssocket=null;
	private Socket rsocket=null;
	private String host="";
	private int sport;
	private int rport;
	private OutputStream os=null;
	private InputStream is=null;
	private InputStreamReader isr=null;
	private BufferedReader br=null;
	
	
	public void send(String host,int port,String date)
	{
		try {
			ssocket=new Socket(host,port);
			os=ssocket.getOutputStream();
			os.write(date.getBytes());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(ssocket!=null)
				try {
					ssocket.close();
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
	
	public void receive(int rport)
	{
		try {
			ss=new ServerSocket(rport);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		while(true)
		{
			try {
				rsocket=ss.accept();
				is=rsocket.getInputStream();
				isr=new InputStreamReader(is);
				br=new BufferedReader(isr);
				String read="";
				String message="";
				while((read=br.readLine())!=null)
				{
					message+=read;
				}
				KB=StringUtils.decrypt(message,KS);
				String data="hello,huachao";
				String Smsg=StringUtils.encrypt(data,KB);
				send("localhost",5500,Smsg);
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(rsocket!=null)
					try {
						rsocket.close();
						br.close();
						isr.close();
						is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		}
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DistriubuteA da=new DistriubuteA();
		da.send("localhost",5500,ask);
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		da.receive(5000);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -