?? socketclienta.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 SocketClientA {
/**
* @param args
*/
String host="localhost";
int Sport=5001;
Socket socket=null;
private ServerSocket serverSocket=null;
private int Rport=5550;
private InputStream is=null;
private InputStreamReader isr=null;
private BufferedReader br=null;
private String KS="";
private String KA="";
public SocketClientA()throws IOException{
serverSocket=new ServerSocket(Rport);
}
public void service()
{
while(true)
{
Socket Rsocket=null;
try {
Rsocket=serverSocket.accept();
is=Rsocket.getInputStream();
isr=new InputStreamReader(is);
br=new BufferedReader(isr);
String read="";
String message="";
while((read=br.readLine())!=null)
{
System.out.print(read+"\n");
message+=read;
}
String msg[]=message.split(";");
//System.out.print(msg[0]+"\n"+msg[1]);
KS=StringUtils.decrypt(msg[0],msg[1]);//StringUtils.decrypt(msg[1],msg[1]));
System.out.print("KS:"+KS);
String sendmsg=StringUtils.encrypt("hello,word",KS);
resend("localhost",6000,msg[2]+";"+sendmsg);
} 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)
{
KA=StringUtils.getPASSWORD_CRYPT_KEY();
try {
socket=new Socket(host,Sport);
OutputStream os=socket.getOutputStream();
os.write(KA.getBytes());
os.close();
service();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(socket!=null)
try {
socket.close();
//serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void resend(String host,int port,String date)
{
try {
socket=new Socket(host,port);
OutputStream os=socket.getOutputStream();
os.write(date.getBytes());
os.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(socket!=null)
try {
socket.close();
//serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
SocketClientA sc=new SocketClientA();
sc.send("localhost",5001);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -