📄 publica.java
字号:
package org.hsuper;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
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 PublicA {
/**
* @param args
*/
private static final String KB="---this is KB--- ";
private static final String KA="---this is KA---";
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 data)
{
try {
ssocket=new Socket(host,port);
os=ssocket.getOutputStream();
String sendmsg=StringUtils.encrypt(data, KB);
os.write(sendmsg.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;
}
String receiveMsg=StringUtils.decrypt(message,KA);
System.out.print(receiveMsg);
} 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) throws Exception {
// TODO Auto-generated method stub
File file = new File("test-1.txt");
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[1024];
int count = 0;
while ((count = in.read(tmpbuf)) != -1) {
bout.write(tmpbuf, 0, count);
tmpbuf = new byte[1024];
}
in.close();
byte[] orgData = bout.toByteArray();
String sendmsg=new String(orgData);
PublicA da=new PublicA();
da.send("localhost",5501,sendmsg);
/*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 + -