?? publicb.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 PublicB {
/**
* @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, KA);
os.write(data.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;
System.out.print(read+"\n");
}
String receiveMsg=StringUtils.decrypt(message,KB);
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) {
// TODO Auto-generated method stub
PublicB da=new PublicB();
da.receive(5501);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -