?? distriubutea.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 + -