📄 distributeb.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 DistributeB {
/**
* @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 rsocket=null;
private Socket ssocket=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 DistributeB()
{
KB=Double.toString(Math.random()*1000);
}
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;
System.out.print(read+"\n");
}
if(message.equals(ask))
{
System.out.print("equals"+"\n");
String toKA=StringUtils.encrypt(KB,KS);
send("localhost",5000,toKA);
}
else{
String msg=StringUtils.decrypt(message,KB);
System.out.print(msg);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(rsocket!=null)
try {
br.close();
isr.close();
is.close();
rsocket.close();
//serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
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 {
os.close();
ssocket.close();
//serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DistributeB db=new DistributeB();
db.receive(5500);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -