import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;/**
* @author 某家:
* @version 创建时间:2015年8月17日 下午3:04:14
* 类说明*/
public classConnect {private static final ThreadLocal threadConnect = new ThreadLocal();private static final String HOST = "192.168.1.120";private static final int PORT = 8888;private staticSocket client;private static OutputStream outStr = null;private static InputStream inStr = null;private static Thread tRecv = new Thread(newRecvThread());private static Thread tKeep = new Thread(newKeepThread());public static voidconnect() throws UnknownHostException, IOException {
client= threadConnect.get();if(client == null){
client= newSocket(HOST, PORT);
threadConnect.set(client);
tKeep.start();
System.out.println("========链接开始!========");
}
outStr=client.getOutputStream();
inStr=client.getInputStream();
}public static voiddisconnect() {try{
outStr.close();
inStr.close();
client.close();
}catch(IOException e) {
e.printStackTrace();
}
}private static classKeepThread implements Runnable {public voidrun() {try{
System.out.println("=====================开始发送心跳包==============");while (true) {try{
Thread.sleep(1000);
}catch(InterruptedException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("发送心跳数据包");
outStr.write("send heart beat data package !".getBytes());
}
}catch(IOException e) {
e.printStackTrace();
}
}
}private static classRecvThread implements Runnable {public voidrun() {try{
System.out.println("==============开始接收数据===============");while (true) {byte[] b = new byte[1024];int r =inStr.read(b);if(r>-1){
String str= newString(b);
System.out.println( str );
}
}
}catch(IOException e) {
e.printStackTrace();
}
}
}public static voidmain(String[] args) {try{
Connect.connect();
tRecv.start();
}catch(UnknownHostException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}